I was thinking about automated memory leak detection for a Java program. The basic algorithm is to create JUnits that contain the following logic:
Call System.gc() several times
Determine initial heap memory consumption using either Runtime class or JMX
Loop
Do something that exercises program under test
End loop
Call System.gc() several times
Determine final heap memory consumption
Compare initial and final memory numbers
The loop is being used to see if memory is creeping up in small increments.
It is necessary to distinguish between expected and unexpected increases in memory use.
This is not really a unit test. But the JUnit framework is convenient to use.
Do you think that this approach is valid? Do you think that this approach will be sucessful in identifying memory leaks? Have you ever done something like this?