When is Java gavage collector called?

Hello~ everyone.

One of our service has a function written with Java.
And some loop call this function many times.

Unfortunately that service sometimes crashed by memory leak.
So I reviewed the source code and found the memory leak point - already fixed it.
But I am not sure gabage collector activate after java function ended.
The Java function use so many variables with ‘new’ operator, but there were no place to clean up.

Please let me know when is gabage collector called?
After Java fuction is ended? or the service is ended?

PS. Thanks for your reading. this is my first time to ask a question with English.

Hi Ally,
There is no pre definied time or any definit point when GC (garbage collector) will run. Mainly when JVM feels that heap is running out of memory then it executes GC. You can also request JVM to run GC (System.gc())but there is no guarantee that GC will get executed.

Memory clean up in java is very complex and is managed by JVM. As vikas said there is no predefined time or moment when GC will run.

JVM schedule’s schedule garbage collection when the entire heap or a subpart of it either fills up or it reaches a threshold percentage of occupancy.

Also in most of the cases its not a good idea to run system.gc() as it does a full garbage collection and may cause a long pause in the processing.

A simple search with “garbage collection” on google or on wmuser may turn up many useful links.

Jiten

Thank you very much.

You mean that GC run automatically by JVM when heap is running out of memory and don’t recommand to call GC by manually.

I feel that is not reliable or I guessed wrong.
I said fixed memory leak. It was duplicated ‘new’ operation.
for example, class a{…} … a arry = new a[100], arry[0] = new a(), arry[1] = … and other place a[0] = new a(), a[1] = new a()…

I think that those codes caused memory leaks, and this will makes heap is running out of memory. → GC will activate and no crash.
But It was not. T^T

Anyway I will study for these more. I am just a begginer of Java and webMethods.
Thank you for your kindness.

You should never need to explicitly call System.GC() when using a JVM created in the last several years.

You might find this article informative on object finalization and cleanup.

Mark