File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes IDEs, Version Control and other tools and the fly likes Measure the Memory taken by Method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » IDEs, Version Control and other tools
Bookmark "Measure the Memory taken by Method" Watch "Measure the Memory taken by Method" New topic

Measure the Memory taken by Method

Malatesh Karabisti
Ranch Hand

Joined: Jul 28, 2010
Posts: 153

I have web service method which takes more than 10 min to execute. I want to monitory the memory used in the method. and want to measure exactly how much memory it has occupied. I tried with below code I am not getting the exact figures since garbage collection may have taken place in span of 10 min

Can any body Please help me out How memory can be measured when method execution takes longer time ?
Peter Johnson

Joined: May 14, 2008
Posts: 5852

GC is almost guaranteed to have take place many time in 10 minutes! As far as I know, there is nothing that will tell you the number of bytes or objects that have been allocated within a given period of time, whether by the whole JVM or by a single thread. I would try one of two things:

a) Visually step through the code noting the objects created and estimating their sizes. Given that the code runs for 10 minutes, this might not be practical. Also, if you are using any kind of framework, this task might be impossible.

b) Turn on one of the GC tracing options, -verbose:gc might be good enough. Select a fairly small young generation size, say 5-10MB. Start the app. If it is a Java EE app, make sure noone else is on the system. Then make a request that causes the method to run. You should print an "about to call method" and a "just now returned from the method" around the method call (do NOT print these inside the method, they must be done outside the method). Also, call System.gc() before printing the "about to call method" and before "just now returned from method". Example:

Observe all of the GCs that take place between the two printlns and calculate the amount of heap space freed by those GCs. That should give you a fairly good idea of how much heap space the method required.

JBoss In Action
I agree. Here's the link:
subject: Measure the Memory taken by Method
It's not a secret anymore!