1

I have written a java program, and I want to see when it runs how much RAM it uses. Is there any way to see how much RAM usage is related to my program? I mean something like time usage of the program that can be seen by writing this code before and after calling the main code:

long startTime = System.currentTimeMillis();
new Main();
long endTime = System.currentTimeMillis();
System.out.println("Total Time: " + (endTime - startTime));
BryanH
  • 5,826
  • 3
  • 34
  • 47
  • possible duplicate of [determining java memory usage](http://stackoverflow.com/questions/390449/determining-java-memory-usage) – axel_c Apr 12 '13 at 09:08

3 Answers3

1

You can use the following class. Implemeting the Instantiator interface you can execute several time the same process to get a precise view of the memory consumption

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

Implement the interface

public interface Instantiator { Object execute(); }

With the code you want to test

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

source : Java Tutorial Java Size of objects

0

I think this must help: visualvm

it comes with jdk, and have many thing that help to control memory usage

Aliaksei Bulhak
  • 6,078
  • 8
  • 45
  • 75
0

you can get a very close value by comparing the free memory of the JVM before and after the loading of your program. The difference is very close to the memory usage of your program. To get the JVM free memory use

Runtime.getRuntime().freeMemory()

To get the memory usage do this:

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 ;

}
MaVRoSCy
  • 17,747
  • 15
  • 82
  • 125
  • I need the free memory while the programe is running not when the run finishes. does it work too? –  Apr 12 '13 at 11:42
  • when the program ends, it works so it doesn't show the memory usage of code. –  Apr 12 '13 at 13:28
  • actually it differs from time, because when the program run ends it doesn't use memory anymore.so I think it doesn't work! –  Apr 12 '13 at 20:55