• 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 question

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi:
�Are 'point' and 'rectangle' objects eligible for garbage collection?
1....
2.Point point = new Point(2,4);
3.Rectangle rectangle = new Rectangle(point, 20, 20);
4.point = null;
5....
You can get Rectangle and Point class sources at http://java.sun.com/docs/books/tutorial/java/data/QandE/objects-questions.html (question 2).
The answer says that neither object is eligible for gc.
The point variable reference is null.
In the Rectangle class exists another Point variable that gets the same reference value that original point variable.
Because of exists two reference variables and only one is finally in null value, this object is not elegible for garbage collection, and it would be eligible for gc if the other reference variable gets a null value.
Is that correct?
Thanks.
 
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exactly. When thinking of your object point, think of this as a handle. This is not your actual object but just a handle do it. If another part of the program now creates its own handle to it, you now have 2 "handles" or references to it. With these "handles" still attached to the object point, the object can not drop.
-Dale
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that you have it correct - I am not quite sure from the way you worded it.
You have a variable named point that holds a reference to "object1" (which is an instance of the Point class). You also have a variable named rectangle that holds a reference to "object2" (which is an instance of the Rectangle class).
When you created object2 you passed a copy of the reference to object1, so somewhere inside of object2 is another variable that is holding that reference. If you look in the code for Rectangle you can see that that other variable is names "origin" and it now holds a reference to object1.
When you set the point variable to null - that got rid of ONE reference to object1 - but not the other one IN object2.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like you get it. If an object is not being referred to, then it's eligible for garbage collection.
If this article doesn't help to (un)clear things up:
Reference Objects and Garbage Collection
then Roedy Green's description of references surely will:
http://www.mindprod.com/jglossr.html#REFERENCE
Good Luck.
 
Sigfred Zamo
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you both!
 
Sigfred Zamo
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To all of you!
 
Sigfred Zamo
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello again:
1. ...
2. Point point = new Point(2,4);
3. Rectangle rectangle = new Rectangle(point, 20, 20);
4. point = null;
5. rectangle = null;
6. ...
Is now point eligible for gc?
Thanks.
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
point is a variable - it will NEVER be garbage collected the way that you are meaning. Only objects are garbage collected.
Both of the OBJECTS that were referenced by point and rectangle are now available for GC.
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
PS: of course this sample code lives inside some OTHER class (that is not named) and to actually have an occurance of point you would have to create an instance of the unnamed class.
When THAT instance is GC'd, of course any variables that live inside in are gone also.
Unless of course that this code is located in a Method, then - when the method is over, the variable is discarded and any objects that were references by those variables are potentially available for the gc.
 
Sigfred Zamo
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot, Cindy, your answers are very helpful for me.
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another Garbage Collection question:
When you call System.gc(), will the Garbage Collector run on the spot or...


it's not calling the garbage collection explicitly per se...
it's suggesting to the system to garbage collect. it doesn't do it "on
command". but i just sticks it in a "asap" queue... it's kinda weird.

as my friend likes to put it?
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It will run when the JVM decides that it wants it to run. You are just giving it a nudge in that direction.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sigfred Z,
Please read JavaRanch's naming policy and edit your profile to change your displayed name to conform. A single letter is not an acceptable last name. Thank you.
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Cindy Glass:
point is a variable - it will NEVER be garbage collected the way that you are meaning........



so what happened with point after the completion of that segment.???
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to make sure that you understand the difference between a variable that references an object and the object itself.
Objects are areas of memory on the heap that are used to keep track of the actual current values of the fields for a particular instance of the class. Objects to not have names. Objects get garbage collected when there are no variables that can find them anymore.
Variables are tools used by programmers to keep track of the objects that they create. They have names to make life easier for programmers. Variables live in the stack. Variables have a different life cycle than objects. Variables are not garbage collected they are destroyed by the JVM when they go out of scope. There are 3 kinds of variables - static variables , member variables(fields), and local, each with different levels of scope.
 
gautam shah
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Cindy Glass:

Objects are areas of memory on the heap


heap stack pointer queues references segment offset high memory low memory reserved memory data segment code segment blah blah blah.......
ooops

Originally posted by Cindy Glass:
You have to make sure that you understand the difference between a variable that references an object and the object itself.

Variables live in the stack.


i was in impression that there was a bird called "Symbol Table" which is being created at the time of compilation and will be used and updated throw out the life of the code according to the scopes.and used to keep variable names and their corresponding pointed locations their types etc.
i was thought that these concepts are in use every where and it doesn't matter that wheteher it is java or c or pascal or fortran. this may be true that java is written by alians and java compiler tooo.

Originally posted by Cindy Glass:

Variables are not garbage collected they are destroyed by the JVM when they go out of scope.


what is the difference between "garbage collection and destroying" both of these terms confused me a lot. great terminology.
probabely garbage collection would have meaning that what ever is garbage in the primary memory, just go and collect them for future uses and in case of so called "destroying" i think the portion of the "Stack" in which that variable declared get removed ("destroyed") from the "stack" i.e. primary memory

one more reading of PPL(principals of programming languages) is required for me now. ok let me start ........

this discussion remind me one of mine classmate
who was generally asking to our professor that how was the problum of external fregmentation in secondary memory get removed. u know what! he was in impresion that all of the clustures from the hard disk first get took outside from the disk and then rearrange one by one in contiguous such a way that all of the unused clustres comes in last . in this way disk becomes defragmented.
 
Warning! Way too comfortable! Do not sit! Try reading this tiny ad instead:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic