Skip to main content

Measuring the memory usage of a algorithm

3 replies [Last post]
mortennobel
Offline
Joined: 2004-11-03
Points: 0

Hi All

My question is, how can you measure how much memory a algorithm (implemented as a Java method) uses?

It's straightforward to measure the duration of this method call and also easy to measure and also easy to see how much memory is allocated before and after a method call. But what I want to measure, is how much memory is used during the method call.

I have written a library for scaling BufferedImages using resampling ( http://code.google.com/p/java-image-scaling/ ). This algorithm is uses a lot of heap memory (so you need to adjust your maximum heap to scale large images). I have tried a few things to improve the memory usage, but I find it really difficult to test if my changes has any effect.

- Morten

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
peter__lawrey
Offline
Joined: 2005-11-01
Points: 0

I suggest you use a profiler to profile how much memory your algorithim uses. It should also tell what the memory is used for and where it is created.

mortennobel
Offline
Joined: 2004-11-03
Points: 0

Actually I have tried to use YourKit 7.5 to profile my algorithm - But I find it hard to use the result. (Maybe there exist a better profiler for this task).

I would be cool if I could get a number or a graph, indicating the size of reachable objects (in bytes) in memory.

peter__lawrey
Offline
Joined: 2005-11-01
Points: 0

I use YourKit 7.5 for this and find it very useful.
You can take a snapshot of the application before and after you perform the algorithm for comparison. You can do this in your code (with an API which come with it) if you would prefer.
You may find all you need is to turn on memory allocation tracing while your algorithim is being performed.
I suggest you run the algorithim for at least a minute to get a reasonable large sample of data, otherwise you may just get "noise" in your results.