14

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?

Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
Grainier
  • 1,634
  • 2
  • 17
  • 30
  • 1
    why should it? How is it useful to force a garbage collection? – Henry Jun 07 '15 at 05:33
  • 6
    A *particular* JVM implementation *might* choose to guarantee such would perform a GC. The Oracle/Open JDKs do not. There question here is then: "For what *implementation reason* does `gc` not immediately force a stopping-GC in the standard JVM?" – user2864740 Jun 07 '15 at 05:39
  • Assuming you want to avoid GC pauses at inappropriate times, you could probably counteract them by using a low-pause garbage collector (like CMS or G1) and having object pools for continuously needed types of objects. That should reduce _stop-the-world_ events to a minimum, as object pools should counteract heap fragmentation, which is the achilles heel for CMS and G1. – mucaho Jun 07 '15 at 10:37
  • Your premise is flawed: there is nothing in the JVM specification that prevents an implementor from choosing to implement `System.gc()` as a forced collection. There is also nothing in the design of any of the popular JVMs that makes it impossible. They just choose not do it. Although you may know more about your program than the JVM does, the JVM knows more about the rest of the system than you do. (After all, one of the selling points of Java is that when you write your program, you don't even know what JVM, what OS, what processor it is going to run on.) … – Jörg W Mittag Jun 07 '15 at 13:22
  • … So, it makes sense to treat `System.gc()` as a signal to the JVM "I know that my app could benefit from a GC at this point in time" and the JVM saying "OK, let's see what I can do, but I know better than you whether a GC right now makes sense for the overall system". – Jörg W Mittag Jun 07 '15 at 13:22

2 Answers2

17

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

Reference:

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.

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
3

Java is designed so that (unlike in C++) you do not have to concern yourself with reclaiming disused memory. If the runtime memory is configured correctly, you should not need to concern yourself with garbage collection. Having said that, doing a gc() will generally trigger a sweep of the young generation space.

  • 1
    Gosh, and here I thought that the garbage collectors that have been implemented for C++ actually worked! That aside, garbage collection does **not** mean that you can ignore memory management. It's easy enough to create [memory leaks in Java](http://stackoverflow.com/questions/6470651/creating-a-memory-leak-with-java). – Pete Becker Jun 07 '15 at 12:01
  • @PeteBecker - Are you referring to conservative C++ collectors like the Boehm GC, smart pointers, or something else? – Stephen C Jun 09 '15 at 13:55
  • @StephenC - smart pointers are a form of automatic memory management, but they are not garbage collection. – Pete Becker Jun 09 '15 at 15:48
  • @PeteBecker - I know that ... but I'm trying to get you to tell me what kind of C++ harbage collector >>you<< are talking about. – Stephen C Jun 09 '15 at 17:10