aspose file tools*
The moose likes Beginning Java and the fly likes garbage collection question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "garbage collection question" Watch "garbage collection question" New topic
Author

garbage collection question

Sigfred Zamo
Ranch Hand

Joined: Mar 04, 2002
Posts: 44
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.


Sigfred Zamo<br />Software Development<p><a href="http://www.sigfredo.ingenieros.net/index2.html" target="_blank" rel="nofollow">My page</a>
Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
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


By failing to prepare, you are preparing to fail.<br />Benjamin Franklin (1706 - 1790)
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Sigfred Zamo
Ranch Hand

Joined: Mar 04, 2002
Posts: 44
Thank you both!
Sigfred Zamo
Ranch Hand

Joined: Mar 04, 2002
Posts: 44
To all of you!
Sigfred Zamo
Ranch Hand

Joined: Mar 04, 2002
Posts: 44
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"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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

Joined: Mar 04, 2002
Posts: 44
Thanks a lot, Cindy, your answers are very helpful for me.
David Duran
Ranch Hand

Joined: Feb 11, 2002
Posts: 122
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"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
It will run when the JVM decides that it wants it to run. You are just giving it a nudge in that direction.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
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.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
gautam shah
Ranch Hand

Joined: Oct 29, 2000
Posts: 72
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"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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

Joined: Oct 29, 2000
Posts: 72
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.
 
Consider Paul's rocket mass heater.
 
subject: garbage collection question