How to set the Java Virtual Machine memory to accommodate large program loading

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.