How to Test the Java Memory Heap Size

Computers run Java applications by having an interpreter (the Java Virtual Machine, or JVM) execute them. The JVM allocates a given amount of memory to the heap -- the data structure that services all run-time requests for memory needed to create new objects. Users can instruct the JVM to allocate any amount of heap memory supported by the computer. You can find out from your Java code if the JVM allocated enough heap memory for the needs of your application; this allows better error handling.

Instructions

    • 1

      Include the following line at the beginning of your Java code:

      import java.lang.Runtime;

    • 2

      Find out the amount of heap memory measured in bytes that your program is currently using by calling method "Runtime.totalMemory()" as in the following example:

      long currentHeapSize = Runtime.getRuntime().totalMemory();

    • 3

      Find out the maximum amount of heap memory measured in bytes that your program will be allowed to use by calling method "Runtime.maxMemory()" as in the following example:

      long maxHeapSize = Runtime.getRuntime().maxMemory();

Related Searches:

References

Comments

You May Also Like

Related Ads

Featured