Skip to main content

Hey Guys, n00b here :)

7 replies [Last post]
blaland
Offline
Joined: 2008-05-21
Points: 0

Hey everyone!
I am currently working my way up the tutorials and learning Java from them and i have a small question.

I finished the section about classes and when i got to the exercises i came across this question:

Question 2: The following code creates one Point object and one Rectangle object. How many references to those objects exist after the code executes? Is either object eligible for garbage collection?

...
Point point = new Point(2,4);
Rectangle rectangle = new Rectangle(point, 20, 20);
point = null;
...

Now, according to what i read in the tutorial the "point" object suppose to be eligible for garbage collection since it was assigned to "null" but the answer says:


Answer 2: There is one reference to the Point object and one to the Rectangle object. Neither object is eligible for garbage collection.

Now i'm confused.. and what's more confusing is that the next question (and answer) says this:

Question: How does a program destroy an object that it creates?
Answer: A program does not explicitly destroy objects. A program can set all references to an object to null so that it becomes eligible for garbage collection. But the program does not actually destroy objects.

which is exactly what i thought... does anyone else see the contradiction here?

I'd love if someone would explain it better for me..

Thanks in advance!
Blaland

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
aberrant
Offline
Joined: 2006-02-02
Points: 0

Blaland,

In "Java The Platform" the garbage collector determines when an object will be deleted. The garbage collector is considered "outside" of the program you are writing in the "Java Language" because it's part of the virtual machine. So there is no way to write Java code to delete an object, only the garbage collector in the virtual machine can do that.

Does that help?

prunge
Offline
Joined: 2004-05-06
Points: 0

Hi Blaland,

The point object cannot be garbage collected because the rectangle has a reference to it, since you passed a reference to the point into the constructor of the rectangle.

Just picking you up on a technicality:

> Now, according to what i read in the tutorial the "point" object suppose to be eligible for
> garbage collection since it was assigned to "null" but the answers says...

The point [i]reference[/i] in your method was set to null. The Point [i]object[/i] lives on until it is garbage collected. An object is only eligible for garbage collection when there are no more direct or indirect references to it. In your case this is not true since the rectangle object holds a reference to the point and you still hold a reference to the rectangle.

Setting the rectangle reference to null will allow the garbage collector to then clean up both the Point and the Rectangle objects.

aberrant
Offline
Joined: 2006-02-02
Points: 0

@prunge ... Actually that might not be correct. At first I assumed the same thing as you, that the rectangle keeps a reference to the point object. Upon further investigation I noticed the following:

1. There is no rectangle constructor that takes a point, a height and a width.
2. The constructors that DO exist for point just extract the x and y coordinates.

public Rectangle(Point p, Dimension d) {
this(p.x, p.y, d.width, d.height);
}

This means that there is nothing holding on the the point object besides the declared reference. I believe the correct answer should be that there is one reference to the rectangle and 0 references to the point. The point object is eligible for garbage collection.

The intent of the question was probably to show that passing object around can create references that prevent it from being garbage collected. One possible way to show this would be to use an array or a collection (List, Set, etc..).

It looks like arrays are covered before the Classes and Object sections so this might be a better example;

String[] students = new String[10];
String studentName = "blaland";
students[0] = studentName;
studentName = null;

In this case there is one reference to the students array and that array has one reference to the string "blaland". Neither object is eligible for garbage collection.

By the way good question.

I posted a correction on the Wiki to bring attention to this issue.

http://wiki.java.net/bin/view/Projects/QuestionsAndExercisesObjectsQuest...

Message was edited by: aberrant

Message was edited by: aberrant

sfshaza
Offline
Joined: 2004-06-03
Points: 0

Good catch! I will fix the tutorial. Collin, do you mind if I use your example? We do try to avoid forward references.

Sharon

aberrant
Offline
Joined: 2006-02-02
Points: 0

@Sharon Thats fine with me. Please do. :)

sfshaza
Offline
Joined: 2004-06-03
Points: 0
aberrant
Offline
Joined: 2006-02-02
Points: 0

Cool, I'll remove the WIKI page about it.