• Post Reply Bookmark Topic Watch Topic
  • New Topic

Small doubt on jvm memory handling  RSS feed

 
Dinuka Arsakularatne
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Looking at the following two links

http://java.sun.com/docs/books/jvms/second_edition/html/Overview.doc.html#15722
http://rmathew.blogspot.com/2007/01/local-variables-in-java.html

It says that all local variables are stored within a fixed-size array and as the first link points out "A single local variable can hold a value of type boolean, byte, char, short, int, float, reference, or returnAddress. A pair of local variables can hold a value of type long or double."

My doubt then this means you do not need to use StringBuilders right? because even if you use a normal String and keep appending to it, as long as its memory scope is "local" it will only use a single array space isnt it? Ofcourse there is still the String pool space which will grow but as far as concatenating is conerned it will always only be within a single frame ( which is the method call as explained in the first link) and it will be removed from space as soon as the method returns. Any thoughts on this would be much appreciated.


Regards,
Dinuka Arseculeratne
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Disagree. Memory is cheap, but on modern applications, processor power is expensive. If you start creating multiple String objects, each has to have a memory location found, and then deleted by the garbage collector. You gain a considerable performance advantage by using a StringBuilder, particularly if you can predict the size of the final String and therefore allocate enough memory at the start of the method.

To go from an Intel Core 3.06GHz to a 3.33GHz would cost me £321 extra (US$530, €360, 25500 Rupees) (a Pentium would cost much less) whereas an extra 1GB of memory would cost £30 (2400 Rs, €33, $50) and an extra 4GB would cost barely twice that.

You avoid frequently reallocating memory with a StringBuilder, so it gives much faster performance, even if you have lots of free heap space all the time.
 
Dinuka Arsakularatne
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Campbell,

Thank you for your response. I totally agree with your points too. Its just that after reading those two posts i was wondering because if frames are dropped after the method call ends that means the memory is freed right? and what does it mean by occupying memory in a fixed-size array in that post? But i agree on the fact that processing power must be well utilised.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!