How to Measure The Amount of Leaked Memory?

I am writing codes to measure approximate how much

memory is leaked when I call some methods. But I am

not sure which method I should use for that in Runtime

class.

My codes are like:

Runtime rtStart = Runtime.getRuntime();

rtStart.gc();

//total memory at starting time

long tmemStart = rtStart.totalMemory();

//free memory at starting time

long fmemStart = rtStart.freeMemory();

//used memory at starting time

long usedMemStart = tmemStart - fmemStart;

//call methods here do something

Runtime rtEnd = Runtime.getRuntime();

rtEnd.gc();

//total memory at end time

long tmemEnd = rtEnd.totalMemory();

//free memory at end time

long fmemEnd = rtStart.freeMemory();

//used memory at starting time

long usedMemEnd = tmemEnd - fmemEnd;

//total memory difference

long totalMem = tmemStart - tmemEnd;

//free memory difference

long freeMem = fmemStart - fmemEnd;

//used memory difference

long usedMem = usedMemEnd - usedMemStart;

Does anybody know which one is the correct answer for

the approximate amount of memory leakage or there is

another measure for it? Thanks in advance.

Cheryl

[1280 byte] By [cherylhu] at [2007-9-26 3:27:14]
# 1
You should go for usedMem - that's what matters here.
mamlason at 2007-6-29 11:49:48 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2
Just some suggestions:-It is cheaper and faster to buy one of the performance profiling tools. And it will provide a much more accurate result.-Or if you really want to roll your own you should look at the debugging interface.
jschell at 2007-6-29 11:49:48 > top of Java-index,Java HotSpot Virtual Machine,Specifications...