Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > buff = new StringBuffer(10000); buff.toString(); Will GC be complete here?

Reply
Thread Tools

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

 
 
qazmlp
Guest
Posts: n/a
 
      02-17-2004
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!
 
Reply With Quote
 
 
 
 
Chris Smith
Guest
Posts: n/a
 
      02-17-2004
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
 
Reply With Quote
 
 
 
 
Tony Morris
Guest
Posts: n/a
 
      02-17-2004
"Chris Smith" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)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/.../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


 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      02-18-2004
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


 
Reply With Quote
 
Thomas G. Marshall
Guest
Posts: n/a
 
      02-19-2004
Chris Uppal <(E-Mail Removed)-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=sk...mon.com&rnum=9

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


 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      02-19-2004
Thomas G. Marshall wrote:
> http://groups.google.com/groups?q=sk...mon.com&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...og.peramon.com


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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
doubts on scanf("%100[^\n]%*[^\n]", buff); lovecreatesbea...@gmail.com C Programming 1 03-29-2007 04:52 PM
Buff's device Zara C++ 3 08-30-2005 06:24 AM
New Releases: Mr. & Mrs. Smith, Complete Buffy & Enterprise: Updated complete R1 DVD DB & info lists Doug MacLean DVD Video 0 08-09-2005 05:07 AM
New Releases: Criterion additions, WB Gangsters & Coupling: Complete: Updated complete downloadable R1 DVD DB& info lists Doug MacLean DVD Video 4 11-07-2004 05:20 PM
Mr. Bean Complete Bean NOT complete!! Waterperson77 DVD Video 5 12-10-2003 05:22 PM



Advertisments