Java.lang.OutOfMemoryError:Java Heap Space

Source: Internet
Author: User

When testing the RESTful interface return data in MyEclipse, the main method executes the program directly, but after a few seconds the program error, the return string result is normal, but the amount of data is a bit large.

First look at the source code arrays.java:2367

Error message when creating a char array object, heap space overflow

Gorgeous split line, the following heap and stack interpretation is still relatively clear

Register: We can't control it in the program

Stack: A reference to a primitive type of data and objects, but the object itself is not stored in the stack, but is stored in the heap (new object)

Heap: Storing data generated with new

Static domain: Static members that are stored in the object with static definitions

Constant pool: Storing constants

Non-RAM storage: Persistent storage space such as hard drives

From the heap and stack function and the role of the popular comparison, the heap is mainly used to store objects, the stack is mainly used to execute the program.

All method calls are made through stacks, all local variables, and formal parameters are allocated from the stack memory space.

    • Heaps and stacks in the JVM

The JVM is a stack-based virtual machine, and the JVM allocates a stack for each newly created thread, which means that, for a Java program, it runs through the operation of the stack. The stack holds the state of the thread in frames, and the JVM operates on the stack in only two ways: stack and stack operations in frames.

When a thread activates a Java method, the JVM will press a new frame into the Java stack of threads, which naturally becomes the current frame. During this method execution, this frame is used to hold parameters, local variables, intermediate calculation procedures, and other data.

Each Java application uniquely corresponds to a single JVM instance, and each instance uniquely corresponds to one heap. All instances or arrays of classes created by the application in the run are placed in this heap and shared by all threads of the application. Allocating heap memory in Java is automatically initialized. The storage space for all objects in Java is allocated in the heap, but the reference to this object is allocated on the stack, that is, allocating memory from two places when an object is built, memory allocated in the heap actually establishes the object, and the memory allocated on the stack is just a pointer (reference) to the heap object.

    • Heap and Stack in Java

Java divides memory into two types: one is stack memory, and the other is heap memory.

Some of the basic types of variables and object reference variables defined in the function are allocated in the stack memory of the function.

Heap memory is used to hold objects and arrays created by new.

The advantage of the stack is that the access speed is faster than the heap, after the register, the stack data can be shared.

The disadvantage is that the size and lifetime of the data in the stack must be deterministic and inflexible.

Here's how it's resolved

    • Method 1, a single class runs:

Select the Run configuration or debug configurations to set the parameters in VM arguments -xms128m-xmx512m

Meaning of the parameter
-vmargs-xms128m-xmx512m-xx:permsize=64m-xx:maxpermsize=128m
The-vmargs description is followed by the parameters of the VM, so the following are actually the parameters of the JVM
-xms128m JVM Initial allocation of heap memory
-XMX512M JVM Maximum allowable allocated heap memory, on demand
-xx:permsize=64m JVM Initial allocation of non-heap memory
-xx:maxpermsize=128m JVM Maximum allowable allocated non-heap memory, on demand

    • Method 2, the overall operating environment settings

preferences-->java-->installed JREs Editing a JDK in use,

Set the parameters in the default VM arguments -xms128m-xmx512m

Java.lang.OutOfMemoryError:Java Heap Space

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.