4

I knew that there are a lot of articles about java garbage collection but after searching I don't know exactly "when dose the garbage collection run in a java application?(when the application restart or while it still running)".

gamo
  • 1,549
  • 7
  • 24
  • 36
  • 1
    Short answer: this is beyond your control; never rely on it. There _are_ JVM options to control its behaviour somewhat, and those options are vendor dependent. – fge Nov 18 '14 at 07:05
  • @fge a garbage collector that would only work when the application starts would be quite useless. – JB Nizet Nov 18 '14 at 07:06
  • @fge I know it's beyond control. I just want to know when is the GC run in java and what is prerequisites? – gamo Nov 18 '14 at 07:15
  • It depends. See the [Available Collectors](https://docs.oracle.com/javase/8/docs/technotes/guides/vm/gctuning/collectors.html#sthref29) in the Oracle Java 8 implementation. – McDowell Nov 18 '14 at 09:02

4 Answers4

5

Garbage Collector is a dameon thread. A dameon thread runs behind the application. It is started by JVM. The thread stops when all non-dameon threads stop.

The JVM controls the Garbage Collector; it decides when to run the Garbage Collector. JVM runs the Garbage Collector when it realizes that the memory is running low. The behavior of GC can be tuned by passing parameters to JVM.

One can request the Garbage Collection to happen from within the java program but there is no guarantee that this request will be taken care of by jvm.Check How to force garbage collection in Java?

Learn More ..

Community
  • 1
  • 1
Santhosh
  • 8,181
  • 4
  • 29
  • 56
  • Thank you for a quick answer. As I understand. If I have an object `A` using a piece of memory and program done with `A` so `A` still waiting till memory running low to make that memory free, right? – gamo Nov 18 '14 at 07:27
  • @user3422401 When `A` becomes unreachable (also its references), it is GCed by the JVM.You need to understand GC is not a scheduled process that run on low memory states it runs with the JVM – Santhosh Nov 18 '14 at 07:33
  • 1
    @user3422401 What you wrote is sort of right, but not exact. At some point, the GC decides to run and collects *all surviving objects* from a memory area, which so becomes a free memory. Then your `A` is gone, but there's no explicit action done specially for `A` (unless there's a finalizer or another complication). Actually, GC should be better called "non-gargabe collector". – maaartinus Nov 18 '14 at 08:41
2

Garbage collection runs regularly alongside the program. It is part of the JVM.

When exactly does it run though? Well, that's unpredictable, version-dependent, and should be treated as if it could run at any time.

When a variable goes out of scope, the application tells the JVM it's done with that piece of memory. Then, when the garbage collector runs it will free those resources for the OS to use.

For example

String capitalizeAll(String s) {
    char[] chars = s.toCharArray();
    for(int i = 0; i < chars.length; i++)
        chars[i] -= 32;
    return new String(chars);
}

As soon as the method returns, the char[] chars allocated within the method will go out of scope. The program tells the JVM it's done with those resources and next time GC runs they will be freed.

Interesting stuff though, the JVM takes into account how much the app has told it is ready to be collected. That means that if your app does a lot of unnecessary copying or boxing, JVM will run often and cause your app to take a performance hit.

  • 3
    Bad example here. This method doesn't add anything to the heap that would need to be GCed. – JB Nizet Nov 18 '14 at 07:10
  • ah true, I'll fix it :) – Michael Goldstein Nov 18 '14 at 07:10
  • Thank you for your answer. Your example clears my mind a lot:) – gamo Nov 18 '14 at 07:33
  • @user3422401 no problem, glad to help. GC is an interesting subject in Java, one so interesting that the Java devs at Oracle and OpenJDK are constantly trying to improve GC because a better GC means many Java apps perform a lot better. – Michael Goldstein Nov 18 '14 at 07:37
  • No copy of String is made when calling the method. A copy of the reference is made. But this copy is not on the heap. what will be garbage collected in your example is the char array, because that is copied by the String constructor to guarantee the immutability of the returned String. – JB Nizet Nov 18 '14 at 08:07
  • @JBNizet haha thanks. I suppose I've learned a thing or two in addition to OP. I should brush up on allocations on stack vs heap. Primatives (unless boxed) and pointers on stack, Objects and arrays on heap, right? – Michael Goldstein Nov 18 '14 at 08:14
  • 1
    Local variables and arguments (whether primitives or references to objects) on stack, the rest (objects and their fields) on the heap. – JB Nizet Nov 18 '14 at 08:17
  • thanks @JBNizet I appreciate the help. Certainly I don't want to be one to propagate incorrect information. – Michael Goldstein Nov 18 '14 at 08:28
0

This is mostly implementation-specific.

The most primitive type of garbage collector, the serial GC will be triggered when it fails to find sufficient free space during an allocation (with generational GCs this usually means the young generation is full). It then suspends the entire JVM by triggering a safepoint on mutator threads and does its work on a single thread, this is called a "stop the world pause" In this case you can say that GCs can be caused by any allocation.

On top of that some GCs may also do background work concurrently to mutators, such as Hotspot's CMS. But it still needs stop the world pauses for some work, they just tend to be shorter than in the serial GC. For CMS it's still triggered by allocations but also does some of its work on background thread(s).

Azul Zing's concurrent compacting collector also does - as its name says - concurrent collecting on dedicated threads more or less all the time. It still needs cooperation from the mutator threads but does so without STW pauses. So this case one could say that the GC is running all the time in the background and does a little work in the foreground.

There are other collectors out there, so this isn't a comprehensive overview. But in general it's an implementation detail, subject to change and not something one should rely on. Some GCs even ignore System.gc() by default because it would mess up their heuristics.

the8472
  • 40,999
  • 5
  • 70
  • 122
0

This is from Kathy Sierra and Bert Bates' book SCJP Study Guide:

"The garbage collector is under the control of the JVM. The JVM decides when to run the garbage collector. From within your Java program, you can ask the JVM to run the garbage collector, but there are no guarantees, under any circumstances, that the JVM will comply. Left to its own devices, the JVM will typically run the garbage collector when it senses that memory is running low. Experience indicates that when your Java program makes a request for garbage collection, the JVM will usually grant your request in short order, but there are no guarantees. Just when you think you can count on it, the JVM will decide to ignore your request."

avs
  • 45
  • 1
  • 7