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

How to solve the memory overflow of JVM

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

Share

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

This article introduces the knowledge of "how to solve JVM memory overflow". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Stack overflow (virtual machine stack and local method stack)

Cause

In HotSpot, it can only be set by the-Xss parameter. Because there is no distinction between the virtual machine stack and the local method stack in HotSpot.

Two kinds of exceptions occur when the stack overflows: StackOverflowError exception and OutOfMemoryError exception.

The StackOverflowError exception is because the stack depth requested by the thread is greater than the maximum depth allowed by the virtual machine.

The OutOfMemoryError exception occurs when the virtual machine stack memory allows dynamic expansion, when the extension stack capacity cannot apply for enough memory.

Because HotSpot does not support extension, OutOfMemoryError occurs only when the requested memory is not satisfied when the thread is created, and the rest is a StackOverflowError exception.

Conclusion: allocating memory to the stack of each thread is not as large as possible. It can be understood that, for example, the total memory is 2 gigabytes, and if a thread occupies 1.5 gigabytes, then.

Solution idea

When a StackOverflowError exception occurs, there is a clear error stack for analysis, which makes it relatively easy to locate the problem.

If you use the default parameters of the Hotspot virtual machine, the stack depth in most cases (because the frame size of each method on the stack is not the same, so it can only be said that in most cases), it should be sufficient for normal method calls (including recursive calls that cannot be optimized by tail recursion). However, if memory overflow is caused by too many threads, if the number of threads cannot be reduced or 64-bit virtual machines can be replaced, more threads can only be obtained by reducing the maximum heap and stack capacity.

Heap overflow

Cause

Overflow occurs when the total capacity reaches the maximum heap capacity when objects are constantly created and garbage collection is avoided.

Run the code: set the vm parameter-Xms10m-Xmx10m

Public class HeapTest {static class OOMObj {} / * vm arg-Xms10m-Xmx10m-XX:+HeapDumpOnOutOfMemoryError * / public static void main (String [] args) {List oomObjList = new ArrayList (); while (true) {oomObjList.add (new OOMObj ());}

Results:

Solution idea

First of all, confirm whether it is a memory leak or a memory overflow through the memory mapping analysis tool.

If it is a memory leak, the object that caused the OOM is not necessary. Further check the GC Roots reference chain through the tool. Generally speaking, it can be located more accurately.

If it is a memory overflow and the object must survive, check the virtual machine's heap parameters-Xms,-Xmx settings, and compare the machine memory to see if there is room for up-regulation. Then check the object life cycle, holding state time, storage structure whether the design is unreasonable and so on from the code.

Method zone and runtime constant pool overflow

Cause

The conditions for a class to be reclaimed by the garbage collector are harsh. Special attention should be paid to application scenarios where a large number of dynamic classes are generated at frequent runtime.

Solution idea

HotSpot has completely used metaspace instead of permanent bands in JDK8. Hotspot provides some parameters as defense measures for metaspace, mainly including:

XX:MaxMetaspacesize: sets the maximum metaspace. The default is-1, which means that there is no limit, or only limited by local memory size.

-XX:Metaspacesize: specify the initial space size of the metaspace, in parcels. Reaching this value will trigger garbage collection to unload the type, and the collector will adjust the value: if a large amount of space is freed, the value will be lowered appropriately; if very little space is freed, the value will be increased appropriately if the value is not more than-XX:MaxMetaspaceSize (if set).

-XX:MinMetaspace Free Ratio: the function is to control the percentage of the minimum metaspace remaining capacity after garbage collection, which can reduce the frequency of garbage collection due to insufficient metaspace. Similarly,-xx:Max-MetaspaceFreeRatio is used to control the percentage of the maximum meta-space remaining capacity.

Native direct memory overflow

Cause

A similar problem occurs when the allocateDirect method in ByteBuffer is used directly or indirectly, but not when clear is done. The obvious feature is that there are no obvious anomalies in the Heap Dump file.

Solution idea

Setting parameter:-XX:MaxDirectMemorySize

This is the end of the content of "how to solve the JVM memory overflow". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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