skip to main | skip to sidebar

Monday, July 18, 2011

JVM Heap Fragmentation

Recently I read about this topic and wanted to share it, I couldn’t recollect the source where I read it first to give a reference link, so just writing it here.
When JVM requires memory, Operating System always allocates continuous memory blocks. When objects get created and destroyed frequently, JVM allocates memory where ever available. Think of a situation where 1000 [each of size 5bytes] objects got allocated in some ‘x’ time span and only 500 of them are killed/de-allocated. Memory occupied by these 500 objects might not be continuous, the de-allocated memory might me scattered across heap memory. Each de-allocated block is of size 5bytes – so, you might be having free memory block of size 5bytes across heap, if JVM wants to allocate a 10byte memory block it cannot use these 5bytes blocks, it has to find a continuous 10byte free space in heap and allocate it. This situation is called as "Heap Fragmentation" [same as ‘Disk Fragmentation’], where the heap memory is fragmented. This results JVM not to release the continuous memory to Operating System.
Well, that is about the problem – how can we avoid it, here it is.
  • Identify places and avoid them where the objects are unnecessarily getting created and destroyed frequently.
  • Keep the maximum Heap memory Size setting to as close as possible to the maximum memory required by your JVM.
  • You can compact the Heap memory, using the JVM option "-compactGc", but remember that this option might result performance reduction.
That is all for now :)

No comments:

Post a Comment