Very useful information.
Thanks for nice post.
The String Pool
#17
Posted 06 May 2013 - 12:28 PM
public static void main(String[] args) { ArrayList<String> countries = new ArrayList<String>(); String ctry; for (int i=0; i < 1000000000; i++) { ctry = "United states states states states states states of America"; countries.add(ctry); } System.out.println("Total memory available for use in MB = " + Runtime.getRuntime().totalMemory()/1000000); }
The above program crashes with OutOfMemory Exception. Any idea? I was excepting this to create just one string for the country. Does adding it to the ArrayList made the difference?
#18
Posted 06 May 2013 - 12:30 PM
An ArrayList stores an Object[] internally, which takes up memory too. So the array is being resized constantly, which is causing your Error.
Also, in the future, please make sure to post help questions in the help forums, not on tutorials.
Also, in the future, please make sure to post help questions in the help forums, not on tutorials.
#19
Posted 07 May 2013 - 05:40 AM
Thanks for your response.
My understanding about the string pool is that whenever the JVM encounters a string literal declaration, it should check the string pool first, if it is present, it should return a reference pointing at that string in the pool.
Also, an array list or any collection object for that matter should contain only references to the objects stored in them. In this case, all such references should be pointing to the same memory location containing the value of the string literal (one and only) in the string pool.
So, I would still expect that there is just one string literal in the memory which is pointed at by the millions of references (pointing at the string in the string pool) stored in the array list. In which case, there should NOT have been an OutOfMemoryException.
Or, is the string pool implementation is up to the jvm implentation and my JVM happens to not to implement the same? Nevertheless, running a simple java test shows that references to two string literals point at the same memory location. That is "==" comparison returns true.
Note: Apologies friend, I did not really understand what you said about 'where to post the questions'.
My understanding about the string pool is that whenever the JVM encounters a string literal declaration, it should check the string pool first, if it is present, it should return a reference pointing at that string in the pool.
Also, an array list or any collection object for that matter should contain only references to the objects stored in them. In this case, all such references should be pointing to the same memory location containing the value of the string literal (one and only) in the string pool.
So, I would still expect that there is just one string literal in the memory which is pointed at by the millions of references (pointing at the string in the string pool) stored in the array list. In which case, there should NOT have been an OutOfMemoryException.
Or, is the string pool implementation is up to the jvm implentation and my JVM happens to not to implement the same? Nevertheless, running a simple java test shows that references to two string literals point at the same memory location. That is "==" comparison returns true.
Note: Apologies friend, I did not really understand what you said about 'where to post the questions'.
#20
Posted 07 May 2013 - 07:04 AM
Instead of using an ArrayList, try making a static array instead. That way, you can see that there is a constant memory allocation due to the array, as opposed to the dynamic one done by the ArrayList.
#21
Posted 07 May 2013 - 06:34 PM
An array is a block of memory. So when you keep adding the same String, the multiple positions in the array do reference that String because of the String pool. However, there are still allocated spots in memory because of the array.
#22
Posted 08 May 2013 - 03:09 AM
Thanks guys! I got it now. The out of memory (heap) should have been caused by the memory taken up by the 'references' to the 'single' string literal in the pool.
To confirm this, I ran the program two times with two different string literals shown below.
In BOTH the cases the program crashed after about 195 million records were added to the ArrayList. We can infer from this that the heap was filled up by all those references to that one string literal. Hence we can conclude that there was just one string created in the memory, right?
To confirm this, I ran the program two times with two different string literals shown below.
ctry = "United states states states states states states of America";
ctry = "USA";
In BOTH the cases the program crashed after about 195 million records were added to the ArrayList. We can infer from this that the heap was filled up by all those references to that one string literal. Hence we can conclude that there was just one string created in the memory, right?