• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Garbage collection

 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
I am preaparing for scjp exam .In one of the Mocks i found this question.Can anyone help me in explaining this question.

Q.Which statements about Garbage Collection are true?
1.You can directly free the memory allocated by an object.
2.You can directly run the garbage Collection whenever you want.
3.The Garbage Collector informs your object when it is about to be garbage Collected.
4.Garbage Collector runs in low memory situation.

The answer given is 3 &4.I dont understand the meaning of 3rd option and also i feel 2nd option is correct because we can run garbage collection by calling Sytem.gc() method.
Can anyone please help me in this
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SCJP questions go in the SCJP forum. I'll move this there for you.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by venkatesh pendharkar:
hi,
I am preaparing for scjp exam .In one of the Mocks i found this question.Can anyone help me in explaining this question.

Q.Which statements about Garbage Collection are true?
1.You can directly free the memory allocated by an object.
2.You can directly run the garbage Collection whenever you want.
3.The Garbage Collector informs your object when it is about to be garbage Collected.
4.Garbage Collector runs in low memory situation.

The answer given is 3 &4.I dont understand the meaning of 3rd option and also i feel 2nd option is correct because we can run garbage collection by calling Sytem.gc() method.
Can anyone please help me in this



You can call System.gc(), but that is not a guarantee that the garbage collector will run.

Before an object is garbage collected, its finalizer method will run (provided that the finalizer method has not been run already).
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
System.gc() is a suggestion to the JVM, so the statement you can directly run it is false.

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.html#gc()

The meaning of 3 is the object about to be garbage collected is somehow told this is about to happen. This notification is in the form of calling finalize() on it.

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Object.html#finalize()
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Originally posted by venkatesh pendharkar

Q.Which statements about Garbage Collection are true?
1.You can directly free the memory allocated by an object.
2.You can directly run the garbage Collection whenever you want.
3.The Garbage Collector informs your object when it is about to be garbage Collected.
4.Garbage Collector runs in low memory situation.


(2) is not correct as by calling the System.gc() or Runtime.getRuntime().gc(),you are just requesting the JVM to run the garbage collector.It is upto the JVM discretion to run the garbage collector.It may or may not run the GC at all.
(3) The kind of information it is talking about is to run the object finalize() method.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic