简体   繁体   中英

How to increase JVM heap size

I have followed some of the posts in increase JVM heap size Stack Overflow to increase the JVM heap size. I set environment variables as suggested above link.

I executed below program, to check, what is the max heap size.

public class GetHeapSize {
     public static void main(String[]args){

            //Get the jvm heap size.
            long heapSize = Runtime.getRuntime().totalMemory();

            //Print the jvm heap size.
            System.out.println("Heap Size = " + heapSize);
        }

}

I got output before setting and after setting is same as 16252928

Please can anyone suggest how can I increase heap size?

The totalMemory() reports the current size of the heap. If you want the maximum size that the heap will grow to, call maxMemory() instead. The javadoc says:

"Returns the maximum amount of memory that the Java virtual machine will attempt to use. If there is no inherent limit then the value Long.MAX_VALUE will be returned."

and then says that the amount is measured in bytes.

in the common case the Heap size grows dynamically, so that you can use -Xmx option to limit the high size of the heap and -Xms option to set the start size of the heap and you can make them the same on the start, so that the JVM will not spend resources for dynamic memory operations. Also may be it would be interesting for you to use more informative JMX way to get heap size through the MemoryMXBean bean.

MemoryMXBean MEMORYMXBEAN = ManagementFactory.getMemoryMXBean();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM