0

I am comparing a Trie with a HashMap storing English words, over 1 million. After the data is loaded, only lookup is performed. I am writing code to test both speed and memory. The speed seems easy to measure, simply recording the system time before and after the testing code.

What's the way to measure the memory usage of an object? In this case, it's either a Trie and HashMap. I watched the system performance monitor and tested in Eclipse. The OS performance monitor shows over 1G memory is used after my testing program is launched. I doubt the fact that storing the data needs so much memory.

Also, on my Windows machine, it shows that memory usage keeps rising throughout the testing time. This shouldn't happen, since the initial loading time of the data is short. And after that, during the lookup phrase, there shouldn't be any more additional memory consumption, since no new objects are created. On linux, the memory usage seems more stable, though it also increased some.

Would you please share some thoughts on this? Thanks a lot.

user697911
  • 10,043
  • 25
  • 95
  • 169
  • Are you trying to measure [the size of your data structure in memory](http://stackoverflow.com/questions/52353/in-java-what-is-the-best-way-to-determine-the-size-of-an-object)? – 2rs2ts Mar 31 '14 at 19:00
  • 1
    Depending on your trie implementation, it's possible that it would take 1G for a million entries. But since you don't show any code, it would be impossible for anyone to give you an an analysis. Alternatively, you could take a heap dump and add together the pieces that belong to your trie. Instructions for getting a heap dump are [here](http://www.kdgregory.com/index.php?page=java.outOfMemory#heapDumps). – kdgregory Mar 31 '14 at 22:17

2 Answers2

0

The short answer is: you can't. The long answer is: you can calculate the size of objects in memory by repeating the differential memory analysis calling GC multiple times before and after the tests. But even then only a very large numbers or round can approximate the real size. You need a warmup phase first and even if it all seams to work just smoothly, you can get stuck with jit and other optimizations, you were not aware of.

In general it's a good rule of thumb to count the amount of objects you use.

If your tree implementation uses objects as structure representing the data, it is quite possible that your memory consumption is high, compared to a map.

If you have wast amount of data a map might become slow because of collisions.

A common approach is to optimize later in case optimization is needed.

Hannes
  • 2,018
  • 25
  • 32
-1

Did you try the "jps" tool which is provided by Oracle in Java SDK? You can find this in JavaSDK/bin folder. Its a great tool for performance checking and even memory usage.

Sri777
  • 530
  • 3
  • 9