• 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
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

what is the trick

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anybody tell me the exact trick when exactly a
member variable can be garbage collected and when a automatic variable is garbage collected.
It would be great help to every body as one question on it is always there.
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Automatic variables can be gc'd when it goes out of scope. That is because that variable can no longer be used. A member variable and an automatic variable can also be gc'd when they no longer reference anything. If they are not looking at anything, then they are wasted space and the gc can come in a clean it up. Objects can also be gc'd when all references to them have been lost.
 
Amit Pasricha
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i want a rule , like when it is equated to null , etc, so that it doesn't take time in exam.
rest can you explain to me with example, reference to it is null
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Amit,
I will try to explain the garbage collection thing:
First of all, garbage collection is vendor specific & is run as the lowest priority thread by the JVM (which is again vendor specific).
Since its run as the lowest priority thread, one can't predict when gc will be run. It is normally run when there are no more threads ready for execution.
The JVM keeps track of all the objects that have references & all that don't. So, whenever the gc is run (can't say when it is run), it will check out all those objects having no references & clear the memory used up by these objects.
Secondly, one can explicitly set the reference of an object to null, as follows...
Assume s1, s2 to be member variables :
1. String s1 = "Java Forum";
2. String s2 = s1;
3. s1 = null;
4. s2 = null
In step 1, a String s1 is created & allocated some memory to store the literal "Java Forum".
In step 2, another String s2 is created & it points to the same location where s1 points to. Hence, we say s2 contains the value "Java Forum".
In step 3, the reference s1 is set to null, that means s1 now DOES NOT point to the string "Java Forum". However, the reference s2 is still pointing to "Java Forum" or any other string.
In step 4, the reference s2 is set to null, that means s2 now DOES NOT point to the string "Java Forum" or any other string.
Hence, the strings s1 & s2 can be gc'd. In fact, the memory location where "Java Forum" is stored does not contain any references, hence that memory is freed.
Second point to remember is automatic variables are gc'd when they go out of scope.
One more important thing to note is that if a thread is created in a function or say in the constructor of an object & the start method is called, the JVM notes that it is a live thread & the thread is not gc'd until its run method is over. (When a thread's run method is over, the thread becomes dead & can be gc'd).
I hope its clear now.
Anybody would like to add something to this ?
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Good notes Kavitha. !!
Sorry for being picky, but I would just like to comment on two of your statements -
First of all, garbage collection is vendor specific & is run as the lowest priority thread by the JVM (which is again vendor specific). Since its run as the lowest priority thread, one can't predict when gc will be run. It is normally run when there are no more threads ready for execution.
This is usually true, however because of the vendor dependency, it is better not to make any assumptions. There may be implementations of VM where GC is made a priority thread - VMs running on hardware where memory is very expensive, hand-held devices and PDAs for example. So let's just say GC is vendor specific and you cannot assume anything about the priority of the GC thread.

Second point to remember is automatic variables are gc'd when they go out of scope.
I would like to correct this statement and say, automatic variables become eligible for garbage collection when they go out of scope.

You can be assured you will not get very tricky questions about GC in the exam. The typical questions will be identifying when the object becomes eligible for garbage collection
Just my two cents worth ....
Ajith
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot kavita and Ajit,
Your comments have helped me a lot in clearing my doubts about the garbage collectioin mechanism of java.
Thanks again.
with regards
raghav..
 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"(When a thread's run method is over, the thread becomes dead & can be gc'd)."
I thought that when a threads run method is over, the thread object becomes dead, but cannot be gc'd. you can still access the Member variables of the thread through the Reference to the thread. The thread becomes eligible for gc when there is no active reference held for the thread, say by setting the thread to null. Correct me if I am wrong.
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good point mohit.
You are right. After the run method completes, the thread object is like any other object. Hence it too becomes eligible for GC only when there are no references pointing to it.
Ajith
 
Amit Pasricha
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks kavita, now i think the point is clear.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi ,
In the explanation given by kavita, I want to know that whether the string references s1 & s2 get garbage collected or the string object referenced by s1 & s2.
Please clear my doubt.
thanks,
mana
 
