Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Garbage Collection time related to number of objects or size of objects?

I am having an issue with slow garbage collection. Obviously, the more objects in heap (all else equal) will lead to longer GC time, and larger sized objects in heap will lead to longer GC times.

However I am wondering about the trade-off between object size and number of objects. For example, comparing two sets of objects that are the same total megabytes but one set has 1,000,000 objects and the other has 5 objects, which is faster to GC?

like image 317
user2763361 Avatar asked Nov 28 '25 10:11

user2763361


2 Answers

It is related to number of objects and complexity and number of references. Basically, how long it takes to visit and mark everything that is "reachable".

This is because it does a mark and sweep where it will move from root(s) to tips and mark everything that can be reached. Everything else is just marked as free memory, regardless of size.

Overall GC Performance

As mentioned in the comments by @Nick Holt, Garbage Collection would occur more frequently with larger objects as they would fill the heap faster affecting GC performance. Clearing out a 100Gig object should still be faster than clearing out 100Gig of 10Meg objects however.

like image 181
Ross Drew Avatar answered Dec 01 '25 13:12

Ross Drew


Object size matters very little for GC performance, whereas object count matters a lot. Therefore you can expect better performance for larger objects, given a fixed memory footprint of those objects.

like image 37
Marko Topolnik Avatar answered Dec 01 '25 14:12

Marko Topolnik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!