This question has cropped up on several occasions in my current project. While I have no reason to disagree with the advice on heap sizing, I was a little uncomfortable that I hadn't seen much real evidence to back it up or indication of how bad things would be once the limit was reached, so I decided to find out for myself.
The first thing I tried was creating a simple Java class to stress the heap. This class will progressively populate an ArrayList with a large number of Java objects each owning five 80 byte random strings. It can also be asked to 'churn' the objects by selecting and replacing groups of them, thus making the old ones eligible for garbage collection. I ran this on a small Linux box and watched what happened using 'top' and 'vmstat' ...
What I found was this...
- While there was plenty of free memory, the resident size of the process grew.
- Once free memory became short, the shared size started to shrink
- Very soon after that, the swap file usage started to grow
- If the 'churn' feature of the stress test was enabled, the system quickly got into heavy swap thrashing and the stress test ground to a halt.
- With no churn (probably not realistic for most real apps), the app could get a little further, but not much and would still get into swap thrashing.
I drew two conclusions from my simple test:-
- The advice to keep the Java heap smaller than physical memory is very sound.
- The degradation in performance if you let your Java heap grow bigger than physical memory is both sudden and severe.
No comments:
Post a Comment