This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbase Collection Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbase Collection Help" Watch "Garbase Collection Help" New topic
Author

Garbase Collection Help

Fred Roger
Greenhorn

Joined: Nov 01, 2006
Posts: 19
How many objects are eligle for GC when System.gc () method is invoked ?

class MyClass{
public static finalize(){System.out.println("finalize method");}
}
class Test{
public static void Main(String [] args){
MyClass[] array = new MyClass[4];
for(int i =0i<array.length;i++)
array[i] = new MyClass();

f(array);
System.gc();
}
}

The choices:
A) 3
B) 4
C) 5

Please help guys.

Thanks.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9345
    
    2

Hi Roger,

First try to spot the flaws in your code. Your finalize method is wrong as it cannot be static and it also misses the return type. Second the call to f(array) is wrong as there is no matching method...Are you trying to say finalize(array)???if so where is the finalize method that takes array as argument??? My sense is that the whole program can be garbage collected....Just kidding...please give the correct code.

Regards,
Jothi Shankar Kumar. S


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Fred Roger
Greenhorn

Joined: Nov 01, 2006
Posts: 19
I am so soory guys for the typo in the code:

The correct is:

How many objects are eligle for GC when System.gc () method is invoked ?

class MyClass{
public void finalize(){System.out.println("finalize method");}

public static void f(MyClass array[]){

array[0] = null;
array[1] = null;
array[2] = null;
array = null;

}

}

class Test{
public static void Main(String [] args){
MyClass[] array = new MyClass[4];
for(int i =0;i<array.length;i++)
array[i] = new MyClass();

f(array);
System.gc();
}
}

The choices:
A) 3
B) 4
C) 5
Fred Roger
Greenhorn

Joined: Nov 01, 2006
Posts: 19
I am so soory guys for the typo in the code:

The correct is:

How many objects are eligle for GC when System.gc () method is invoked ?

class MyClass{
public void finalize(){System.out.println("finalize method");}

public static void f(MyClass array[]){

array[0] = null;
array[1] = null;
array[2] = null;
array = null;

}

}

class Test{
public static void Main(String [] args){
MyClass[] array = new MyClass[4];
for(int i =0;i<array.length;i++)
array[i] = new MyClass();

f(array);
System.gc();
}
}

The choices:
A) 3
B) 4
C) 5
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
3 that are objects in array[0], array[1], and array[2].
[ November 02, 2006: Message edited by: wise owen ]
Fred Roger
Greenhorn

Joined: Nov 01, 2006
Posts: 19
3 is correct as given in solutions.

But I dont understand one thing what about MyClass objects in array in main method.
Is it b'coz as the f() method is completed and thats why it is eligible for GC? But then what about array = null in the method ??
Can you explain how many total objects will be created and out that how many will be created.
Jae Stryker
Greenhorn

Joined: Oct 31, 2006
Posts: 21
I agree, three objects are up for the chop!
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi all,

Fred Roger posted November 02, 2006 08:41 AM

But then what about array = null in the method ??


The following example seems to have nothing to do with ours, but what will be the output?



Yours,
Bu.


all events occur in real time
Fred Roger
Greenhorn

Joined: Nov 01, 2006
Posts: 19
I am not sure,....may be 0 objects are available for GC. ...please correct me if I am worng
Naveen Zed
Ranch Hand

Joined: Aug 11, 2006
Posts: 61
I also agree with the answar '0' ojects are garbage collected.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi ranchers,

Fred and Naveen posted
0


It was not my attention to ask how many objects are eligible to GC.
I just wanted to push one of you, Fred, into the direction concerning his question, what will happen in line
array = null;
in your second posting.
Here's that again (bugs removed):

How many eli?
Three.

What happens to the array object in the main method of class Test?
Won't tell.


Yours,
Bu.
Loveen Jain
Ranch Hand

Joined: Jan 24, 2004
Posts: 35
Hi Bu,

According to my understanding. array will not be garbage collected because no matter you setted it to null when it was in the method f. when you come out of it it still holds three nulls, and an object array[3], which is still initialized.

Please tell me if I am right Bu? Because if i am right, then I have a clue for Fred that its something related to how an array is passed in the method as parameter, just think over it.

And I apologize in advance to Fred, if I am wrong.

So Bu please tell am i right or wrong?

Loveen...
Fred Roger
Greenhorn

Joined: Nov 01, 2006
Posts: 19
I think Array consits of 4 MyClass objects and we are setting 3 of them to null. Maybe that is why array = null doesnt make any sense b'coz there is one MyClass object there.

May be I am wrong. I dont know.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi Fred & Loveen,

yes, you nulled 3 out of four elements of the array. And the objects that stood behind this array elements now are no longer referenced. So no living thread can access them. And so they are garbage collected (or at least eligible for garbage collection).

The array itself is also an object.
You CAN change fields of an object (or null it), that is handed over as a parameter through a method. But you cannot directly null the object behind through this.

The individual elements of an array are like the the fields of any object. You can change or null this fields via the method, but not the object itself, here an example for an array:




prints:
before
[java.awt.Point[x=1,y=2], java.awt.Point[x=2,y=3]]

after deleteFirst()
[null, java.awt.Point[x=2,y=3]]

after noEffect()
[null, java.awt.Point[x=2,y=3]]

deleteFirst nulled the zeroth (0.) element of the array.

After nulling the reference in the noEffect method, the array is still there. The null in it is still from the deleteFirst method.



Fred said:
Maybe that is why array = null doesnt make any sense b'coz there is one MyClass object there.
As said above, you cannot null an object via a method parameter reference. Only the reference (anotherPointArray in Example2) will no longer point to pointArray from the main method, but it points to null then.

Not quite, as I said already. It makes no difference if the array is empty.

You can null the pointArray in the main method directly, even if it still contains elements. Then all of it's elements can no longer be referenced (reached) and will be eligible, as the array object itself.
Calling the asList method on it would then cause a null pointer exception.

If you'd just made an array of MyClass objects in main, fill it with MyClass object, and then null the array reference itself, all these elements will be eligible for GC. you can read the output from the finalize-method for them:
prints seven times: "method finalize" (but this is System dependent, as it is not guaranteed, that GC runs).


Yours,
Bu.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbase Collection Help
 
Similar Threads
Garbage collection
Qustion about Garbage Collection
garbage collection
Objects eligible for GC
Garbage Collection