wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Explicit Garbage Collection invocation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Explicit Garbage Collection invocation" Watch "Explicit Garbage Collection invocation" New topic
Author

Explicit Garbage Collection invocation

Ram Reddy
Ranch Hand

Joined: Feb 20, 2007
Posts: 88
If we explicitly call Garbage collector , then it will reove memory for non-referenced objects Or it only removes once the managed memory is low(the memory which is used by running program)?
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638


If we explicitly call Garbage collector


Even if you call System.gc(), it is not guaranteed that the garbage collector will run, it will take its own sweet time to run. Call to System.gc() is just a request to the JVM to run the the garbage collector as soon as possible.
However, if the garbage collector runs, it will surely clean all the non-refernced memory from the JVM.


apigee, a better way to API!
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11476
    
  16

There is really no reason to ever invoke garbage collection. The JVM is much smarter than you (not "you" specifically, but "you" the whole world, just about). It knows when it needs to do garbage collection, and most of the time, it will just ignore your request anyway.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ram Reddy
Ranch Hand

Joined: Feb 20, 2007
Posts: 88
Thank you Fred and nitesh.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
I don't think that it is gauranteed garbage collector would always remove all non-referenced objects. I don't see any reason to have a explicit gc call. GC is smart enough to handle that.
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Hi Anupam, is there any specific reason as to why the garbage collector will not clean all the non-referenced objects that it finds while it is running?
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
Hi Anupam, is there any specific reason as to why the garbage collector will not clean all the non-referenced objects that it finds while it is running?

Do you ever remove plants from your garden which can be fruitful!


~Sanjeev Singh<br />SCJP 1.5
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Anupam,

Though we call gc call,JVM should realize that there is a need to do that;

we can create a situation like
1)see free space using Runtime class
2)try to occupy the entire free space by creating loop as,

for (int i=0;i<= ??? ;i++) \\ ?? depends on free space
Double d = new Double(225);


3) See free space again

4)then call gc.

5) Now see the free space

I am sure .. you will regain the free space.
[ March 20, 2007: Message edited by: Srinivasan thoyyeti ]

Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
This is surely not required to be known for the SCJP version atleast not till 1.5

First there are a number of garbage collector implementation that a JVM can support. Secondly JVM's generally would not actually collect all non referenced objects in one go, though of course it can. JVM would particularly look for objects with high infant mortality rate(objects that die soon after being allocated). You can and probably should read this for more info on Garbage collection.

Calling gc explicitly would cause the garbage collector to scan the entire memory and then sweep the non referenced objects. Which might not be required. So don't use it unless there's a specific reason to use it.

Here's a program which benchmarks the performance with constant gc calls and one with no explicit gc calls.



Even if you remove the GC from the loop and keep it outside the loop then also there would be a considerable difference.
[ March 20, 2007: Message edited by: Anupam Sinha ]
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Anupam,

Garbage collector is not meant for improving your system performance in terms of time but in terms of space, by deallocating unused memory occupied by Objects.

You should check for free space rather than time needed to execute statements.

[ March 20, 2007: Message edited by: Srinivasan thoyyeti ]
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Don't wanna argue but how come performance is function of free space?
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Anupam,

We should have perfect balance Time and Space to get performance.

Garbage collector helps us to get rid of performance problems caused by "lack of space".

if you have still doubt,
Please search in google with "Time Space trade off".
if you have read "Data Structures" before you can understand it easily.
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Originally posted by Sanjeev Singh:

Do you ever remove plants from your garden which can be fruitful!

Plants without roots need not be removed, they just fall down!
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Surely when I said gc shouldn't be called explicitly I did not mean that let the process die due to lack of memory. What I wanted to emphasize was that an explicit gc call is not required for and gc is generally smart enough to figure out when to gc.
JB Ramesh
Greenhorn

Joined: Jan 09, 2007
Posts: 20
explicit gc call is not required for and gc is generally smart enough to figure out when to gc


Absolutely correct . GC is smart enough. But explicit call to Systme.gc() always makes chance for GC to become more smart

Here is the code suggested by srinivasan



Here the explicit call System.gc() makes GC more smart
and gives us more free space. If we very much concern about memory free space, explicit GC call will make difference.
[ March 21, 2007: Message edited by: JB Ramesh ]
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Exactly my point for not using explicit gc. In JB's code snippet if we remove the call to gc and measure the time there would be a difference in explicit call invocation and if we remove the call. Secondly we are not making gc smarter by explicitly calling gc. In your simple program the code would generally complete much earlier if you remove the explicit calls. I mean what would you do with the free memory when you do not need it?
JB Ramesh
Greenhorn

Joined: Jan 09, 2007
Posts: 20
Secondly we are not making gc smarter by explicitly calling gc. In your simple program the code would generally complete much earlier if you remove the explicit calls.


Correct. you are concerning about the performance. But Garbage collection deals with JVM Memory.


I mean what would you do with the free memory when you do not need it?


Yes. when we dont need free space, we dont need to worry about GC. What about when you need it ? if we consider Data and memory, it makes difference. Consider about data structures and manupulation on data. If a program constructs huge number of objects and operates on that, then memory plays important role there. Ther we can optimize the program to address problem with free space and memory leake by using GC.

[ March 21, 2007: Message edited by: JB Ramesh ]
[ March 21, 2007: Message edited by: JB Ramesh ]
 
 
subject: Explicit Garbage Collection invocation