Vitaly
Vitaly

Reputation: 2662

Good practice to call System.gc()

I read many articles about bad practice to call System.gc().

I understand that no guaranty at all that JVM will react on this call.

And I know that System.gc() is pretty good indicator of fundamentally broken code.

But, if I have web backend server and I need to process many resources at server load stage. And after load memory is full of garbage.

And I know that my server will be used only in Ubuntu with Hotspot JDK1.8 and this JDK reacts on System.gc().

Is it bad to call System.gc() only once after load and before I open server for users?

Is there someone who does the same thing?

Upvotes: 4

Views: 2473

Answers (2)

user207421
user207421

Reputation: 310874

There is no need to call it at all. It isn't guaranteed that it will do anything, and it is guaranteed that GC will be performed before an OutOfMemoryException can be thrown. And if it does do something it may waste CPU time.

Upvotes: 4

Jerry101
Jerry101

Reputation: 13367

It's fine to call System.gc(). Do use a memory analysis tool to check that it's helping in your case.

The spec can't guarantee any results since the vm might've just done a GC.

Upvotes: 1

Related Questions