简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM