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 ?
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.