Search code examples
memorybluej

How do I find the amount of memory used by a program?


I'm running a couple of Java programs in BlueJ. I want to be able to know how much memory the program uses for a given input value. Is there any way I can do this?


Solution

  • 1) to get the maximum amount of memory that your Java application can use Runtime:

    Runtime runtime = Runtime.getRuntime(); 
    System.out.println("max memory: " + runtime.maxMemory() / 1024); 
    

    2)to get how much memory that JVM has allocated for your application :

    Runtime runtime = Runtime.getRuntime(); 
    System.out.println("allocated memory: " + runtime.totalMemory() / 1024); 
    

    3)to get how much memory is being used by your application:

    Runtime runtime = Runtime.getRuntime(); 
    System.out.println("free memory: " + runtime.freeMemory() / 1024);