Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Measure the Memory taken by Method

 
Malatesh Karabisti
Ranch Hand
Posts: 153
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author
Bartender
Posts: 5852
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic