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

Q

qazmlp

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!
 
C

Chris Smith

qazmlp said:
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
 
T

Tony Morris

Chris Smith said:
qazmlp said:
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
 
C

Chris Uppal

Chris said:
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
 
T

Thomas G. Marshall

Chris said:
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/[email protected]&rnum=9

Depending on your newsreader you might have to piece that link together.
 
C

Chris Smith

Thomas said:
http://groups.google.com/[email protected]&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:

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top