• Post Reply Bookmark Topic Watch Topic
  • New Topic

System.gc()  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically, I try to take a test and stuck with question about the garbage collector:
How many objects available for System.gc() in the following code snippet?



I think 2 objects are available:
1)arr – after clear(arr)
2)n2 – after n2=null

I don't sure that arr – is one object, may be arr is a series of objects. Also, I'm not sure about instances of StaticNestedClass, it is static and locates in a PermGen – not in the Heap as typical objects. Does GC works in PermGen as well as in the Heap?
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You must tell us where such questions come from, so we know there are no copyright problems.
Before going farther, let me tell you that the clear method is a red herring.
 
Sergey Chepurnov
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch

You must tell us where such questions come from, so we know there are no copyright problems.
Before going farther, let me tell you that the clear method is a red herring.


This question comes from a public vacancy of java developer as a filter of "bad" developers. I've realised that in the method clear() was created new link to the arr object, but in the main() method - link arr still references to the arr object. So, I think no one object will be available for GC. n1 saved in ArrayList, n2 saved in array StaticNestedClass[] arr.
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sergey Chepurnov wrote:
This question comes from a public vacancy of java developer as a filter of "bad" developers.


Huh?!?!?
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would have to go through the whole method with a pencil and paper and write down which reference points to which object.

I am not certain but I think that will confirm that your answer in the last post was correct
You need to confirm whether there are any array objects or List objects to delete from memory, too.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote: . . .
Huh?!?!?
I think it means it is an interview question.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!