简体   繁体   中英

Java: Memory usage issue

I've set maxmimum memory this value

java -Xmx280m -jar ...

Code for memory usage information (on button click):

    int mb = 1024*1024;
    //Getting the runtime reference from system
    Runtime runtime = Runtime.getRuntime();
    System.out.println("##### Heap utilization statistics [MB] #####");
    //Print used memory
    System.out.println("Used Memory:"
        + (runtime.totalMemory() - runtime.freeMemory()) / mb);
    //Print free memory
    System.out.println("Free Memory:"
        + runtime.freeMemory() / mb);
    //Print total available memory
    System.out.println("Total Memory:" + runtime.totalMemory() / mb);
    //Print Maximum available memory
    System.out.println("Max Memory:" + runtime.maxMemory() / mb);

The program shows the following output (2 clicks+new tabs etc):

Used Memory:41
Free Memory:123
Total Memory:165
Max Memory:249
Used Memory:67
Free Memory:97
Total Memory:165
Max Memory:249

However, when I look at the Linux (Centos 7) System Monitor it shows 330mb for this java process. How to explain it?

The JVM memory consists of the following segments:

  • Heap Memory, which is the storage for Java objects
  • Non-Heap Memory, which is used by Java to store loaded classes and other meta-data
  • JVM code itself, JVM internal structures, loaded profiler agent code and data, etc.

Those parts can be configured with flags that passed during process startup:

JVM进程的内存结构

The 280 megabytes you specified is reserved for the Java program's heap. The VM and other things also use memory, so it'll never be the same number on the command line and in the process listing.

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