确定我的应用程序使用的内存量

时间:2013-04-12 09:05:47

标签: java memory ram

我编写了一个java程序,我想知道它运行多少RAM。有没有办法看看我的程序有多少RAM使用?我的意思是类似程序的时间使用,可以通过在调用主代码之前和之后编写此代码来看到:

long startTime = System.currentTimeMillis();
new Main();
long endTime = System.currentTimeMillis();
System.out.println("Total Time: " + (endTime - startTime));

4 个答案:

答案 0 :(得分:1)

您可以使用以下课程。实现Instantiator接口,您可以执行多次相同的过程以获得内存消耗的精确视图

public class SizeOfUtil {

    private static final Runtime runtime = Runtime.getRuntime();
    private static final int OBJECT_COUNT = 100000;

    /**
     * Return the size of an object instantiated using the instantiator
     * 
     * @param instantiator
     * @return byte size of the instantiate object
     */      
    static public int execute(Instantiator instantiator) {
        runGarbageCollection();
        usedMemory();
        Object[] objects = new Object[OBJECT_COUNT + 1];
        long heapSize = 0;
        for (int i = 0; i < OBJECT_COUNT + 1; ++i) {
            Object object = instantiator.execute();
            if (i > 0)
                objects[i] = object;
            else {
                object = null;
                runGarbageCollection();
                heapSize = usedMemory();
            }
        }
        runGarbageCollection();
        long heap2 = usedMemory(); // Take an after heap snapshot:
        final int size = Math.round(((float) (heap2 - heapSize)) / OBJECT_COUNT);

        for (int i = 1; i < OBJECT_COUNT + 1; ++i)
            objects[i] = null;
        objects = null;
        return size;
    }

    private static void runGarbageCollection() {
        for (int r = 0; r < 4; ++r){
            long usedMem1 = usedMemory();
            long usedMem2 = Long.MAX_VALUE;
            for (int i = 0; (usedMem1 < usedMem2) && (i < 500); ++i) {
                runtime.runFinalization();
                runtime.gc();
                Thread.yield();
                usedMem2 = usedMem1;
                usedMem1 = usedMemory();
            }
        }
    }

    private static long usedMemory() {
        return runtime.totalMemory() - runtime.freeMemory();
    }
}

实施界面

  

public interface Instantiator {Object execute(); }

使用您要测试的代码

public void sizeOfInteger(){
    int size = SizeOfUtil.execute(new Instantiator(){
        @Override public Object execute() {
            return new Integer (3);
        }
    });
    System.out.println(Integer.class.getSimpleName() + " size = " + size + " bytes");
}

来源:Java Tutorial Java Size of objects

答案 1 :(得分:0)

我认为这必须有所帮助: visualvm

它带有jdk,并且有许多有助于控制内存使用的东西

答案 2 :(得分:0)

通过比较加载程序之前和之后JVM的可用内存,可以获得非常接近的值。差异非常接近程序的内存使用情况。要使用JVM空闲内存

Runtime.getRuntime().freeMemory()

要获取内存使用量,请执行以下操作:

public static void main (String args[]){
  long initial = Runtime.getRuntime().freeMemory(); //this must be the first line of code executed
  //run your program ... load gui etc
  long memoryUsage = Runtime.getRuntime().freeMemory() -  initial ;

}

答案 3 :(得分:0)

来自java-application-memory-usage
使用-Dcom.sun.management.jmxremote选项启动您的应用,例如

java -Dcom.sun.management.jmxremote -jar myapp.jar

或者您可以尝试:

  

jvisualvm.exe您可以在jdk / bin目录中找到它。

看看以下可能会有所帮助:

  1. how-can-i-monitor-how-many-threads-and-memory-usage-in-my-jvm-or-eclipse
  2. Memory usage of a Java process