Rahul
Rahul

Reputation: 51

Is Java GC Deterministic

I am running multiple runs of the same scenario on a Java product with same JVM arguments. Every run gives a different GC behavior both in terms of its duration and its 'start time'. Is this expected?

Upvotes: 4

Views: 1787

Answers (3)

Aleš
Aleš

Reputation: 9028

Java Hotspot VM does not implement a deterministic GC algorithm.

In general, deterministic GC algorithms do exist for Java. For example in the following JVMs:

  • Metronome GC (IBM VM)
  • BEA JRockit
  • Azul's Pauseless Garbage Collector
  • FijiVM and its Schism: Fragmentation-Tolerant Real-Time Garbage Collection

Upvotes: 1

Matteo
Matteo

Reputation: 14930

The Java VM specifications do not specify how garbage collection should be implemented. So you cannot assume any deterministic behavior.

From: The Java® Virtual Machine Specification, Java SE 7 Edition: 2.5.3. Heap

The heap is created on virtual machine start-up. Heap storage for objects is reclaimed by an automatic storage management system (known as a garbage collector); objects are never explicitly deallocated. The Java Virtual Machine assumes no particular type of automatic storage management system, and the storage management technique may be chosen according to the implementor's system requirements. The heap may be of a fixed size or may be expanded as required by the computation and may be contracted if a larger heap becomes unnecessary. The memory for the heap does not need to be contiguous.

Summarizing: yes the behavior you are observing is normal and expectable.

Upvotes: 2

austin
austin

Reputation: 5866

Are you manually running System.gc()? Becuase that's not guaranteed to actually do the garbage collection immediately (or even at all).

For automatic garbage collection, I'd assume it's the same where the JVM determines a good time to do the garbage collection.

Upvotes: 2

Related Questions