0

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.

gunr2171
  • 16,104
  • 25
  • 61
  • 88
DKSRathore
  • 3,043
  • 6
  • 27
  • 36

4 Answers4

7

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.

Community
  • 1
  • 1
Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
4

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.

Michael Lloyd Lee mlk
  • 14,561
  • 3
  • 44
  • 81
  • no It is not necessary. In spare time System.gc runs if you have called it. – DKSRathore Dec 09 '09 at 17:26
  • 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 Dec 09 '09 at 17:31
  • 2
    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. – Michael Lloyd Lee mlk Dec 09 '09 at 17:34
  • @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 Dec 09 '09 at 17:36
  • 2
    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. – Michael Lloyd Lee mlk Dec 09 '09 at 17:36
  • 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 Dec 09 '09 at 17:38
  • 5
    @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 Dec 09 '09 at 17:43
1

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
  • 525,659
  • 79
  • 751
  • 1,130
0

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
  • 21,634
  • 6
  • 33
  • 36
  • 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 Dec 09 '09 at 17:33
  • 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 Dec 09 '09 at 17:45