aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collection 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 "Garbage Collection" Watch "Garbage Collection" New topic
Author

Garbage Collection

Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
As per K&B, as soon as getDate() method is completed, StringBuffer Object referenced by "sb"
is eligible for Garbage collection and "date" reference is not eligible for Garbage collection because it returns.
My Question is, How to make "date" reference to null.


OCPJP 6.0-81% | Preparing for OCWCD
http://www.certpal.com/blogs/cert-articles | http://sites.google.com/site/mostlyjava/scwcd |
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Make the Date variable d as null in the 6th or below lines, then there are no references to refer the Date object.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 440

[EDIT]
deleted this post as I had answered incorrectly due to misinterpreting the question fully
[/EDIT]

SCJP 6 [86%] June 30th, 2010
OCPWCD [84%] March 26th, 2012
OCEJPAD [83%] September 6th, 2014
If you find any post useful, click the "plus one" sign on the right
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Hi Abimaran, I am referring about "date" at Line 10 and you are referring "d" at Line 5.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Harikrishna Gorrepati wrote:Hi Abimaran, I am referring about "date" at Line 10 and you are referring "d" at Line 5.

Why do you need that? After completion of the getDate() method, the stack will be erased, so no need to explicitly make it null.
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
K &B says that "date" reference is not eligible for Garbage collection because it returns on Page 259.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Harikrishna Gorrepati wrote:K &B says that "date" reference is not eligible for Garbage collection because it returns on Page 259.

Garbage Collection can only be applied to objects not to reference variables. I think, the means the object which referred by the variable date.
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 440

Harikrishna Gorrepati wrote:K &B says that "date" reference is not eligible for Garbage collection because it returns on Page 259.

I am extremely sorry for misinterpreting the question completely
I did not notice that "date" variable is actually returned and asisgned to another variable

sorry
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
That's fine Prasad..No problem
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Harikrishna Gorrepati : Doubt cleared?
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
No Abimaran, I am referring Date object which has "date" object reference. When this will become null ? (or) When we can make it null ?
Abimaran Kugathasan wrote:
Harikrishna Gorrepati wrote:K &B says that "date" reference is not eligible for Garbage collection because it returns on Page 259.

Garbage Collection can only be applied to objects not to reference variables. I think, the means the object which referred by the variable date.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Making the variable which refers an object as null, makes the object eligible for GCed. What you are trying to achieve? To make the reference variable date as null or make the Date object which were referred by date GCed?
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Making the variable which refers an object as null, makes the object eligible for GCed.--> I agree with you
What you are trying to achieve? --> I want to nullify so that it will be GCed..Where can I do ? How can I do ?
To make the reference variable date as null or make the Date object which were referred by date GCed? --> Yes
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Harikrishna Gorrepati wrote:
What you are trying to achieve? --> I want to nullify so that it will be GCed..Where can I do ? How can I do ?

So, you ultimate goal is, making that Date object eligible for GCed? If so, you can refer my first reply to this post.
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
your first thread refers "d" at Line 5. I am referring about "date" at Line 10
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 440

date variable at line 10 will not exist after the method is called off the stack
so there is no question regarding making the variable null

the variable will not exist after the method completion at all
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
I thought the same but K& B says, "date" reference is not eligible for Garbage collection because it returns.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

The instance created at Line 10 will be referred by the Reference "d" at line 5. So the object created at line 10 will not be GCed as its still being referenced by a valid reference- "d". To make the Object created at line 10 to be eligible for GC, you shouldn't return it.

And to make the "date" reference to null- You need not do that as the reference variable goes out of scope once the method getDate() completes its execution- The date reference variable goes out of scope, but the Object it refers to is still referred by the reference-d.


Mohamed Sanaulla | My Blog
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

Harikrishna Gorrepati wrote:I thought the same but K& B says, "date" reference is not eligible for Garbage collection because it returns.


I dont think its date reference- It must be the Object referred by the date reference.
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 440

