• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

garbage collection

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At what stage in the following method does the object initially referenced by s becomes available for garbage collection. Select the one correct answer.

void method X() {
String r = new String("abc");
String s = new String("abc");
r = r+1; //1
r = null; //2
s = s + r; //3
} //4


a) Before statement labeled 1
b) Before statement labeled 2
c) Before statement labeled 3
d) Before statement labeled 4
e) Never.
the ans says its d
but even if an object is created inside a method it is allocated space on the heap
so even if the method exits the object ref by out lives int the memory then how come it will be coolected before line 4
even if it is collected for gc after the method invocation it will be collected after line 4 because there actully the method dies
please claer this
QUESTION 2)
How can you ensure that the memory allocated by an object is freed. Select the one correct answer.
a) By invoking the free method on the object.
b) By calling system.gc() method.
c) By setting all references to the object to new values (say null).
d) Garbage collection cannot be forced. The programmer cannot force the compiler to free the memory used by an object.

the ans says d
but we can ensure that the object is gc ed by killing all its live rferences though not 100% sure about it
the ans d is the behavoiur of the gc in java
i think the question is wrongly worded
please correct me
 
Ranch Hand
Posts: 1246
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[B]At what stage in the following method does the object initially referenced by s becomes available for garbage collection. Select the one correct answer.

void method X() {
String r = new String("abc");
String s = new String("abc");
r = r+1; //1
r = null; //2
s = s + r; //3
} //4


a) Before statement labeled 1
b) Before statement labeled 2
c) Before statement labeled 3
d) Before statement labeled 4
e) Never.
the ans says its d
but even if an object is created inside a method it is allocated space on the heap
so even if the method exits the object ref by out lives int the memory then how come it will be coolected before line 4
even if it is collected for gc after the method invocation it will be collected after line 4 because there actully the method dies

"Available for garbage collection"!! It doesn't mean
the GC will actually collect it!.
QUESTION 2)
How can you ensure that the memory allocated by an object is freed. Select the one correct answer.
a) By invoking the free method on the object.
b) By calling system.gc() method.
c) By setting all references to the object to new values (say null).
d) Garbage collection cannot be forced. The programmer cannot force the compiler to free the memory used by an object.

the ans says d
but we can ensure that the object is gc ed by killing all its live rferences though not 100% sure about it

We can't ensure the object is GC. By killing all its live references is only make it available for garbage collection.
We can't ensure anything that the object is GC-ed.
We can only make it available for GC to collect.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic