• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Small doubt on jvm memory handling

 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic