Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why JVM is designed in a way that it does not allow force Garbage Collection? [closed]

As far as I know, we can't force for Garbage Collection in JAVA. The best we can do is to send a request by calling System.gc() or Runtime.gc(). Doing so will send request of Garbage collection to JVM but it’s not guaranteed that garbage collection will happen. So my question is: Are there any particular reasons, WHY JVM is designed in a way that it doesn't support Force Garbage Collection?

like image 463
Grainier Avatar asked Jun 07 '15 05:06

Grainier


People also ask

Can you force JVM to do garbage collection?

gc() Developers can call System. gc() anywhere in their code to instruct the JVM to prioritize garbage collection. When a developer calls this method -- and there isn't an extreme load on the JVM -- a Java GC cycle will happen within seconds.

What is the role of JVM in garbage collection?

Java garbage collection is the process by which Java programs perform automatic memory management. Java programs compile to bytecode that can be run on a Java Virtual Machine, or JVM for short. When Java programs run on the JVM, objects are created on the heap, which is a portion of memory dedicated to the program.

How JVM can destroy unreferenced object?

It makes java memory-efficient because the garbage collector removes the unreferenced objects from heap memory. It is automatically done by the garbage collector(a part of JVM), so we don't need extra effort.

How can we stop garbage collection in Java?

You can't force garbage collection in Java. There are some strategies you can use to get the Java virtual machine to prioritize the task, but the indeterminate nature of garbage collection means the process can't be forced. Similarly, you can't stop Java garbage collection from happening, either.


1 Answers

Forcing garbage collection is inefficient, and unnecessary in most cases1 ... if you have written your program correctly.

Reference:

  • Why is it bad practice to call System.gc()?

It turns out that if you control the launching of the JVM that will run your application (or applet or servlet or whatever) then you can ensure that calling System.gc() will run the GC. Or at least, that is the case with Sun / Oracle JVMs ... where the behaviour is controlled via a -XX option on the java command.

The point that the javadoc is making is that this is platform dependent, and a portable application can't rely on it.


As to your question:

WHY JVM is designed in a way that it doesn't support Force Garbage Collection?

So that the JVM can be protected against the performance impact of badly written code; e.g. in applets, plugins, 3rd-party libraries, etc.

(And I imagine, in part because the original Sun engineers got a bit fed up with people complaining that "Java is slow" when the real problem was unnecessary calls to System.gc() ...)


1 - But not always. For example, calling System.gc() at a convenient time can be a way to avoid a GC-related pause at an inconvenient time. However, if your code only works if you run the GC at certain points, then you are doing something wrong.

like image 178
Stephen C Avatar answered Nov 15 '22 19:11

Stephen C