Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 

}
like image 701
Sagar Avatar asked Jan 30 '26 12:01

Sagar


1 Answers

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
like image 123
Erick G. Hagstrom Avatar answered Feb 01 '26 03:02

Erick G. Hagstrom