actually you are thinking a little different
I will make short and distinct statements here now
  • The date variable on line 10 will be not available after the method completion
  • But you are returning the object referred by reference variable date which is stored in variable d at line 5
  • so even if the method and the variable date is removed as it isnot on the stack, the object referred by date is still being referred by d on line 5
  • and the reference variable d will refer to the object returned through method
  • To solve this you don't return the object referred by date on line 10



  • Another thing I am considering is that you did not get the concept of reference variable and garbage collections
    here it is
  • Reference variable are NEVER eligible for the GC, the objects referred by them are eligible for GC


  • look the following code


    I hope this is clear now
    Harikrishna Gorrepati
    Ranch Hand

    Joined: Sep 23, 2010
    Posts: 422
    Yes. That's the right way to frame the sentence.
    Harikrishna Gorrepati
    Ranch Hand

    Joined: Sep 23, 2010
    Posts: 422
    Hi prasad, I got what you are saying. But my question is, How can we make Date object null referenced by "date" reference variable at line 10.
    Mohamed Sanaulla
    Saloon Keeper

    Joined: Sep 08, 2007
    Posts: 3071
        
      33

    Harikrishna Gorrepati wrote:Hi prasad, I got what you are saying. But my question is, How can we make Date object null referenced by "date" reference variable at line 10.



    There's nothing like making an object null- Or you meant Garbage Collected?
    Prasad Kharkar
    Ranch Hand

    Joined: Mar 07, 2010
    Posts: 440

    @ Harikrishna

    like Sanaullah has said, we cannot make an object refer to null
    we can just make the reference variable null which is referring to the object and then the object will be eligible for garbage collection

    hope this is clear to you now

    @ Sanauallah, can we be friends? I have been reading your posts for long now
    and is it ok to call you Sanaullah instead of Mohammad Sanauallah?
    Abimaran Kugathasan
    Ranch Hand

    Joined: Nov 04, 2009
    Posts: 2066

    Harikrishna Gorrepati wrote:But my question is, How can we make Date object null referenced by "date" reference variable at line 10.

    You have a wrong idea about GC. You can make an Object null, what you can do is, make the reference variable of an Object null, and by doing it, the Object which was referred by that variable is eligible for GC.
    Harikrishna Gorrepati
    Ranch Hand

    Joined: Sep 23, 2010
    Posts: 422
    @Mohammed, Prasad, Abimaran. I agree with all of you. I am sure I did not form the sentence in the right way. Let me put it like this. I want to make the Date object created at Line 10 to be Garbage Collected. To do that, we have to assign null to the object reference (Here the object reference is "date"). Because we are returning Date object which is referenced by "date" and assigning to "d" reference variable at Line 5, we are not able to nullify "date". How and where can we nullify the Date object referenced by "date" ? Is it possible to do that ?
    Prasad Kharkar
    Ranch Hand

    Joined: Mar 07, 2010
    Posts: 440

    then just write date = null in the method
    you will have one Date object eligible for garbage collection


    Reading of the posts above will surely help you understand this
    just go through them neatly
    your problem is solved already
    Harikrishna Gorrepati
    Ranch Hand

    Joined: Sep 23, 2010
    Posts: 422
    Hi Prasad, If I do that, I cannot return Date object referenced by "date" to Line 5 and will end up in assigning null to "d" at Line 5. Please let me know if I am going wrong.
    Abimaran Kugathasan
    Ranch Hand

    Joined: Nov 04, 2009
    Posts: 2066

    Why do you want to make the date reference variable null?
    Mohamed Sanaulla
    Saloon Keeper

    Joined: Sep 08, 2007
    Posts: 3071
        
      33

    So you want the object created in line 10 to be eligible for GC? After you return the reference to the Object created in line 10- date goes out of scope. Now d references the object created in line 10. So you would have to make d=null after line 6.
    Harikrishna Gorrepati
    Ranch Hand

    Joined: Sep 23, 2010
    Posts: 422
    @mohamed :

    So you want the object created in line 10 to be eligible for GC? --> Yes.
    make d=null after line 6 --> By doing so, we are assigning null to "d" reference only. But "date" object type is still pointed to Date object.

    @Abimaran

    Why do you want to make the date reference variable null? --> I do not know but trying to understand the concept.
    Prasad Kharkar
    Ranch Hand

    Joined: Mar 07, 2010
    Posts: 440

    we have already ansered the question very well
    please read above posts to make it clear
    Abimaran Kugathasan
    Ranch Hand

    Joined: Nov 04, 2009
    Posts: 2066

    This is what I can say,

    A Date object is created on the 10th line, and it's referred by the method local variable date. The object is returned to the caller on the 13th line, so the object is noe referred by another local variable d. After the completion of the method getDate() the stack used for the method is erased, so the local variable is no more there. But, since the object is on the heap and it's referred by another variable d in another method, you can't make the object eligible for GC. By making the variable d null, there is no more variables to refer the object. So, it's eligible for GC.

    Within the getDate() method, you can't make the object null, because, your method intention in to create a Date object and returned it to the caller. And further, you can't make the local variable date null, because, if you want to do that, you have to do it after the return statement. But, no statements can't be after a control statement[return, etc].
    Prasad Kharkar
    Ranch Hand

    Joined: Mar 07, 2010
    Posts: 440

    wel Abimaran's post is perfect now
    plus one to the post
    Abimaran Kugathasan
    Ranch Hand

    Joined: Nov 04, 2009
    Posts: 2066

    Prasad Kharkar wrote:wel Abimaran's post is perfect now
    plus one to the post

    That's what I can do.
    Harikrishna Gorrepati
    Ranch Hand

    Joined: Sep 23, 2010
    Posts: 422
    Thanks Abimaran. I am clear now.
    Abimaran Kugathasan wrote:This is what I can say,

    A Date object is created on the 10th line, and it's referred by the method local variable date. The object is returned to the caller on the 13th line, so the object is noe referred by another local variable d. After the completion of the method getDate() the stack used for the method is erased, so the local variable is no more there. But, since the object is on the heap and it's referred by another variable d in another method, you can't make the object eligible for GC. By making the variable d null, there is no more variables to refer the object. So, it's eligible for GC.

    Within the getDate() method, you can't make the object null, because, your method intention in to create a Date object and returned it to the caller. And further, you can't make the local variable date null, because, if you want to do that, you have to do it after the return statement. But, no statements can't be after a control statement[return, etc].
    Abimaran Kugathasan
    Ranch Hand

    Joined: Nov 04, 2009
    Posts: 2066

    Harikrishna Gorrepati wrote:Thanks Abimaran. I am clear now.

    You are Welcome, and happy to hear that my explanation helped you.

    Thanks!
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Garbage Collection