简体   繁体   中英

JVM memory consumption double of heapsize

I have set maximum heap size to 4gb for 64bit Weblogic JVM. When i stress-test app, heap size never goes over 4Gb, but java.exe in taskmanager can consume up to whopping 10 Gb. Where this consumption comes from?

The total used / free memory of an program can be obtained in the program via java.lang.Runtime.getRuntime();

The runtime has several method which relates to the memory. The following coding example demonstrate its usage.

import java.util.ArrayList;
import java.util.List;

public class PerformanceTest {
  private static final long MEGABYTE = 1024L * 1024L;

  public static long bytesToMegabytes(long bytes) {
    return bytes / MEGABYTE;
  }

  public static void main(String[] args) {
    // I assume you will know how to create a object Person yourself...
    List<Person> list = new ArrayList<Person>();
    for (int i = 0; i <= 100000; i++) {
      list.add(new Person("Jim", "Knopf"));
    }
    // Get the Java runtime
    Runtime runtime = Runtime.getRuntime();
    // Run the garbage collector
    runtime.gc();
    // Calculate the used memory
    long memory = runtime.totalMemory() - runtime.freeMemory();
    System.out.println("Used memory is bytes: " + memory);
    System.out.println("Used memory is megabytes: "
        + bytesToMegabytes(memory));
  }
} 

Well, the whopping can have lot's of causes an none of them is especially exciting. First, as said before, in "task manager", have a look at the detailed info for the process and make sure your looking at the working set and not memory commit size (virtual memory).

Then, below is a list (from the top of my head) of things in the JVM process that won't count as java heap, others might be able to add to the list(or perhaps subtract or elaborate):

  • Perm gen
  • Thread stack space
  • Shared code segments
  • Process private code segments
  • In process I/O buffers
  • JVM "data/code" (hotspot stuff, various temporaries, caches, etc)
  • Memory fragmentation in above mentioned data

But I agree with you, if you're actually looking at the working set, it sounds a bit whopping. We usually have 1-1.5 GB non java-heap memory usage on our 32-bit resin servers under high load (Debian Linux).

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