DotNetty icon indicating copy to clipboard operation
DotNetty copied to clipboard

Buffer size

Open IgorMamushin opened this issue 6 years ago • 14 comments

My application "eats" 22 MB per client. I found that this is because of the giant buffer size (16777216). How can I set the buffer size?

IgorMamushin avatar Jan 20 '18 15:01 IgorMamushin

That is the default global buffer pool (16 MB) shared by all channels and handlers. If you switch on debug logging, all the heap parameters are reported when DotNetty starts.

StormHub avatar Jan 20 '18 20:01 StormHub

If it is global, is it normal that it is created for each client? And after disconnecting the client, the buffer continues to hang in the memory, causing a leak

IgorMamushin avatar Jan 21 '18 07:01 IgorMamushin

The global buffer pool lasts for the life time of the application and shared across all channels.

StormHub avatar Jan 21 '18 11:01 StormHub

Okay, I did ChannelOption.Allocator, UnpooledByteBufferAllocator.Default. But the memory is not released after the client is turned off. What can I do?

IgorMamushin avatar Jan 21 '18 14:01 IgorMamushin

The global buffer pool lasts for the entire lifetime of the applications. The idea is to eliminate the GC pressure to improve performance. This is by design. You can tune the size down a bit though.

StormHub avatar Jan 22 '18 10:01 StormHub

what do you mean by this?

memory is not released

If you're not accessing pooled allocator, it should not be used at all and should not allocate. What memory are you referring to here?

nayato avatar Jan 22 '18 18:01 nayato

I meant a memory leak. The buffer is not destroyed with the client turned off. Even if I use UnpooledByteBufferAllocator

IgorMamushin avatar Jan 24 '18 17:01 IgorMamushin

@Fler Call this before your first call to DotNetty, on static init of class or somewhere else. Environment.SetEnvironmentVariable("io.netty.allocator.type", "unpooled"); https://github.com/Azure/DotNetty/blob/dev/src/DotNetty.Buffers/ByteBufferUtil.cs#L19-L42

Or use the below code to lower the memory used by PooledByteBufferAllocator. Environment.SetEnvironmentVariable("io.netty.allocator.maxOrder", "5");

yyjdelete avatar Jan 25 '18 02:01 yyjdelete

@Fler could you pls check what holds the buffer? I.e. take a process dump and inspect path to root for the buffer.

nayato avatar Mar 21 '18 15:03 nayato

@Fler did you manage to solve this issue? I'm also seeing memory not being released after a client disconnects and not being reused.

goncalo-oliveira avatar Jun 21 '18 08:06 goncalo-oliveira

Here's what I can see in taking memory in the heap:

  • DotNetty.Buffers.PoolThreadCache+MemoryRegionCache+Entry<Byte[]>[]
  • DotNetty.Common.Internal.MpscArrayQueue<DotNetty.Buffers.PoolThreadCache+MemoryRegionCache+Entry<Byte[]>>
  • DotNetty.Buffers.PoolThreadCache+SubPageMemoryRegionCache<Byte[]>

There's also a big chunk on Action items, being referenced by DotNetty.Common.Concurrency.XThread+<>c__DisplayClass10_0 via DotNetty.Common.Concurrency.XParameterizedThreadStart, which is a bit weird.

I tried setting io.netty.allocator.maxOrder=5 along with io.netty.allocator.type=unpooled and had no change. Also tried `io.netty.noPreferDirect=true', same result.

It's kind of frustrating, as a chunk is allocated every time a new connection comes in, but it isn't released when the connection drops.

Manual calls to the garbage collector don't produce any results either.

goncalo-oliveira avatar Jun 21 '18 16:06 goncalo-oliveira

Through the memory dumps, I traced it to here

0:1051> !dumpheap -stat
...
00007fff6a0150f8       75      1230600 System.ArraySegment`1[[System.Byte, System.Private.CoreLib]][]
...

0:1051> !gcroot 2858da22a58
HandleTable:
    000002858bb2ae20 (strong handle)
    -> 000002858d9ff710 System.Object[]
    -> 000002858d9ff988 DotNetty.Common.InternalThreadLocalMap
    -> 000002858d9ffa08 System.Object[]
    -> 000002858da004b0 System.Collections.Generic.List`1[[System.ArraySegment`1[[System.Byte, System.Private.CoreLib]], System.Private.CoreLib]]
    -> 000002858da22a58 System.ArraySegment`1[[System.Byte, System.Private.CoreLib]][]

This is using unpooled allocator.

goncalo-oliveira avatar Jun 22 '18 10:06 goncalo-oliveira

The same question, is there a solution?

MetSystem avatar Jul 11 '19 09:07 MetSystem

5 years later and it still is not fixed. Why does the memory stay allocated after the connection is dropped? Any solutions?

FabianHummel avatar Sep 26 '23 18:09 FabianHummel