buff = new StringBuffer(10000); buff.toString(); Will GC be complete here?

Discussion in 'Java' started by qazmlp, Feb 17, 2004.

  1. qazmlp

    qazmlp Guest

    import java.util.*;

    class garbage_collection {

    public static void main(String args[]){
    System.out.println("Init");
    for(int i = 0; true; i++) {
    System.out.println(i) ;
    String str = test1() ; // After this, will it be fully
    garbage
    // collected (10000 bytes) or only
    6 bytes?
    }
    }

    private static String test1() {
    StringBuffer buff = new StringBuffer(10000); // Huge
    StringBuffer
    buff.append("Hello");
    return buff.toString(); // The reference is returned here.
    // So, not available for garbage
    collection
    }
    }


    Will 'buff' be completely garbage collected or only the bytes occupies
    by the "Hello" will be GC-ed here?

    Basically, I would like to know whether StringBuffer.toString() will
    return the reference for the Complete 10000 bytes so that, it can be
    garbage collected easily.

    Thanks!
     
    qazmlp, Feb 17, 2004
    #1
    1. Advertising

  2. qazmlp

    Chris Smith Guest

    qazmlp wrote:
    > Will 'buff' be completely garbage collected or only the bytes occupies
    > by the "Hello" will be GC-ed here?
    >
    > Basically, I would like to know whether StringBuffer.toString() will
    > return the reference for the Complete 10000 bytes so that, it can be
    > garbage collected easily.


    First of all, these details are part of the implementation of the Java 2
    SDK, and are not specified. However, the SDK is clearly designed around
    a certain implementation, and that implementation can be found almost
    universally. So the rest of my answer applies to the Sun reference
    implementation of the J2SDK, and probably applies to most other
    implementations... but it's not guaranteed.

    There are three objects involved for each iteration of the loop in main:
    a StringBuffer, a String, and a (internal) char[] which is used to hold
    the real character data. The StringBuffer object (which is referred to
    by your reference called buff) will be eligible for garbage collection
    after the toString call is completed at the end of the test1 method.
    The String object (which is referred to by str) will be eligible for
    collection at the end of each iteration of the for loop. The char[]
    object (which doesn't have any direct references from your code but is
    used internally by both the String and StringBuffer objects) also
    becomes eligible for collection at the end of each iteration of the
    loop.

    Your question above isn't really clear, but it sounds as if you're
    asking whether the whole character data or only part will be eligible
    for collection at any given point. The answer (and this is specified,
    not an implementation detail) is that objects are preserved or garbage
    collected in their entirety. There is no such thing as garbage
    collection of part of an array; it's all or nothing.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Feb 17, 2004
    #2
    1. Advertising

  3. qazmlp

    Tony Morris Guest

    "Chris Smith" <> wrote in message
    news:4.net...
    > qazmlp wrote:
    > > Will 'buff' be completely garbage collected or only the bytes occupies
    > > by the "Hello" will be GC-ed here?
    > >
    > > Basically, I would like to know whether StringBuffer.toString() will
    > > return the reference for the Complete 10000 bytes so that, it can be
    > > garbage collected easily.

    >
    > First of all, these details are part of the implementation of the Java 2
    > SDK, and are not specified. However, the SDK is clearly designed around
    > a certain implementation, and that implementation can be found almost
    > universally. So the rest of my answer applies to the Sun reference
    > implementation of the J2SDK, and probably applies to most other
    > implementations... but it's not guaranteed.
    >
    > There are three objects involved for each iteration of the loop in main:
    > a StringBuffer, a String, and a (internal) char[] which is used to hold
    > the real character data. The StringBuffer object (which is referred to
    > by your reference called buff) will be eligible for garbage collection
    > after the toString call is completed at the end of the test1 method.
    > The String object (which is referred to by str) will be eligible for
    > collection at the end of each iteration of the for loop. The char[]
    > object (which doesn't have any direct references from your code but is
    > used internally by both the String and StringBuffer objects) also
    > becomes eligible for collection at the end of each iteration of the
    > loop.
    >
    > Your question above isn't really clear, but it sounds as if you're
    > asking whether the whole character data or only part will be eligible
    > for collection at any given point. The answer (and this is specified,
    > not an implementation detail) is that objects are preserved or garbage
    > collected in their entirety. There is no such thing as garbage
    > collection of part of an array; it's all or nothing.
    >
    > --
    > www.designacourse.com
    > The Easiest Way to Train Anyone... Anywhere.
    >
    > Chris Smith - Lead Software Developer/Technical Trainer
    > MindIQ Corporation


    To the OP,
    Note that Chris said
    "eligible for garbage collection"
    which is different to what you said, which is
    "After this, will it be fully garbage collected "
    Being eligible for garbage collection doesn't necessarily imply that the
    object will be garbage collected immediately, or at all.

    One of my favourite articles on garbage collection,
    http://java.sun.com/developer/Books/performance/performance2/appendixa.pdf

    --
    Tony Morris
    (BInfTech, Cert 3 I.T.)
    Software Engineer
    IBM Australia - Tivoli Security Software
    (2003 VTR1000F)
    Sun Certified Programmer for the Java 2 Platform (1.4)
    Sun Certified Developer for the Java 2 Platform
     
    Tony Morris, Feb 17, 2004
    #3
  4. qazmlp

    Chris Uppal Guest

    Chris Smith wrote:

    > There are three objects involved for each iteration of the loop in main:
    > a StringBuffer, a String, and a (internal) char[] which is used to hold
    > the real character data.


    Well, whatdya know ?

    I was about to reply that I thought there must be at least four. The
    StringBuffer, its large internal char[] array, the String object, and its small
    internal char[] array. Since the String is not allowed to change when the
    contents of the StringBuffer are changed, it is not possible for the two
    objects to share a common char[] buffer.

    But it turns out that the Sun implementation *does* share the buffer, but marks
    it copy-on-write in the StringBuffer object.

    Live and learn...

    Thanks.

    -- chris
     
    Chris Uppal, Feb 18, 2004
    #4
  5. Chris Uppal <-THIS.org> coughed up the
    following:

    > Chris Smith wrote:
    >
    >> There are three objects involved for each iteration of the loop in
    >> main:
    >> a StringBuffer, a String, and a (internal) char[] which is used to
    >> hold
    >> the real character data.

    >
    > Well, whatdya know ?
    >
    > I was about to reply that I thought there must be at least four. The
    > StringBuffer, its large internal char[] array, the String object, and
    > its small internal char[] array. Since the String is not allowed to
    > change when the contents of the StringBuffer are changed, it is not
    > possible for the two objects to share a common char[] buffer.
    >
    > But it turns out that the Sun implementation *does* share the buffer,
    > but marks it copy-on-write in the StringBuffer object.


    Jon Skeet made the point a long time ago that it is often critical to do the
    following:

    String myNewString = new String(myOldString);

    to trim away any excess unused array space that is still hidden within the
    string.

    http://groups.google.com/groups?q=&rnum=9

    Depending on your newsreader you might have to piece that link together.
     
    Thomas G. Marshall, Feb 19, 2004
    #5
  6. qazmlp

    Chris Smith Guest

    Thomas G. Marshall wrote:
    > http://groups.google.com/groups?q=&rnum=9
    >
    > Depending on your newsreader you might have to piece that link together.


    Just a note for anyone watching. When posting links to Google articles,
    you can remove a lot of those parameters -- everything except "selm",
    which is the message ID. For example, it would work just as well if you
    had said:

    > http://groups.google.com/groups?selm=


    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Feb 19, 2004
    #6
    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. Jerry

    Thread.sleep(10000)

    Jerry, Aug 1, 2005, in forum: Java
    Replies:
    2
    Views:
    15,497
    John Currier
    Aug 2, 2005
  2. Replies:
    6
    Views:
    39,792
    ksrao
    Aug 30, 2010
  3. Zara

    Buff's device

    Zara, Aug 30, 2005, in forum: C++
    Replies:
    3
    Views:
    393
  4. Replies:
    1
    Views:
    649
    Eric Sosman
    Mar 29, 2007
  5. Fergus McMenemie

    LibXML element->toString vs document->toString

    Fergus McMenemie, Jul 12, 2012, in forum: Perl Misc
    Replies:
    5
    Views:
    650
    Fergus McMenemie
    Jul 17, 2012
Loading...

Share This Page