Is it a good practice to call System.gc()
in all my Java methods?
Example: I have a class. Before the return statement of all the functions, i am trying to call the System.gc()
function. Is it correct?
Is it a good practice to call System.gc()
in all my Java methods?
Example: I have a class. Before the return statement of all the functions, i am trying to call the System.gc()
function. Is it correct?
No. There is barely ever a need to call System.gc()
, since garbage collection is automatic and the JVM knows what it's doing.
As manouti said, the method is simply a suggestion. At one time it was possible to "force" GC to run if you called System.gc()
several times in succession, but all in all it's not your job to worry about the garbage collector.
If you need to affect how the GC works, you can do that by providing command line arguments to select different collection algorithms and tune the parameters as explained here.
Calling System.gc()
from application code is a bad idea1. Calling it frequently is a TERRIBLE idea.
Running the GC is a relatively expensive operation, especially if it runs when it is not necessary to run it. And an application rarely has the information available to it to know when it is necessary.
The Java runtime has a much better handle on when and how to run the garbage collector efficiently. Leave it to make that decision for itself.
Reference:
Example: I have a class. Before the return statement of all the functions, i am trying to call the System.gc() function. Is it correct?
Absolutely not.
1 - There are one or two edge cases where calling System.gc() is justifiable in production code. However, these are a tiny exception.
No, it's not good practice, at least not in all methods. It may even cause performance overhead if the number of method calls is huge and more time gets allocated for GC (and depending on the type of the GC).
Furthermore, the behavior of System.gc()
is not predictable as implied by the docs:
Calling the
gc
method suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects.
It does not really matter of you call or do not call System.gc()
.
It is not guaranteed that the GC will kick in and it would most likely not run as the JVM is much smarter to determine where to run GC than a developer.
Is it a good practice to call System.gc() in all my Java methods?
You don't at all need to call System.gc() in your program. It's not a good practice. Even if you do that, it's not certain that jvm will run the garbage collector on your command.
Just try to avoid creating unnecessary objects in your class. Deference them if you don't need it. Check here How garbage collector works in Java.
Ensure that there are no memory leaks in your code.
Is there any need to call System.gc() explicitly..? If yes, where can i call..?