Author | Topic: Garbage Collection |
Rekha Suri greenhorn |
posted April 12, 2000 06:40 AM
Hi, Looking at the following code public class A When will the newly created instance of the class A be garbage collected ? It has no reference variable attached to it, so is it once the class executes ? And what about the member variable a? When will it be garbage collected ? Will it be GCed after the instance is GCed ?
|
SaiRam NageshKumar greenhorn |
posted April 13, 2000 03:11 AM
As per my view both the class A and variable a are GCed at the same time. Though the class hasn't any ref variable but has the instance which will be looked after by the JVM and does the GC part only after the execution. Pl look below the following code and execute you get the answer(I hope). If any corrections in my view let me clarify(any body). code: with wishes.
|
girish greenhorn |
posted April 15, 2000 06:00 AM
What I feel is newly created object here is local to the main() method. So it will be elligible for collection only after the scope goes out of this main method i.e after completion of main method.
|
maha anna bartender |
posted April 15, 2000 01:25 PM
girish, You are partly correct. In the sense, all method level scope references WILL be eligible for garbage collection after the method returns. This is true provided you don't keep the copy of the reference to another var which is still known to the program. For example assigning the local object's reference to a class/instance level var or making a copy and sending it as a return value of this method. Assuming we don't keep any extra reference to the local object, we CAN't say that all local objects in the method are eligible gor GC ONLY when the method returns. It is not true. You as a programmer can suggest for the GC that you don't need a particular object and the GC can collect that object's memory by setting all the references of the object to null. So assuming the method you called does some time consuming work and it doesn't come out at all, or it takes a longer time to finish, (Example painiting the user screen pixel by pixel ), does it mean you can't clean up the object's created inside the method at all before the method comes out ? No it is not true. Assuming the thread which run's the time-consuming work is not a selfish thread and it does allow other threads to run by means of yield() /sleep() etc, then there is a very good chance for the low priority GC thread and collects all the objects which are not any more referenced by this program. So you make use of this feature by setting all your unwanted objects to null immediately whenever you are done with your object inside the method itself. Which means if at all the GC does its work before the time consuming method comes out , the GC CAN collect all the objects without ref which are inside this method.
SaiRam, [This message has been edited by maha anna (edited April 15, 2000).]
|
Jim Yingst sheriff |
posted April 15, 2000 01:41 PM
One more thing you can do to help see what gc is doing if run java using the -verbose:gc option - e.g. java -verbose:gc ClassName . This tells you when gc runs and how much memory is freed, without having to use so many print statements.
|
Rekha Suri greenhorn |
posted April 17, 2000 06:50 AM
Thanks to all for your responses.
|
| | |