• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Garbage Collection

 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Make the Date variable d as null in the 6th or below lines, then there are no references to refer the Date object.
 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[EDIT]
deleted this post as I had answered incorrectly due to misinterpreting the question fully
[/EDIT]
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Abimaran, I am referring about "date" at Line 10 and you are referring "d" at Line 5.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
K &B says that "date" reference is not eligible for Garbage collection because it returns on Page 259.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's fine Prasad..No problem
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Harikrishna Gorrepati : Doubt cleared?
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
your first thread refers "d" at Line 5. I am referring about "date" at Line 10
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought the same but K& B says, "date" reference is not eligible for Garbage collection because it returns.
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
    Posts: 423
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Yes. That's the right way to frame the sentence.
     
    Harikrishna Gorrepati
    Ranch Hand
    Posts: 423
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Bartender
    Posts: 3225
    34
    IntelliJ IDE Oracle Spring Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 446
    1
    Eclipse IDE MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    @ 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
    Posts: 2066
    IntelliJ IDE Clojure Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 423
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    @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
    Posts: 446
    1
    Eclipse IDE MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 423
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 2066
    IntelliJ IDE Clojure Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Why do you want to make the date reference variable null?
     
    Mohamed Sanaulla
    Bartender
    Posts: 3225
    34
    IntelliJ IDE Oracle Spring Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 423
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    @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
    Posts: 446
    1
    Eclipse IDE MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    we have already ansered the question very well
    please read above posts to make it clear
     
    Abimaran Kugathasan
    Ranch Hand
    Posts: 2066
    IntelliJ IDE Clojure Linux
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 446
    1
    Eclipse IDE MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    wel Abimaran's post is perfect now
    plus one to the post
     
    Abimaran Kugathasan
    Ranch Hand
    Posts: 2066
    IntelliJ IDE Clojure Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 423
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 2066
    IntelliJ IDE Clojure Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

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


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

    Thanks!
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic