Search code examples
javamemorygarbage-collectionheap-memory

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?


Solution

  • 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.