Results 1 to 14 of 14
Like Tree2Likes
  • 2 Post By JosAH

Thread: OutOfMemoryError - String Pool?

  1. #1
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default OutOfMemoryError - String Pool?

    I'm working on a large reporting program and regularly encounter an OutOfMemoryError. I've tracked this down to being primarily char arrays (via visualvm). When identical reports are generated the memory usage does not increase significantly; however, new reports cause it to jump up very rapidly. So, I think Java's String pool is storing millions and millions of Strings and does not seem to clean itself out (not really sure how it works internally). Is there any way to avoid storing Strings in the pool? Or clear it out after I am finished with a report? Or any other suggestions?

    Thanks.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default Re: OutOfMemoryError - String Pool?

    Java's String pool only stores String literals, or Strings you've interned. I highly doubt you can blame that for your problem.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: OutOfMemoryError - String Pool?

    I have no idea (yet!). But I would find it strange that the interning of strings (which is done to help performance and save memory, not cause problems) would be the issue. Are you certain you aren't keeping some hard references around somewhere?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default Re: OutOfMemoryError - String Pool?

    My guess is that it's all the String fiddling that is done in your program; use StringBuilders as much as possible.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default Re: OutOfMemoryError - String Pool?

    Hmm. I'm not really concatenating a large amount of Strings, but when I do I am using StringBuilder. I'll have to look into the hard references. Thanks.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: OutOfMemoryError - String Pool?

    Find out what objects own those char arrays.
    That'll tell you what you are holding onto.

    Since running new reports results in the problem then I would look at whether you are holding onto the old reports in memory.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: OutOfMemoryError - String Pool?

    I don't see any metrics actually being mentioned in this thread. How much memory does Java have maximally?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0

    Default Re: OutOfMemoryError - String Pool?

    there are some ways:-
    1 - use Stringbuffer instead of String.
    2 - make your null if your are making more instance from your string.
    3 - use GarbageCollector to freeMemory


    try out all three. your problem solved....:)

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: OutOfMemoryError - String Pool?

    1. Unless you are manipulating the String then there is no point in using a StringBuffer.
    2. Just keep everything in the correct scope and then you don't need to do this. I usually look on code where various variables are set to null for no good reason as code written by someone who doesn't understand what is going on.
    3. The garbage collector frees memory all by itself. It needs no assistance beyond some tuning for special occasions. Manually requesting a collection will have no effect on an OutOfMemoryError.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: OutOfMemoryError - String Pool?

    1) the OP already clearly stated that StringBuilder is being used
    2) That is Java 1.0 thinking. Manually nulling things is actually counter-productive in modern JVMs.
    3) You don't USE the garbage collector, it is a totally independent thing which you cannot touch. You can only pass hints to it, but it will likely totally ignore you.

    Nice try.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default Re: OutOfMemoryError - String Pool?

    Quote Originally Posted by gimbal2 View Post
    3) You don't USE the garbage collector, it is a totally independent thing which you cannot touch. You can only pass hints to it, but it will likely totally ignore you.
    For the SUN implementations this is only partly true: although the specs say it's only a hint, Sun's implementation does/did a garbage collection. It as even recommended by James himself in his book "The Javva Programming Language" and I used it on occasion when users started whining about a visible hiccup in a lengthly calculation (a progress bar was shown and it stopped sometimes for a very short period of time). I hate users, they belong in the fire place.

    kind regards,

    Jos
    masijade and KevinWorkman like this.
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: OutOfMemoryError - String Pool?

    In my experience with the old sun versions, you had to trigger at least twice in a row before it would actually happen. For a wonky plugin system I wrote back in the day where you could load/unload jars before this was implemented in the JDK itself, I triggered even three times because once in a blue moon two times wasn't even enough to get it to play ball.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: OutOfMemoryError - String Pool?

    If you are certain that it is your char arrays that are causing the problem, then why not manage them with soft references? As long as the references to the arrays are only softly reachable, the GC will free up any soft references before throwing an OutOfMemoryError.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default Re: OutOfMemoryError - String Pool?

    Quote Originally Posted by gimbal2 View Post
    In my experience with the old sun versions, you had to trigger at least twice in a row before it would actually happen. For a wonky plugin system I wrote back in the day where you could load/unload jars before this was implemented in the JDK itself, I triggered even three times because once in a blue moon two times wasn't even enough to get it to play ball.
    According to daddy James, you had to call it until Runtime.freeMemory() didn't rise anymore; because I am a fine, young, law obedient puppy, that was how I did it ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. DB2 Connection Pool
    By dilshod in forum JDBC
    Replies: 1
    Last Post: 07-22-2013, 10:23 AM
  2. Replies: 3
    Last Post: 07-05-2011, 07:18 PM
  3. Connection Pool
    By mine0926 in forum Advanced Java
    Replies: 57
    Last Post: 06-18-2011, 04:07 AM
  4. Help with pool connections
    By juamd in forum Threads and Synchronization
    Replies: 1
    Last Post: 06-01-2011, 07:02 PM
  5. connection pool for db2
    By paty in forum JDBC
    Replies: 1
    Last Post: 08-06-2007, 03:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •