Re: Memory Leaks & Strings

Discussion in 'Java' started by Steven, Aug 14, 2003.

  1. Steven

    Steven Guest

    "Laura P" <> wrote in message
    news:...

    > properties.setProperty("preferredWidth", preferredSize+"");
    >


    preferredSize + "" will generally do this:

    String.operator+(preferredSize, "");

    Java will call an implemented function, called the +operator and will pass 2
    parameters of which 1 is of type double and the other one is an empty string
    which has the size of 1 byte (0x00).

    the operator+() function will do this:

    convert preferredSize into a string:

    String pSize = Double.toString(preferredSize);

    and then append the empty string of size 1 to the end of pSize.

    pSize[pSize.length()] = emptystring[0] // emptystring=""

    and then:

    return pSize;

    (33 + "")

    will return "33"

    This is the same as Double.toString(33)

    this will return "33"

    Note that "33"[3] == 0x00 (stop byte)

    > I am not sure whether memory efficiency could be improved by doing


    This is not a memory leak. This is more of a performance leak. A memory leak
    occures when you declare a global variable, but don't use it or you new an
    instant or class, but that instant or class is not cleared out of memory.

    > this in a different way. In other concatenation instances, I have
    > changed to using StringBuffers, alternatively, would any difference be
     
    Steven, Aug 14, 2003
    #1
    1. Advertising

  2. Steven

    pete kirkham Guest

    Steven wrote:
    > "Laura P" <> wrote in message
    > news:...
    >
    >
    >> properties.setProperty("preferredWidth", preferredSize+"");
    >>

    >
    >
    > preferredSize + "" will generally do this:
    >
    > String.operator+(preferredSize, "");
    >
    > Java will call an implemented function, called the +operator and will pass 2
    > parameters of which 1 is of type double and the other one is an empty string
    > which has the size of 1 byte (0x00).


    I think you will find that javac expands it to:

    new StringBuffer().append(preferredSize).append("").toString();

    > the operator+() function will do this:

    There is no C++ style operator+() function in Java, anymore than there
    is a Lisp style compiler macroexpansion loop. Though you can explain
    what the effect in either terms, what actually happens is a non-generic
    Java.

    > String pSize = Double.toString(preferredSize);
    >
    > and then append the empty string of size 1 to the end of pSize.
    >
    > pSize[pSize.length()] = emptystring[0] // emptystring=""
    >
    > and then:
    >
    > return pSize;
    >
    > (33 + "")
    >
    > will return "33"


    No. Read the API on StringBuffer and its append method. Strings are
    immutable in Java (reflection hacks excluded), and are not the same as
    character arrays.

    > This is the same as Double.toString(33)
    >
    > this will return "33"


    Double.toString(33) peforms a widening cast on its input to a double and
    will return the representation "33.0". Try it.

    > Note that "33"[3] == 0x00 (stop byte)


    No. There is an internal char array which underlies the String in Java,
    and integer fields to indicate the first and last character index in the
    array. The length of this array is sufficent to contain the string data,
    and occasionally will be slightly longer if memory efficiency is served.

    For example Double.toString() will allocate a large enough array to
    guarentee there is enough space for the representation, but the actual
    string may be a couple of characters shorter. This is achieved through
    the package access level constructor, so can't be used outside of the
    java.lang package to create mutable strings.

    The length and start index are stored independently, allowing the
    strings "123456" and "123455".substring(2, 3) to share the same
    underlying array. This sharing means there can be no terminating /u00000
    in Java strings.


    Pete
     
    pete kirkham, Aug 15, 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:
    461
    Jon Skeet
    Jul 30, 2003
  2. Harald Hein

    Re: Memory Leaks & Strings

    Harald Hein, Jul 31, 2003, in forum: Java
    Replies:
    1
    Views:
    404
    Laura P
    Jul 31, 2003
  3. Steven

    Re: Memory Leaks & Strings

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

    Strings, Strings and Damned Strings

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

Share This Page