Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected GC behaviour: some data always going into the tenure generation

Ran a very simple code to try to see how garbage collectors function.

String a = null;
while ( true ) {
  a = new String(" no... ");
}

I am using ParallelGC. I printed GC results and here is the first (minor) GCs.

[GC [PSYoungGen: 16448K->1616K(19136K)] 16448K->1624K(62848K), 0.0022134 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]

youngGen came down by 14880K however fullHeap came down by just 14872K

Does this mean 8k has moved to the tenure generation? My understanding is the GC might have been called some instanced of class 'a' must have been marked alive and moved to the tenure generation. Is this understanding correct? Also, is this "Floating Garbage"? Overtime the tenure generation does get filled up and a fullGC is required, however it does take awhile.

Also, in this particular case, shoudln't the entire minor collection get collected and ideally nothing goes into the tenure generation? All these are short lived objects.

like image 518
noi.m Avatar asked Jul 15 '13 06:07

noi.m


People also ask

Which generation contains objects that have reached the minor GC age threshold?

Tenured Space: The objects which reach to max tenured threshold during the minor GC or young GC, will be moved to “Tenured Space” or “Old Generation Space“.

How do I fix long garbage collection time?

High Object Creation Rate A high garbage collection rate will increase the GC pause time as well. Thus, optimizing the application to create fewer objects is THE EFFECTIVE strategy to reduce long GC pauses. This might be a time-consuming exercise, but it is 100% worth doing.

What causes high garbage collection?

The root cause of high-frequency garbage collection is object churn—many objects being created and disposed of in short order. Nearly all garbage collection strategies are well suited for such a scenario; they do their job well and are fast. However, this still consumes resources.

What is GC issue?

Incorrect or non-optimized garbage-collection configurations are the most common cause for GC-related performance problems. These are also the easiest to fix, since they do not require any code changes. Memory leaks, as you will see in the next chapter, are a very different story.


1 Answers

You've got 1 instance of String alive when the GC is in progress (the strong reference inside the while loop) so that one is the one that survives hence the 8k.

In this case I wouldn't call the String ref floating garbage. Floating garbage is when an object wasn't ready to be GCed when the GC checked it, but was ready by the time the GC finished. An example of that would be.

Thread1:    Person p = new Person("sammy")

    Thread2:    gc runs and sees that the Person instance is reachable through p.

Thread1:    p = null; // This Person instance is now unreachable.

    Thread2:    GC finishes. The person instance could have been collected but was reachable at the time the collector checked it.
like image 150
sksamuel Avatar answered Nov 15 '22 00:11

sksamuel