• (2 Pages)
  • +
  • 1
  • 2

The String Pool Rate Topic: ***** 2 Votes

#16 raghu132  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 27-August 12

Posted 27 August 2012 - 05:41 AM

Very useful information.
Thanks for nice post.
Was This Post Helpful? 0
  • +
  • -

#17 phantomreference  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-May 13

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?
Was This Post Helpful? 0
  • +
  • -

#18 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,473
  • Joined: 27-December 08

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.
Was This Post Helpful? 1
  • +
  • -

#19 phantomreference  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-May 13

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'.
Was This Post Helpful? 0
  • +
  • -

#20 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,023
  • Joined: 15-July 08

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.
Was This Post Helpful? 1
  • +
  • -

#21 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,473
  • Joined: 27-December 08

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.
Was This Post Helpful? 1
  • +
  • -

#22 phantomreference  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-May 13

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.
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?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2