views:

338

answers:

4

Java garbage collector runs with priority 1, due to which it is not guaranteed that System.gc() will actually execute if called.

Is there any way to change its priority? This shall enable me to run if I want.

+4  A: 

Even if you upped the thread priority to 11 System.gc will not guarantee anything. All you can be sure of is that if Java needs to GC it will before it throws an out of memory exception.

mlk
no It is not necessary. In spare time System.gc runs if you have called it.
DKSRathore
Because if that was the case all the unused objects will remain in the JVM for long time until OOM condition comes? On my system, which has 8GB JVM it never riches 7GB as used after reaching 6GB it comes to 5 GB and then 6GB when my program is in running state and so on. In your case it should have been reached 8GB before coming back to 5 GB
DKSRathore
I'm not sure what you mean by the above.Anyhoo - System.gc is not guaranteed. At all. I'm not sure even having a GC thread is required by the VM specs. Just that Java will give it a good go before throwing a OOM.One final point, System.gc on the Sun VM does (or did) kick of a full GC sweep, right then in the current thread. It does NOT have to do this. It could do nothing at all and still meet its requirements.
mlk
@DKSRathore: You seem to be confused about how the garbage collector works. The collections will always happen, whether you ever call `System.gc()` or not. 99% of the time, the garbage collector doesn't need your help to do its job.
Daniel Pryden
Looking at the memory usage from the OS preservative is not a valid measuring method in Java. Java (the Runtime) will keep hold of memory to reduce expensive calls to the OS.
mlk
That why GC is in Java? If you are not calling it, it will do your job for you. But if you need it you may call.
DKSRathore
@DKSRathore: You misunderstand. If you feel that the collector isn't freeing memory quickly enough, then there are much better ways to fix that than putting `System.gc()` in all over the place. In fact, since a forced full GC may promote objects to PermGen unnecessarily, you may actually be *wasting* memory by calling `System.gc()` unnecessarily. You might want to read up on this: http://java.sun.com/javase/technologies/hotspot/gc/index.jsp
Daniel Pryden
Thanks for the link Daniel.
DKSRathore
A: 

When a thread is created it inherits the priority of the thread that created it. The priority of a thread can be adjusted by using

public final void setPriority(int newPriority)

The problem is a thread doesn't "run" the garbage collector. The garbage collector is run by the VM (when Java wants or is in "good mood .. :)" ).

EDIT: The GC is not a thread but a method of the Runtime thread. You can change the priority of the Runtime thread but this will have no effect on the GC. The thread that calls the GC is a thread of the VM and is out side the scope of the API so you can't change its priority.

So, I don't really think you can set its priority.

Diego Dias
Pasting this from javadoc of gc()The name gc stands for "garbage collector". The virtual machine performs this recycling process automatically as needed, in a separate thread, even if the gc method is not invoked explicitly.
DKSRathore
But... the collector may use more than one thread, or no thread at all, depending on the JVM implementation. So this answer looks correct to me.
Daniel Pryden
+5  A: 

Garbage Collector is an independent thread (as reminded by Tom Hawtin in a comment, not even necessarily a single thread) and is on a priority which is decided by the Java Virtual Machine. This means you can't force garbage collection. Calling System.gc() tells the runtime environment that "now" might be a good time to run the GC but garbage collection is actually not guaranteed to be done immediately.

Pascal Thivent
Nice short fact.
BalusC
It's not even necessarily a single thread.
Tom Hawtin - tackline
Thanks for your satisfactory answer Pascal. You are just to the point.
DKSRathore
A: 

The GC will run as required. You shouldn't need to call it manually. If you don't like when it is run you can control it with command line arguments.

If you believe you have a problem with the behaviour of the GC you should try to fix the cause rather than trying to write your own work around.

In summary, you should tell us what is the real cause of your concern so we can address that.

Peter Lawrey