How do I know the memory size in virtual machine?

If you want to know the free memory and the total memory available in the Java runtime environment then you can use the following code snippet to check.

package org.kodejava.example.lang;

public class MemoryExample {
    public static void main(String[] args) {
        long freeMemory = Runtime.getRuntime().freeMemory();
        long totalMemory = Runtime.getRuntime().totalMemory();

        System.out.println("Free Memory  = " + freeMemory);
        System.out.println("Total Memory = " + totalMemory);
    }
}

Here is the result of the code snippet above:

Free Memory  = 252054480
Total Memory = 257425408

Wayan Saryada

Founder at Kode Java Org
I am a programmer, a runner, a recreational diver, currently live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA. You can support my works by donating here. Thank you 🙂.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.