Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of memory area and memory overflow in JVM

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you the JVM memory area and memory overflow example analysis, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!

Java memory region and memory overflow exception

Runtime data region

Program counter

The line number indicator of the bytecode executed by the current thread

Current thread is private

There will be no OutOfMemoryError situation

Java virtual machine stack

The thread is private and has the same life cycle as the thread.

In the memory model of java method execution, a stack frame is created while each method is executed, which stores information such as local variable table (basic type, object reference), Operand stack, dynamic link, method exit, etc.

StackOverflowError exception: when the stack depth requested by the thread is greater than the depth allowed by the virtual machine

OutOfMemoryError exception: if enough memory cannot be applied for when the stack is extended

Local method stack

Similar to the virtual machine stack, it mainly serves the Native method used by the virtual machine, and directly integrates the local method stack and the virtual machine stack in the HotSpot virtual machine.

Java heap (Java Heap)

The java heap is an area of memory shared by all threads and is created when the virtual machine starts. The purpose of this area is to store object instances. The java heap is the main area managed by the garbage collector. The java heap can also be subdivided into the new generation and the old age. In detail, there are Eden space, Form Survivor space, To Survivor space and so on.

Heap size can be controlled through-Xmx and-Xms

OutOfMemoryError exception: when there is no memory in the heap to complete instance allocation and the heap can no longer be expanded.

Method area

Inter-thread sharing

Used to store data such as class information, constants, static variables, compiled code, etc. that have been loaded by the virtual machine.

OutOfMemoryError exception: when the method area cannot meet the memory allocation requirements

Running constant pool

Part of the method area

Used to store all kinds of literals and symbol references generated during compilation

OutOfMemoryError exception: when the constant pool can no longer apply for memory

Direct memory

NIO can use the Native function library to directly allocate out-of-heap memory, and the DirectByteBuffer object in the heap operates as a reference to this memory.

Size is not limited by Java heap size, but by native (server) memory

OutOfMemoryError exception: when the system is out of memory

HotSpot virtual machine

Creation of object

When the virtual machine encounters a new instruction, it will first check whether the parameters of the object locate a symbolic reference to a class in the constant pool, and check that the class represented by the symbolic reference has been loaded, parsed, and initialized. If not, you must first perform the class loading process.

After the class load check passes, the virtual machine allocates memory for the new object. The amount of memory required by the object can be determined after the class loading is complete. Memory allocation can be in the form of "pointer collisions" and "free lists".

Access location of object

Java programs need to manipulate specific objects on the heap through the reference data on the stack. There are two access methods: handle and direct pointer.

Handle access to the java heap will divide a piece of memory as a handle pool. What is stored in the reference is the handle address of the object, and the handle contains the specific address information of the object instance data and the type data.

The layout of direct pointer access to java heap objects must consider how to place information about access type data. Object addresses are stored in reference.

The solution of OOM exception

Generate a Dump snapshot file:

The jvm parameter-XX:-HeapDumpOnOutOfMemoryError allows JVM to Dump the current memory dump snapshot when a memory overflow occurs.

Use jmap to produce dump files, and win to view the processes of tomcat through the task manager pid,linux use the ps command to view the process pid, and then use the jmap command

First, it is analyzed by a memory image analysis tool (such as Eclipse's Memory Analyzer). Common situations are:

Memory leak, the object is dead and cannot be automatically collected through the garbage collector. Only by finding out the location and cause of the leaked code can we determine the solution.

When memory overflows, all objects in memory must still survive, which means that the Java heap does not allocate enough space. Check the heap setting size (- Xmx and-Xms), and check the code to see if the object life cycle is too long and the state is held for too long.

Example of OOM exception:

Package oom

Import java.util.ArrayList; import java.util.List; / * VM Args:-Xms20m-Xmx20m-XX:+HeapDumpOnOutOfMemoryError * @ ClassName: HeapOOM * * / public class HeapOOM {static class OOMObject {} public static void main (String [] args) {List list = new ArrayList (); while (true) {list.add (new OOMObject ()) } these are all the contents of the article "example Analysis of memory regions and memory spills in JVM". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report