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 does Netty monitor memory leaks

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Editor to share with you Netty how to monitor memory leaks, I believe that most people do not know much, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Preface to how Netty monitors memory leaks

Generally speaking, pooled ByteBuf, that is, PooledByteBuf, is used in Netty programs to improve program performance. However, PooledByteBuf needs to be released manually after use, otherwise the memory requested by PooledByteBuf will not be returned, resulting in a memory leak and eventually OOM. Once the leak occurs, it is not a simple task to find unmanually released ByteBuf in complex applications. Without tool assistance, you can only check all the source code in a white box, which is undoubtedly very inefficient.

In order to solve this problem, Netty designed a special leak detection interface to monitor the resource objects that need to be released manually.

Weak references and reference queues for JDK

Before analyzing the leak monitoring capabilities of Netty, let's review the JDK knowledge that will be used: references.

There are 4 reference types in java, which are strong reference, soft reference, weak reference and virtual reference.

Strong citation

Strong quotation is the most common way for us to write programs. For example, if a value is assigned to a variable, the object value is strongly referenced by that variable. Java's memory collection does not recycle the object unless it is set to null. Even if an out-of-memory exception occurs.

Soft reference

Objects referenced by soft references will be reclaimed by java when gc runs out of memory. If java has enough memory when gc occurs, the object will not be recycled in the following way

SoftReference ref = new SoftReference (new Date ())

Date tmp = ref.get (); / / if the object is not recycled, the get operation returns the initialized value. If it is recycled, null is returned.

Weak reference

Weak references are worse than soft references. Weakly referenced objects are recycled whenever a gc occurs. It is used in a manner similar to soft references, as follows

WeakReference re = new WeakReference (new Date ())

Re.get ()

Virtual reference

Unlike the previous soft and weak references, virtual references do not affect the lifecycle of objects. It is represented by the java.lang.ref.PhantomReference class in java. If an object is associated with a virtual reference, it can be reclaimed by the garbage collector at any time, just as no reference is associated with it.

With the exception of strong references, all references have a reference queue to match. When java cleans up calls to unnecessary references, the reference itself (not the value object that the reference points to) is added to the queue. The code is as follows

ReferenceQueue queue = new ReferenceQueue (); WeakReference re = new WeakReference (new Date (), queue); Reference > allLeaks;// records the hash value of the tracking object, which is used for object comparison in subsequent operations. Private final int trackedHash

This class serves three purposes:

Call the record method to record the call trajectory

Call the close method to end the trace

And as a WeakReference, it is listed in the ReferenceQueue after the tracking object is reclaimed by the GC.

Let's take a look at the record method first. The code is as follows

@ Overridepublic void record () {record0 (null);} @ Overridepublic void record (Object hint) {record0 (hint);} private void record0 (Object hint) {if (TARGET_RECORDS > 0) {Record oldHead; Record prevHead; Record newHead; boolean dropped Do {if ((prevHead = oldHead = headUpdater.get (this)) = = null) {/ / already closed. Return;} final int numElements = oldHead.pos + 1; if (numElements > = TARGET_RECORDS) {final int backOffFactor = Math.min (numElements-TARGET_RECORDS, 30); if (dropped = PlatformDependent.threadLocalRandom (). NextInt (1)

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

Internet Technology

Wechat

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

12
Report