• 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

Garbase Collection Help

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Fred Roger
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
3 that are objects in array[0], array[1], and array[2].
[ November 02, 2006: Message edited by: wise owen ]
 
Fred Roger
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree, three objects are up for the chop!
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Fred Roger
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not sure,....may be 0 objects are available for GC. ...please correct me if I am worng
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also agree with the answar '0' ojects are garbage collected.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Talk sense to a fool and he calls you foolish. -Euripides A foolish tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic