An exception occurred when using a Java program to query large amounts of data from a database:
Java.lang.OutOfMemoryError:Java Heap Space
This exception information is thrown in the JVM if 98% of the time is used for GC and the available HEAP size is less than 2%.
The JVM heap setting is the set of memory space that the JVM can provision during the run of the Java program. The JVM automatically sets the value of the heap size when it starts, and its initial space (that is,-XMS) is 1/64 of the physical memory, and the maximum space (-XMX) is 1/4 of the physical memory. You can use options such as the-XMN-XMS-XMX provided by the JVM to set it up.
Example:java-jar-xmn16m-xms64m-xmx128m Myapp.jar
If the heap size setting is small, in addition to these exception information, you will notice that the program's responsiveness slows down. The GC takes up more time and the application is assigned less execution time.
The Heap Size does not exceed 80% of the available physical memory, generally the-XMS and-XMX options are set to the same, and the-XMX value of-xmn is 1/4.
The-XMS-XMN setting of the Heap size does not exceed the size of the physical memory. Otherwise, "Error occurred during initialization of VM Could not reserve enough space for object heap" will be prompted.
Java memory configuration