JVM内存消耗是堆大小的两倍 [英] JVM memory consumption double of heapsize

查看:113
本文介绍了JVM内存消耗是堆大小的两倍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将64位Weblogic JVM的最大堆大小设置为4gb.当我对应用程序进行压力测试时,堆大小永远不会超过4Gb,但是taskmanager中的java.exe最多可能消耗10 Gb.这种消费来自哪里?

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?

推荐答案

可以通过java.lang.Runtime.getRuntime();在程序中获取程序的总已用/可用内存.

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));
  }
} 

这篇关于JVM内存消耗是堆大小的两倍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