Note that there are some explanatory texts on larger screens.

plurals
  1. PODoes Immutability of Strings in Java cause Out Of Memory
    primarykey
    data
    text
    <p>I have written a simple Java program that reads a million rows from the Database and writes them to a File.</p> <p>The max memory that this program can use is 512M.</p> <p>I frequently notice that this program runs Out Of Memory for more than 500K rows. </p> <p>Since the program is a very simple program it is easy to find out that this doesn't have a memory leak. the way the program works is that it fetches a thousand rows from the Database, writes them to a file using Streams and then goes and fetches the next thousand rows. The size of each row varies but none of the rows is huge. On taking a dump while the program is running the older string are easily seen on the heap. These String in heap are unreachable which means they are waiting to get Garbage collected. I also believe that the GC doesn't necessarily run during the execution of this program which leaves String's in the heap longer than they should. </p> <p>I think the solution would be to use long Char Arrays(or Stringbuffer) instead of using String objects to store the lines that are returned by the DB. The assumption is that I can overwrite the contents of a Char Array which means the same Char Array can be used across multiple iterations without having to allocate new Space each time.</p> <p>Pseudocode :</p> <ol> <li>Create an Array of Arrays using new char[1000][1000];</li> <li>Fill the thousand rows from DB to the Array.</li> <li>Write Array to File.</li> <li>Use the same Array for next thousand rows </li> </ol> <p>If the above pseudocode fixes my problem then in reality the Immutable nature of the String class hurts the Java programmer as there is no direct way to claim the space used up by a String even though the String is no longer in use.</p> <p>Are there any better alternatives to this problem ?</p> <p>P.S : I didn't do a static analysis alone. I used yourkit profiler to test a heap dump. The dump clearly says 96% of the Strings have NO GC Roots which means they are waiting to get Garbage collected. Also I don't use Substring in my code.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload