my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes GC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "GC" Watch "GC" New topic


fengqiao cao
Ranch Hand

Joined: Oct 26, 2001
Posts: 71
hi, there
as far as i know, if we creat object throught new, the automatic garbage collector will take care of cleaning up. How about java primitive value?
i came cross that finalize() will take care of them. I also know that before garbage collection take place, the finalize() always will be called. i am wondering who on earth will detetroy the object created without using new.
In the following code. will the object referenced by s garbage collected after setting it as null.( someone claimed that string literal will not be GC, is he or she right?)
String s="garbage collector"
please help!
thanks in adv....

Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
You don't have to worry about g.c. and primitive types. G.C. frees the memory ocupied by objects. If the object hold primitive variables they were placed in the image of the object in the heap. So they will be destroyied at the same time that the object. If the fields are static they are stored in the method area along the rest of the data for its class. If the class is ever unloaded so they are. Variables declared in a method are destroyied when the methods completes.
This code shows that String literals are not made eligable for g.c.

SCJP2. Please Indent your code using UBB Code
Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
Strings are a special case of objects. Strings may be created in two ways: either with the keyword new in which case they will be garbage collected or with a String literal like it is the case here and they will not be garbage collected.
This topic has been discussed an awful lots of times, please, make a search in this forum and look at what you get. I think it is useless to always and always repeat the same things again and again and again !!! Thank you !
The search engine works well and is very efficient
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform

[Blog] [Blogroll] [My Reviews] My Linked In
Peter den Haan
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Are primitive types garbage collected? Well, yes and no. Let's take an "int" and see what happens.
If our "int" is a member variable of a class, it's easy. An instance of the class will ultimately be garbage collected, and the "int" will go with it.
But what about local variables? Eg,These are not garbage collected. Instead, they are allocated on the stack. When the thread enters the method, the JVM allocates a stack frame with enough space for all local variables and some administrative stuff besides. This stack frame is deallocated the moment you leave the method (whether it's with a return, by throwing an exception or by encountering the closing brace). Memory management is directly tied to the foo() method block and completely automatic (they are called automatic variables in C/C++ parlance).
What about an object, then?You might wonder, is this automatically allocated and deallocated too? Well, no. There are actually two allocations here. First, a reference variable holding a reference to a Bar object is allocated on the stack. It will be deallocated along with the rest of the stack frame as soon as you leave the foo() method. Second, a Bar object which is allocated on the heap. A reference to this object is assigned to the "bar" variable. The object will ultimately be garbage collected.
Then your final question.String literals take a rather privileged position in Java. There is only ever one copy of each literal:These literals are not subject to garbage collection. They are part of a "string pool" that stays around as long as your application lives. You add new strings to the pool, or retrieve existing instances, using the intern() method:Hope this helps
- Peter
Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 761

A post by a bartender somewhere in the nether regions of javaranch also postulated:

"good" is a literal and can never be collected, but "goodmorning" can be collected.
(s += "morning" is equivalent to s = s + "morning", and since s isn't a constant, the expression s can be garbage collected.

I have question regarding the first line of the code sample above. e.g. String s = "foo";
Is anything created on the heap in this statement ?
[This message has been edited by Pho Tek (edited November 25, 2001).]


Peter den Haan
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
No, nothing is created on the heap. A reference to the string constant "good" is assigned to the local variable "s".
- Peter
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Hello Peter

No, nothing is created on the heap. A reference to the string constant "good" is assigned to the local variable "s".

But how is the reference assigned to a variable?
I have tried several examples and all of them decompiles in a bytecode of type "ldc # pool-entry-index".
According to Bill Venners' Inside The Java 2 Virtual Machine page 618 describing this bytecode:

Otherwise the entry is a CONSTANT_String_info entry, and the virtual machine pushes a reference to the interned String object that was produced by the process of resolving the entry onto the operand stack.

Thus I would say that the mere String s = "Hola"; produces an entry in the constant pool that gets resolved to the addres of the proper object. This is so because the addres must be assigned to a variable. In other examples the string literal could be used as a parameter to a method. Then the ldc bytecode pushes the reference
to the interned String object onto the operand stack just before calling the method.
What dou you think?
Don't get me started about those stupid light bulbs.
subject: GC
Similar Threads
Garbage Collection Problem
Question on Garbage Collection
Java Object's cleanup procedures
Resurrecting an Object thru Finalize() - in Action !
Garbage Collection