Raj
Raj

Reputation: 440

Memory Profiler

Need to create our own Java profiler. The CPU profiling part was done with the help of Build your own profiler article from IBM. Now checking for similar kind for memory profiling also. On search found Hprof, but it was not useful to me. What i want is get the memory consumed in bytes by each method in a class

For eg:

consider a class test1 , class test2 etc are there in java file

method1() is present in test1

method2() is present in test2

many variables will be declared inside each methods So what i want is like

test1/method1 = 12 bytes (if possible display trace i.e how it came like this much number of char + number of integers etc)

test2/method2 = 18bytes

Please help

Upvotes: 2

Views: 503

Answers (1)

Joachim Sauer
Joachim Sauer

Reputation: 308001

If you're interested in how much memory a given method takes on the stack then there are two attributes in the class files that give information on this:

max_locals tells you how much space is reserved for local variables (in 32bit increments, i.e. multiply it by 4 to get the bytes).

max_stack tells you the largest the operand stack can grow to during execution of this method.

Adding those two will give you a rough estimate about how much the stack will grow if you invoke this method. There will be an additional (probably constant) overhead in addition to these values, but those should be the major variable factors.

You could use a Byte Code Manipulation Library such as BCEL to read the .class files and extract those attributes.

Upvotes: 1

Related Questions