JVM memory

In this chapter you will learn:

  1. Get the memory in your Java virtual machine

Get the memory in your Java virtual machine

  • long totalMemory()
    Returns the total amount of memory in the Java virtual machine.
  • long freeMemory()
    Returns the amount of free memory in the Java Virtual Machine.
  • long maxMemory()
    Returns the maximum amount of memory that the Java virtual machine will attempt to use.
public class Main {
  public static void main(String[] args) {
    Runtime runtime = Runtime.getRuntime();
    System.out.println("Total memory:"+runtime.totalMemory());
    System.out.println("Free memory:"+runtime.freeMemory());
    System.out.println("Max memory:"+runtime.maxMemory());
  }/*from j  av a  2s  .  co m*/
}

The output:

Total memory:5177344
Free memory:4991032
Max memory:66650112

Next chapter...

What you will learn in the next chapter:

  1. Run garbage collector and finalization methods
Home » Java Tutorial » Utility Classes
Java standard stream
Java system property get and set
Current time in millis second and nano second
Random UUID
JVM memory
JVM garbage collector
JVM shutting down
Processor count
OS system commands
Random class
Random value
Random value range
Compile Java source code
Timer and TimerTask