Sagar
Sagar

Reputation: 5596

When does GC starts collecting garbage?

I am naive about Garbage Collector. Does Garbage collector waits until method exits, if heap is running out of memory during the current method execution ? OR it can kick-in at Milestone 1 (in other words in the middle of the current method execution) and free-up space occupied by keywords (in other words, space occupied by the local variable of current method).

public void myMethod() {

//Milestone 1
 List<Keyword> keywords = callSomeFunction();
 keywords = null;
 .
 .
 .
 .
 //Milestone 2

  .
  .
  .

// Milestone 3 - Exit here 

}

Upvotes: 2

Views: 2802

Answers (3)

Erick G. Hagstrom
Erick G. Hagstrom

Reputation: 4945

The short answer: the garbage collector will never collect space that is currently being used. So in your example, as long as keywords is in scope, whatever it references won't be collected. But when you assign it to null, the objects that it used to reference may be eligible for collection, if no one else is referencing them. That doesn't mean those objects will be collected, just that it might happen.

The long answer: First, you have to understand that there are multiple implementations of the Java Virtual Machine. There is no requirement in the Java 8 Virtual Machine Specification that a JVM even do garbage collection.

That said, garbage collection is a common feature in JVMs. The Java Language Specification 8 states, in Chapter 1:

The Java programming language ... includes automatic storage management, typically using a garbage collector, to avoid the safety problems of explicit deallocation (as in C's free or C++'s delete).

The JavaDoc for Java 8 documents the System.gc() method as follows:

Runs the garbage collector.

Calling the gc method suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects.

It then goes on to say that System.gc() is equivalent to Runtime.getRuntime().gc(). The JavaDoc for that method says:

Runs the garbage collector. Calling this method suggests that the Java virtual machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the virtual machine has made its best effort to recycle all discarded objects.

The name gc stands for "garbage collector". The virtual machine performs this recycling process automatically as needed, in a separate thread, even if the gc method is not invoked explicitly.

So, here are the takeaways:

  1. Garbage collection isn't required by the spec
  2. It's a common part of JVM's anyway
  3. You don't get any real control over it, you can only "suggest" that the JVM do some garbage collection
  4. Garbage collection often happens in its own thread, whenever the JVM wants it to happen, so it's totally unpredictable

Upvotes: 5

Peter Lawrey
Peter Lawrey

Reputation: 533492

The JVM adds savepoints to the code so that all the threads can be brought to a safepoint before performing a GC. These savepoints can happen between any byte code instruction, but the JVM often optimises these out to reduce overhead.

The GC can be triggered when the memory is running low (as CMS does) however the minor collection and parallel collection only runs when you either have run out of memory or you trigger it directly.

Upvotes: 2

Xirema
Xirema

Reputation: 20396

The Java Garbage Collector runs in the background, and it can fire off at basically any time. It's more likely to run when the program requests new memory, but there's no deterministic way to know when the GC will run.

Upvotes: 2

Related Questions