Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is calling `System.gc()` a good way to manage memory in a Java application?

I have a java application that wants to invoke the system.gc(). Is it a reasonable method to release memory? or any other advice? Really appreciate!

like image 958
newprospect Avatar asked Nov 30 '22 05:11

newprospect


2 Answers

Just stop referencing the variable. You don't need to invoke System#gc() yourself. If the JVM is on an edge of an OutOfMemoryError , it will certainly run the GC.

If stopping referencing the variables is not an option because you really need them, then you need to profile your application to fix/clean any memory leaks and/or just give JVM more memory at startup.

like image 75
BalusC Avatar answered Dec 09 '22 18:12

BalusC


In Java it is not necessary to explicitly invoke garbage collection. It is done automatically by the virtual machine.

like image 40
Bernard Avatar answered Dec 09 '22 18:12

Bernard