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 problem of memory leakage caused by EnyimMemcached Asynchronous Transformation

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

How to solve the memory leakage problem caused by asynchronous transformation of EnyimMemcached, I believe many inexperienced people are helpless about this, for this reason this article summarizes the causes and solutions of the problem, through this article I hope you can solve this problem.

On June 30, after we released the asynchronous revamped blog program, there was an undesirable situation of high memory, high CPU, and high thread count.

After a week of tracking, the water finally fell sunrise. The root cause of the undesirable situation is a memory leak in our modified EnyimMemcached code.

The root cause of the memory leak was that we didn't dispose of SocketAsyncEventArgs. In fact, we didn't notice that SocketAsyncEventArgs implemented the IDdispose interface at that time, and this small oversight actually tormented us for a week.

The code with the memory leak problem is written like this:

a) Read data asynchronously from Socket:

public async Task ReadBytesAsync(int count) { var args = new SocketAsyncEventArgs(); args.SetBuffer(new byte[count], 0, count); var awaitable = new SocketAwaitable(args); await this.socket.ReceiveAsync(awaitable); return args.Buffer; }

b) Write data asynchronously to Socket:

public async Task WriteSync(IList buffers) { var args = new SocketAsyncEventArgs(); args.BufferList = buffers; var awaitable = new SocketAwaitable(args); await this.socket.SendAsync(awaitable); }

The solution to the memory leak problem is simple: using+Buffer.BlockCopy, the code is as follows:

a) Improved asynchronous reading of data from Socket:

public async Task ReadBytesAsync(int count) { using (var args = new SocketAsyncEventArgs()) { args.SetBuffer(new byte[count], 0, count); var awaitable = new SocketAwaitable(args); await this.socket.ReceiveAsync(awaitable); var receivedBytes = new Byte[args.BytesTransferred]; Buffer.BlockCopy(args.Buffer, 0, receivedBytes, 0, args.BytesTransferred); return receivedBytes; } }

b) Improved asynchronous writing to Socket:

public async Task WriteSync(IList buffers) { using (var args = new SocketAsyncEventArgs()) { args.BufferList = buffers; var awaitable = new SocketAwaitable(args); await this.socket.SendAsync(awaitable); } }

The improved code has been posted to github: https://github.com/cnblogs/EnyimMemcached.

You might ask how we detected the memory leak?

We rely on two tools: Windows Task Manager and Performance Monitor.

1. Through the task manager, we observed that the memory occupied by w3wp will continue to grow. When it reaches about 5G, the CPU on the 8-core 8G Alibaba Cloud virtual machine will start to ride a roller coaster. Only by recycling the program pool (restarting the w3wp process) can it return to normal.

2. With Performance Monitor, we monitor two metrics:

a) \. NET CLR Memory(w3wp)\# Bytes in all Heaps (for managed memory)

b) \Process(w3wp)\Private Bytes (for unmanaged memory)

The observed conditions are shown in the figure below:

(Green is Private Bytes)

Both Bytes in all Heaps and Private Bytes continue to grow.

After Dispose SocketAsyncEventArgs, the\. NET CLR Memory\# Bytes in all Heaps seen by Performance Monitor looks like this:

\Process\Private Bytes also contrasts with Bytes in all Heaps:

As soon as you see this graphic, you should feel, as we do, GC's hard work behind it.

After reading the above, do you know how to solve the memory leak problem caused by asynchronous transformation of EnyimMemcached? If you still want to learn more skills or want to know more related content, welcome to pay attention to the industry information channel, thank you for reading!

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