Java memory overflow and stack Overflow

Source: Internet
Author: User
Tags uuid

First, background knowledge
1. JVM Architecture



2. JVM Runtime Data area



3. JVM Memory model
JVM Runtime memory = Shared memory area + Thread memory Area




3-1. Shared Memory Area

Shared memory Area = Persistent Band + heap

Persistence Band = Method Area + Other

Heap = old space + young space

Young Space = Eden + S0 + S1




3-1-1, durable generation

The JVM implements the method area with the persistence band (Permanent Space), which mainly holds all loaded class information, method information, constant pool, and so on.

The -xx:permsize and-xx:maxpermsize can be used to specify the persistence of the initialization and maximum values .

Permanent space is not the same as the method area, but the hotspot JVM uses Permanent space to implement the method area, and some virtual machines do not

There are permanent space and other mechanisms are used to implement the method area.


3-1-2, Heap

Heap, used primarily to store object instance information for a class, including objects instantiated by the new operation and an array of definitions.

The heap is divided into old space (aka, tenured Generation) and young space.

Old space primarily stores live objects with long life cycles in the application;

Eden (Eden) mainly stores the new objects;

S0 and S1 are two memory areas of the same size, primarily storing Eden-surviving objects after each garbage collection, transitioning from Eden to old Space as objects

Buffer zone (s refers to the English word Survivor Space).

The heap is partitioned to facilitate object creation and garbage collection, which is explained later in the garbage collection section.


3-2. Thread Memory Area

Thread memory Area = Single thread memory + single thread memory + ....

Single thread memory =pc REGSTER+JVM Stack + local method stack

JVM stack = stack frame + stack frame + .....

Stack frame = local variable area + operand area + frame data area




In Java, a thread corresponds to a JVM stack (the JVM stack), and the running state of the thread is recorded in the JVM stack.

The JVM stack is made up of stack frames, and a stack frame represents a method call. Stack frame consists of three parts: local variable area, operand stack, frame data area.


Second, heap overflow

The heap is where Java holds object instances.

Heap Overflow can be divided into the following two cases, both of which throw the Outofmemoryerror:java heap space exception:


1. Memory leaks

A memory leak is when an object instance is still referenced after it has been created and used, not released by garbage collection, and accumulates until there is no remaining

Memory is available.

If memory leaks, we want to find out how the compromised object was referenced by GC ROOT , and then use the reference chain to specifically analyze the cause of the leak.

The tools for analyzing memory leaks are:JPROFILER,VISUALVM and so on.


Example code:

Package Com.jvm;import Java.util.arraylist;import java.util.list;import java.util.uuid;/** * Memory leak * @author Feizi * @time 2015-1-23 am 8:42:53 */public class Oomtest {public static void main (string[] args) {list<uuid> List = new arraylist& Lt Uuid> (); while (true) {List.add (Uuid.randomuuid ());}}}



Look at the output of the console, because the JVM on my side sets the parameter memory to be large enough to wait a certain amount of time to see the effect:






If you use the cmd command line, you can specify the parameter compilation to run, so the effect is faster:

Run the program with the following command, and be careful to compile the. Java source file into a. class bytecode file with the Javac command first.


Java-xms10m-xmx10m-xx:-usegcoverheadlimit Oomtest


2. Memory Overflow

memory overflow means that when we create a new strength object, the instance object takes up more memory space than the heap's free space.

If there is a memory overflow problem, this is often the program native needs more memory than we give the virtual machine configuration of memory, in this case, we can use the-XMX to solve this problem.


Example code:

Package Com.jvm;import java.util.arraylist;import java.util.list;/** * Memory Overflow * @author Feizi * @time 2015-1-23 morning 8:56:22 */p Ublic class Oomtest_1 {public static void main (String args[]) {list<byte[]> bytelist = new Arraylist<byte[]> () ; Bytelist.add (New byte[1000 * 1024 * 1024]);}}



See how the Console works:




Use the cmd command line to specify the parameters to run:


JAVA-VERBOSE:GC-XMN10M-XMS20M-XMX20M-XX:+PRINTGC oomtest_1




Third, thread stack

stack (JVM stack) storage is mainly the place of stack frame (local variable table, operand stack, dynamic link, method exit information). Note the stack and stack frames: stack frames are included.

There are two memory exceptions associated with thread stacks:

A), Stackoverflowerror (the method call hierarchy is too deep, the memory is not enough to create a new stack frame)

b), OutOfMemoryError (too many threads, not enough memory to create new threads)


1, Java.lang.StackOverflowError

Stack Overflow throws a java.lang.StackOverflowError error, which occurs when a new stack frame is requested when the method is running.

The space remaining on the stack is less than the space required for the battle frame.

For example, by recursively calling the method, the stack frame is constantly generated and the stack space is piled up until an exception is thrown:


Example code:

Package com.jvm;/** * Stack Overflow * @author Feizi * @time 2015-1-23 morning 9:13:11 */public class Softest {public void Stackoverflowmeth OD () {Stackoverflowmethod ();} /** * by recursive calling method, the stack frame is constantly generated, and the stack space is piled up until an exception is thrown: * @param args */public static void main (string[] args) {Softest sof = new softest (); Sof.stackoverflowmethod ();}}


See how the Console works:





Java memory overflow and stack Overflow

Related Article

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.