Re: Memory Leaks & Strings

Discussion in 'Java' started by Harald Hein, Jul 31, 2003.

  1. Harald Hein

    Harald Hein Guest

    "Laura P" wrote:

    > double preferredSize = s.readDouble();
    > properties.setProperty("preferredWidth", preferredSize+"");
    >
    > I am not sure whether memory efficiency could be improved by doing
    > this in a different way.


    This does not give you a memory leak. It might be a little bit more
    work for the VM to calculate the result, but it might also be that the
    overhead is optimized away. I just think it is ugly.

    > In other concatenation instances, I have
    > changed to using StringBuffers, alternatively, would any
    > difference be made by using preferredSize.toString()?


    StingBuffer will not really help here. In other
    cases it can sead up string processing. preferredSize.toString() does
    not exist. Maybe you want String.valueOf(), or Double.valueOf().
    Harald Hein, Jul 31, 2003
    #1
    1. Advertising

  2. Harald Hein

    Laura P Guest

    > This does not give you a memory leak. It might be a little bit more
    > work for the VM to calculate the result, but it might also be that the
    > overhead is optimized away. I just think it is ugly.


    > StingBuffer will not really help here. In other
    > cases it can sead up string processing. preferredSize.toString() does
    > not exist. Maybe you want String.valueOf(), or Double.valueOf().


    (Oops, I meant String.valueOf(), not preferredSize.toString() ;-)

    I don't think I actually meant memory leak, more high memory usage. I
    seem to remember that Strings are created into a 'persistent pool',
    where each use of '+' created a new String object, I have been trying
    to reduce this as much as possible. I was first aware of this when
    using OptimizeIt, and found that a long concatenated String creation
    that was being called very frequently (logging!) was causing the heap
    usage to increase steadily (about 1k, every 3-5 seconds) Replacing
    this with a StringBuffer has slowed this rate of increase
    substantially, but has driven me to look for other areas where
    unnecessarily high memory usage could be avoided.

    I have another question arising from one of the responses so far - in
    that java uses a StringBuffer behind the scenes to do every String
    concatenation. I always thought that for performance and memory
    optimisation StringBuffers should be used, as concatenation involves
    the copying and reallocation of the contents of both sides of the '+'.
    If it is just a hidden StringBuffer, then how is this optimisation
    achieved?
    Laura P, Jul 31, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon Skeet

    Re: Memory Leaks & Strings

    Jon Skeet, Jul 30, 2003, in forum: Java
    Replies:
    0
    Views:
    454
    Jon Skeet
    Jul 30, 2003
  2. Steven

    Re: Memory Leaks & Strings

    Steven, Aug 14, 2003, in forum: Java
    Replies:
    1
    Views:
    421
    pete kirkham
    Aug 15, 2003
  3. Steven

    Re: Memory Leaks & Strings

    Steven, Aug 14, 2003, in forum: Java
    Replies:
    1
    Views:
    343
    Chris Smith
    Aug 15, 2003
  4. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    755
    Malcolm
    Jun 24, 2006
  5. Replies:
    4
    Views:
    118
Loading...

Share This Page