Kavita Bopardikar
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey, thanks a lot.
That certainly helps. Because so far in all the books I have read, its mentioned that GC is run as the lowest priority thread. But, I never thought that there could be scenarios where it may differ.
Yes, the other thing about being "eligible" for garbage collection is also true. An object need not be gc'd, but it could be ready for being gc'd.
Thanks for sharing your thoughts about this one.

Originally posted by Ajith Kallambella:

Good notes Kavitha. !!
Sorry for being picky, but I would just like to comment on two of your statements -
[b]First of all, garbage collection is vendor specific & is run as the lowest priority thread by the JVM (which is again vendor specific). Since its run as the lowest priority thread, one can't predict when gc will be run. It is normally run when there are no more threads ready for execution.

This is usually true, however because of the vendor dependency, it is better not to make any assumptions. There may be implementations of VM where GC is made a priority thread - VMs running on hardware where memory is very expensive, hand-held devices and PDAs for example. So let's just say GC is vendor specific and you cannot assume anything about the priority of the GC thread.

Second point to remember is automatic variables are gc'd when they go out of scope.
I would like to correct this statement and say, automatic variables become eligible for garbage collection when they go out of scope.

You can be assured you will not get very tricky questions about GC in the exam. The typical questions will be identifying when the object becomes eligible for garbage collection
Just my two cents worth ....
Ajith[/B]


 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kavita:
A simple question, string literals are GC'ed ?
I'd read in some place i don't remember now that string literals are not GC'ed
Am I missing something?
Anyone comments are welcome! Thanks!
Regds.
 
Amit Pasricha
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
THE FINALIZATION METHOD CAN MAKE THE OBJECT LIVE EVEN IF THE OBJECT IS ELIGIBLE FOR GARBAGE COLLECTION,MOREOVER THE FINALIZATION METHOD IS CALLED ON EVERY OBJECT WHICH IS TO BE GARBAGED COLLECTED.
NOW ARE'T THESE TWO POINTS CLASHING , PLEASE EXPLAIN
AMIT
 
Ranch Hand
Posts: 318
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It has been said that local variables become eligible for garbage collection when they go out of scope. This sort of implies that there aren't eligible before then, but what happens if they are set to null before they go out of scope. Would they then be eligible for gc BEFORE they went out of scope?

Matt
 
Leverager of our synergies
Posts: 10065
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody!
angel custodio:
I read in somebody�s post that String literals may be GC'ed when the whole class is unloaded. If you are interested, you may try to find this message. But for the exam you do not need to know such subtle things. I remember Bill Brodgen said that regarding GC you should threat String objects like any other objects and to pay no attention whether they are literals or not. By the way, on new exam I had a question on GC, but objects were not of String class.
Amit Pasricha:
this article about GC and finalization is very good: http://www.artima.com/designtechniques/cleanup.html
- it helped me a lot�
Matt DeLacey:
Would they then be eligible for gc BEFORE they went out of scope?
- Interesting question� I wrote a small research program to investigate�

The output on my computer is:

Apparently the answer for your question is �yes�.

[This message has been edited by Mapraputa Is (edited October 19, 2000).]
 
Kavita Bopardikar
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Matt,
See if this makes things clear :
1. A local variable becomes eligible for gc when it goes out of scope.
A local variable is alive only in the method in which it is declared. Hence, once the method is over, the local variable sor of "dies", means it goes out of scope. Once it goes out of scope, it is eligible for gc.
2. When a local variable is set to null, it means that it no longer has any references to it. So, it is eligible for being gc'd. If the gc routine runs at the time when the local variable has no reference, it is gc'd.
In short, a local variable is ready for gc whenever
a. it goes out of scope
or
b. all references to it are set to null
whichever is earlier.
Got it ???

Originally posted by Matt DeLacey:
It has been said that local variables become eligible for garbage collection when they go out of scope. This sort of implies that there aren't eligible before then, but what happens if they are set to null before they go out of scope. Would they then be eligible for gc BEFORE they went out of scope?
Matt


 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
mamta's question is still unaswered?
-------------------
hi ,
In the explanation given by kavita, I want to know that whether the string references s1 & s2 get garbage collected or the string object referenced by s1 & s2.
Please clear my doubt.
thanks,
mana
-------------------
Om
 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Originally posted by Om Sonie:
> mamta's question is still unaswered?
Om, only objects get garbage collected. References are never gc'd.
reply
    Bookmark Topic Watch Topic
  • New Topic