Java virtual machines have a limited amount of memory for running programs, and when our projects or programs are large, they tend to be memory overrun.
As an example:
Public class SmallTest1 { publicstaticvoid main (string[] args) { byte newbyte[1024*1024*500]; }}
When you define such a 500MB array, it causes a JVM memory overflow:
While the Java Virtual Machine default program run can get the memory size is the system, by the Java API architecture, click Java:
After finding the "-xmxn" item:
So the default size is set according to the system, so how do we set the maximum running memory?
If you are using Eclipse, right-click on the project or program you want to do, select "Run as"---> "Run configurations ...", select "(x) = Arguments" and increase the set memory size in VM Arguments:
Get the following and set the parameters:
Here I set the 800MB of memory, enough to run the program just now, and then we will try to test whether the program can actually run and no longer memory overflow:
Public class SmallTest1 { publicstaticvoid main (string[] args) { byte newbyte[1024*1024*500]; System.out.println ("Run successfully! "); }}
============================================================
You can see that the program no longer causes the Java Virtual machine memory to overflow, and we pass the thread sleep mode to verify again:
Public class SmallTest1 { publicstaticvoidthrows interruptedexception { byte New byte [1024*1024*500]; System.out.println ("Run successfully! "); Thread.Sleep (the); // set the program to run for 5 seconds to view memory changes }}
System memory before the program was run:
System memory after running the program:
===========================================
If you are using a DOS command, type JAVA-XMX (a numeric value) (Memory unit) program name in the CMD window
For example:
How to set the Java Virtual Machine memory to accommodate large program loading