user697911
user697911

Reputation: 10531

Measuring Memory Usage in Linux

I am testing memory consumption of a block of code by using:

long totalMemory = Runtime.getRuntime().totalMemory();
long freeMemory = Runtime.getRuntime().freeMemory();

Used memory is the difference between totalMemory and freeMemory. I launched and run the same program three times with exactly same conditions. But the used memory amount differs a lot:

1120M
802M
312M

What might cause this? And should I take the average of the three as the memory usage of the code? First time doing this, and thanks for any insights on this.

Upvotes: 2

Views: 89

Answers (1)

codethilina
codethilina

Reputation: 11

This is couse due to java java vitual machine (JVM). Java use three types of memmory structures.

  1. Heap memmory
  2. Non heap memmory
  3. other memmory

So Garbage collector also run in JVM. That also effect to change memmory usage. You can reffer this link to more details.

http://www.yourkit.com/docs/kb/sizes.jsp

Upvotes: 1

Related Questions