wood burning stoves 2.0*
The moose likes Java in General and the fly likes gc and the string pool Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "gc and the string pool" Watch "gc and the string pool" New topic
Author

gc and the string pool

paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20542
    ∞

Consider the code snippet:
String s1 = "The Tick!" ;
String s2 = new String("Arthur");
s1 = null ;
s2 = null ;
How many objects are ready for gc? I'm thinking that there are two string constants from the string pool and they don't get garbage collected. Just the one string object that was created using "Arthur" - THAT gets collected.
So what really happens? Is one or three object ready for gc?

permaculture Wood Burning Stoves 2.0 - 4-DVD set
Pawel Veselov
Ranch Hand

Joined: Jan 14, 1999
Posts: 165
Java's gc is a very mysterious thing.
Testing your case, I found the following:
If this variables are local, no classes freed at all <WHY?>
If this variables was defined and initialized globally, than 1 class is collected as garbage:
<pre>
class Test10 {
Test10() {
String s1 = "The Tick!" ;
String s2 = new String("Arthur");
s1 = null;
s2 = null;
System.gc();
}
public static void main(String a[]) {
new Test10();
}
}
[vps@druid]~/jprogs$ java -verbosegc Test10
<GC: need to expand mark bits to cover 32768 bytes>
<GC: compactHeap took 5 ms, blocks_moved=0x885>
<GC: last free block at 0xee353944 of length 706232>
<GC: last free block is at end>
<GC: freed 332 objects, 10112 bytes in 31 ms, 85% free (715520/838856)>
<GC: init&scan: 1 ms, scan handles: 4 ms, sweep: 2 ms, compact: 24 ms>
</pre>


<pre>
[vps@druid]~/jprogs$ cat Test10.java
class Test10 {
String s1 = "The Tick!" ;
String s2 = new String("Arthur");
Test10() {
s1 = null;
s2 = null;
System.gc();
}
public static void main(String a[]) {
new Test10();
}
}
[vps@druid]~/jprogs$ java -verbosegc Test10
<GC: need to expand mark bits to cover 32768 bytes>
<GC: compactHeap took 7 ms, blocks_moved=0x885>
<GC: last free block at 0xee35394c of length 706224>
<GC: last free block is at end<
<GC: freeing class java.lang.Compiler>
<GC: unloaded and freed 1 class>
<GC: freed 333 objects, 10224 bytes in 22 ms, 85% free (715632/838856)>
<GC: init&scan: 1 ms, scan handles: 4 ms, sweep: 2 ms, compact: 15 ms>
[vps@druid]~/jprogs$
</pre>


The interssting question : what on earth are this 'objects' the collector found ? I would understand that if there were few of them , but 330 ??? That's too much...
More than that. Putting two garbage collector calls to the last code I got the following:
<pre>
[vps@druid]~/jprogs$ java -verbosegc Test10
<GC: need to expand mark bits to cover 32768 bytes>
<GC: compactHeap took 6 ms, blocks_moved=0x87e<
<GC: last free block at 0xee3538ec of length 706320>
<GC: last free block is at end>
<GC: freed 330 objects, 10192 bytes in 12 ms, 85% free (715600/838856)>
<GC: init&scan: 1 ms, scan handles: 4 ms, sweep: 1 ms, compact: 6 ms>
<GC: compactHeap took 18 ms, blocks_moved=0x712>
<GC: last free block at 0xee3518bc of length 714560>
<GC: last free block is at end>
<GC: freed 1 objects, 16 bytes in 48 ms, 85% free (714584/838856)>
<GC: init&scan: 0 ms, scan handles: 4 ms, sweep: 1 ms, compact: 43 ms>
</pre>
As you can see, the object was released. May be in the first time collector just didn't want to garbage the class of some reasons ? Time, for example... Anyway, looking through source code, I didn't find any conditions, when freeClasses() couldn't be called, so it is called every time gc() is called.

Anyway, there is only one class freed after gc (if there was at least one class freed), so I think, that an answer for you original question will be '1'

------------------
With best of best regards, Pawel S. Veselov ( aka Black Angel )

[This message has been edited by Pawel Veselov (edited September 07, 1999).]


With best of best regards, Pawel S. Veselov ( aka Black Angel )
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20542
    ∞

Fascinating!
This is the first I have seen of the -verbosegc parameter. Cool!
Best of all.... I love being right!
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Well, i guess that I'm stuck. Is gc collecting Strings that were created without the use of [new]?
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20542
    ∞

Nope.
Lookit this code.

Somewhere in your bytecode there will be an instance of a string object that says "goat" - otherwise, how would you specify what to make a new string out of?
In this case, "goat" is a string literal. It is stored in the string pool. I'm guessing that anything that is in the string pool is not gc'd.
Look at this code sample:

Since "goat" already exists in the string pool, s just points to it. When s is set to null, "goat" is not gc'd because it is part of the string pool. On the third line, s is assigned to the same instance of "goat".
Suppose that we treated "goat" like any other string and it was gc'd after the second line? How would the third line work?
I'm certain that string literals are not gc'd - but I do have some question about stuff in the string pool. Because you can make up a new string dynamically and move it into the string pool if you wanted to. Does that get gc'd? I dunno.

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Well, I am just starting (exam in 10 days), so this question may sound stupid:
Does printing to standard output create a string that's added to the String pool? If yes, and if gc doesn't care about String pool, shouldn't we run out of memory if we never unload VM?
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20542
    ∞

Nope.
The only stuff in the string pool is stuff that is a string literal (or stuff you specify is to be put in the string pool, but lets not go there now).
So with

You have one string object in the string pool. It will not be gc'd.
With

You have two string objects. One in the string pool and one in the regular heap. Both string objects contain the same thing. One will be gc'd and one won't.
Suppose you call a program passing the argument SPARKY and your progrm has one line:

There are four string objects.
One string object contains "arg is " and is in the string pool.
The second string object contains "." and is also in the string pool.
A third string object contains SPARKY". It is not in the string pool.
A fourth string object contains "arg is SPARKY." and is printed to STDOUT. After being printed, it is ready for gc.
I hope this helps.

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
<snip>You have two string objects. One in the string pool and one in the regular heap. Both
string objects contain the same thing. One will be gc'd and one won't.
What do you mean by this? That every String object will have a copy in the String pool? If that is the case, maybe the size of the pool is limited? I mean, when it reaches certain size, it starts writing on itself? Is that platform dependent? I tried to find something (not for very long) in documentation, no luck. Constant pool, heap, stack, my brain hurts! Nurse!
Tim Uckun
Ranch Hand

Joined: Jan 19, 1999
Posts: 88
Given all of the above it would seem that if you plan on setting a variable repeatedly to different strings it's better to use the construct.
s="This"
... some code ..
s="That"
Especially if keep setting the string over and over again. Is this right? Can you force gc on teh constant strings?


"There are some who call me TIM?"
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
We should get a life;^D
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20542
    ∞

(for the stuff I'm about to say, I'll assume that the intern() stuff doesn't exist)
The only stuff in the string pool are the string literals. That is anything appearing within quotes in your source code. Any strings that you create, or is created by something else, goes on the regular object heap and could be candidates for gc.
The moral of this story is that there are only a few strings in the string pool and they are determined at compile time. All other strings are treated like regular objects.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanks, I get it now. Someone should close this thread before it becomes silly.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: gc and the string pool