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 8194979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:59:04+00:00 2026-06-07T04:59:04+00:00

With trial and error, i learned that 11_451_104 is the magic number, which causes

  • 0

With trial and error, i learned that 11_451_104 is the magic number, which causes my machine to throw the OOM error.

Using 11_451_103, i am packing with as much data as it can hold.

private static void init() {
    int i = 0;
    try {
        while (++i < 11451104) {
            list.add("a");
        }
    } catch (OutOfMemoryError e) {
        System.out.println("oh no, not again :("); // <-- Not getting here
    }
}

If on the next line i do

    String x = "some new string";

I would expect an exception to occur, as heap can’t allocate space for 1 more string. And yet, it does not.

If i try to add this new string to the list,

    String x = "some new string"; // <-- expect OOM Error to happen here
    list.add(x);

Program does abort with OOM. Why did not this happen upon string allocation?

How to best protect oneself knowing that OOM is a possibility, as unknown (possibly very big) amount of data may need to be held? Is serialization and persistence to disk a way to go to handle this?

  • 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-06-07T04:59:06+00:00Added an answer on June 7, 2026 at 4:59 am

    Why did not this happen upon string allocation?

    The list.add(...) method may also be allocating memory. If the list is an LinkedList then each add call creates a new list node. If it is an ArrayList, then add may cause the backing array to be reallocated.

    (UPDATE – and I just noticed that you are not even creating new string objects. You are continually adding the same literal string "a" to the list, and that guarantees that the OOME won’t occur in string allocation!)

    How to best protect oneself knowing that OOM is a possibility …

    It is tempting to try to catch and recover from an OOME, but it can be risky. The problem is that you never know for sure what your application (e.g. some library method called by your application code) was actually trying to allocate, and whether or not the Error occurred at an inconvenient time and has left some important data structure in a partial or inconsistent state. Your application may therefore not be in a fit state to attempt to recover.

    In general, the safest thing to do when you get an OOME is to cause the application to exit immediately. Don’t try to commit transactions, etc. Let the database’s automatic rollback clean up any uncommitted transactions when your application’s database connection socket/pipe/whatever gets closed by the OS.

    In fact, the “don’t attempt to recover” advice applies to all Error exceptions. It is just that OOME’s are a case where developers are inclined to ignore the advice ‘cos they think they know better …


    In terms of “protecting oneself”, the general solution is to keep a copy of important state safely on non-volatile storage; e.g. by writing it to a database, serializing to a flat file and so on. The specifics (e.g. which technology is best) will depend on the data, how your application uses it, and how you would make your application restartable.

    The problem / situation is not qualitatively different to the problem of dealing with possible application crashes, OS reboots, power failures and so on.

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

Sidebar

Related Questions

After some trial and error (ok, just error) I figured out that c089b69c3d contained
After many cases of trial and error, I have concluded that the only solution
After painful trial and error, I arrived at a grotesque function that behaves the
I have discovered through trial and error that the MATLAB engine function is not
I have a math problem that I solve by trial and error (I think
I would like to code an script that would with some trial and error
I just worked out, by trial-and-error, that IE 7 has an upper limit of
After much research and trial and error I found how to store the items
I have a dilemma on my hands. After much trial and error, I still
I know the $downloadfile - and I want the $user_id. By trial and error

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.