• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Garbage Collection

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Looking at the following code
public class A
{
int a ;
public static void main(String args[])
{
System.out.println(new A().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 ?
Any clarifications will be very helpful.
Thanks .

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).


with wishes.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Rekha,
In your code the object created inside the println(..) statement is eligible for GC as soon as the println is over.(Assuming the println(..) doesn't keep another reference to this object. I will check the source code of println(..) ). The reason I had explained in the above paragraph. Please go through it. The simple reason is there is no more reference to it. The JLS says only this. Whenever a object loses all its reference , it is eligible for GC. That's all. JLS doesn't force the mechanism of the GC . It differs from JVM to JVM. The qstn here is when is an object WILL BE ELIGIBLE for GC if at all GC runs. I did some research using SaiRam's code under JDK 1.2.2 in win98. It turns out that when a object loses all its ref it is eligible for GC. So both the anononymous object and the var inside this object(a) are eligible gor GC.
SaiRam,
I played well with your code. Here is the benchMark of the results.
1. If a object is created inside a method and if it does not have any ref it is eligible for GC. As you did you can suggest the GC to do the clean up by saying Runtime.getRunTime.gc(). IF it honours our request the memory WILL BE released before the method returns.
2. Before a method returns, when you keep the reference to all obects created inside a method (not setting them to null) , and call Runtime.getRuntime.gc(), it doesn't have any effect. The object's memory will not be released.
3. When you increase the no. of objects created, inside the method, more memory will be hogged by the system.
4. GC doesn't resolve the circular refs. (i.e) If a obejct is set to null and if it has another instance var as a reference to another object, the referenced object is eligible for GC in the next RUN of GC.
regds
maha anna

[This message has been edited by maha anna (edited April 15, 2000).]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more thing you can do to help see what gc is doing if run java using the -verbose:gc option - e.g. <code>java -verbose:gc ClassName</code>. This tells you when gc runs and how much memory is freed, without having to use so many print statements.
 
Rekha Suri
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all for your responses.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic