NoobCoder
NoobCoder

Reputation: 615

Is it possible to change the frequency of the garbage collector in Java?

Is there any way to change the frequency of the garbage collector, whether if to reduce it or increase it?

I found some articles that say that in order to increase the frequency, I need to increase the young generation to allow more objects to get into it before a GC is called.

But I didn't find anywhere a real way to do it, with real commands or actions or instructions HOW to make it happen (to reduce or to increase GC frequency).

Upvotes: 4

Views: 1817

Answers (1)

rzwitserloot
rzwitserloot

Reputation: 103189

You first configure which garbage collector you want to use, then you may be able to configure said garbage collector.

Whatever you read about it is irrelevant / invalid / misleading / vastly oversimplified. Garbage Collection is extremely complicated, specifically so complicated that talking about 'frequency' doesn't really make sense. 20 years ago garbage collection was simple:

  • Freeze every thread.
  • Make a list of all live objects.
  • Tree-walk these objects to find all objects reachable from those live objects, and keep walking.
  • Start from position 0 in the heap memory allocated to the JVM and start moving every object still reachable, updating all pointers as you go, and therefore silently overwriting all non-reachables.
  • Now you're done; memory is nicely compacted, lots of free space, unfreeze the world.

That model? It died 20 years ago. Garbage collection is vastly more complicated now, with aspects like:

  • Live tracking: Where the JVM uses heuristic mechanisms to be able to fast-collect a subset of garbage. (basically, reference counting; if the refcount is 0, it's definitely garbage. However, non-0 refcounts could also be garbage, for example if a refers to b, b refers to a, and nothing 'live' refers to either: Both refcounts are 1, but they're still garbage). These garbage collectors still collect them, just, not as quickly as refcount-0 garbage. What does 'frequency' mean now?

  • Generations, with vastly different approaches between generations. For example, your basic eden/'fast garbage' system works in reverse: A java thread gets a page worth of memory, new objects are created here, completely unreachable by any other thread. Once it is full, the system does a quick check on what this and only this thread can currently reach in context, makes a new page, copies over just the objects still reachable, and marks the old page as free. "Free garbage collection" just occurred. What the heck would 'frequency' mean here? There is nothing to configure: When the page is full, this process kicks in. Until the page is full, it doesn't. There's nothing to configure.

that's just 2 of like 50 things that garbage collectors do that cannot be described simply as a thing to which the term 'frequency' can be applied unambiguously.

Every JDK version sees pretty massive changes to the GC implementations available, and the way these implementations works, and even the settings these implementations support. None of it is part of the core java spec, which means that the OpenJDK team is far more cavalier about changing them between java releases, and for the same reason, alternate JDK providers like Azul, coretto etc often provide extra GC impls and extra settings.

So what do I do?

Stop worrying. The general rule of thumb is: If you mess with GC settings, you'll make everything worse. Get an expert if you need to tweak GC settings, and rest safe in the knowledge that it is highly unlikely you need it.

Forget about what you read. It's outdated information.

Upvotes: 4

Related Questions