Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6532773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:02:42+00:00 2026-05-25T10:02:42+00:00

I knowingly created the following class to cause out of memory error public class

  • 0

I knowingly created the following class to cause out of memory error

public class Test1
{
    public static void main(String[] args)
    {
        StringBuffer sb = new StringBuffer();
        while(true)
        {
            Test1 a = new Test1();
            sb.append(a.toString());
        }
    }
} 

As I expected this above class fails with what I wanted…

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
        at java.util.Arrays.copyOf(Unknown Source)
        at java.lang.AbstractStringBuilder.expandCapacity(Unknown Source)
        at java.lang.AbstractStringBuilder.append(Unknown Source)
        at java.lang.StringBuffer.append(Unknown Source)
        at Test1.main(Test1.java:10)

but this:

public class Test1
{
    public static void main(String[] args)
    {
        StringBuffer sb = new StringBuffer();
        while(true)
        {
            Test1 a = new Test1();
            System.out.println(sb.toString());
            sb.append(a.toString());
        }
    }
} 

Does not crash. Runs just fine, by printing the object address over and over again on console.

My question is:

What difference a simple SOP made?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:02:43+00:00Added an answer on May 25, 2026 at 10:02 am

    Your assumption that there is no OutOfMemoryError is likely to be incorrect. It is just massively delayed. Printing a string that is getting bigger and bigger on the out stream takes so much time, that your loop may take an hour to run out of memory.

    You can double-check this, by printing only every 10th, 100th, 1000th time. You’ll see the error will occur the earlier the less IO you generate. Probably you’ll see a curve like this in jconsole:

    enter image description here

    As you can see, the heap is slowly but steadily going up. Even if I try to force garbage collection (15:02 and 15:07), I cannot free all memory anymore. But since I’m still only at 5% of my heap, I’ll stop running your code now 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following inheritance tree: I have a public class BaseForm : Form This
I have an object with the following hierarchy class Account string username List Delegations
How can i reference a class property knowing only a string? class Foo {
How many String object are created I am studying for the SCJP I cant
I have created an external class ( TheClass ) with an event ( TheEvent
Here is my scenario: I have the following menu created from a viewModel <ul>
I am following an intro tutorial http://www.linuxinsight.com/files/alp/alp-ch01-getting-started.pdf . I have created the source files
As a debugger/test program for my brainf*ck implementation, I have created the following counting
I'm trying to create new object member with a dynamically created string as the
Knowing an exception code, is there a way to find out more about what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.