Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java -> System.gc(); Does this call opens a new Thread or not?

For example I such a code

... get some memory and lose all the pointers to that memory so that System.gc(); can collect it.

call System.gc();

do some other tasks;


Here does the "do some other tasks;" and "System.gc();" works in paralel or does the "do some other tasks;" waits for "System.gc();" to be executed

Thank you

like image 339
ogzylz Avatar asked Apr 19 '10 12:04

ogzylz


4 Answers

You probably shouldn't be using System.gc(). A common misconception with C/C++ users coming over to java is they think they need to tell the virtual machine when it can perform garbage collection. The reality is that the garbage collector is highly optimized and performs this task by itself when it feels it is best to do so. Calling System.gc() is not usually recommended.

If you do call System.gc() though, it will 'recommend' to the system to perform garbage collection. It may not actually perform a collection though it usually does. If it runs in another thread depends on the actual collection algorithm. The default one will block everything while it runs. So it may run in separate threads but it will block your current execution when it does.

like image 168
Chris Dail Avatar answered Nov 15 '22 01:11

Chris Dail


The exact behavior here depends entirely on the JVM implementation. In spec (that is what a proper JVM implementation needs to provide you), it can happen in parallel, it can happen first before your code executes, or it can not happen at all.

In practice, my observation on JVMs I have happened to observe is that it runs immediately in a separate thread. However, in some cases multiple calls spawn multiple threads, sometimes it queues the requests on one thread. The Garbage collection launched was always a "stop-the-world" type (that is it was very complete and slowed or paused the application).

However, given your comment to @Chris Dail, your underlying problem isn't the behavior of a System.gc() call. Calling System.gc() can have some uses. It can be used clear memory so you can get a sense of how large the footprint of the application actually is currently. It can also be used as a strategy to ensure that a stop-the-world garbage collection happens earlier so that it "stops the world" for a shorter amount of time because there is less memory to clear. (I should note that as JVM's get more and more sophisticated this kind of thing is less and less necessary, and actually becomes counter-productive).

What it does not do however, is in any way solve an OutOfMemoryError. The JVM will not give you an OutOfMemoryError until it has garbage collected to the best of its ability. Calling System.gc does not change that. If you have an OutOfMemoryError it is likely because you are holding reference to objects in ways that you don't really need, but that are preventing those Objects memory from being reclaimed.

like image 29
Yishai Avatar answered Nov 15 '22 01:11

Yishai


It's usually synchronous, but not guaranteed.

Actually is neither guaranteed that a collection will effectively take place since the JVM will decide if it makes sense and what kind of garbage collection use.

If you need additional info you can launch your program with -verbosegc flag..

In any case the garbage collection is something that it issued automatically by the JVM without any specified calls, invoking System.gc() is just a hint you give to let it know that it may start a collection.

like image 45
Jack Avatar answered Nov 15 '22 01:11

Jack


As Eric Eijkelenboom pointed out, calling this method does not mean the the Garbage Collector will execute immediately. Actually you don't know if it will be called at all...

In your case (judging from the comment on Amarghosh's answer) you need to free up some memory before you do something that requires a lot of RAM am i right? In that case you don't need to worry. If there is enough memory to be garbage collected it will be done automatically when you try to allocate it.

like image 44
Savvas Dalkitsis Avatar answered Nov 15 '22 01:11

Savvas Dalkitsis