Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Possible memory leak when used with ASP.NET Core DI #148

@simonhaines

Description

@simonhaines

While investigating an issue with high CPU usage, a memory dump was taken of a long-running ASP.NET Core 6 server application (see below) that uses the PubNubPCL library.

The heap has a lot of retained instances of objects related to the PubNub API, particularly PNConfguration which, after about 3 days of continuous operation, occupies 107MB of heap space.

I suspect the issue lies with the lifetime of the PubNub client in the DI container. We have a thin wrapper around the PubNub API that we inject with a Transient lifetime. The constructor creates a new PubNub object and configures it every time it is injected.

Based on the large number of PubnubApi.ConcurrentDictionary objects also on the heap, I believe the PubNub client is supposed to have a Singleton lifetime, but I could not find any guidance in the documentation regarding DI containers.

Can you confirm the expected lifetime of the PubNub client? Is it expected to be a Singleton, or can it be constructed as needed in a long-running service?

Here is the relevant part of the heap analysis created with dotnet-dump. The command used was dumpheap -stat -live (statistical analysis of live objects only).

              MT    Count    TotalSize Class Name
[snip]
00007f832b80f308   235225      7527200 PubnubApi.ConcurrentDictionary`2[[System.String, System.Private.CoreLib],[System.Net.HttpWebRequest, System.Net.Requests]]
00007f83294d3a10   235225      7527200 PubnubApi.NewtonsoftJsonDotNet
00007f832b827460        1      7786800 System.Collections.Generic.Dictionary`2+Entry[[System.String, System.Private.CoreLib],[PubnubApi.ConcurrentDictionary`2[[System.String, System.Private.CoreLib],[System.Net.HttpWebRequest, System.Net.Requests]], PubnubPCL]][]
00007f832b826520        1      7786800 System.Collections.Generic.Dictionary`2+Entry[[System.String, System.Private.CoreLib],[PubnubApi.EndPoint.TokenManager, PubnubPCL]][]
00007f832b8262a0        1      7786800 System.Collections.Generic.Dictionary`2+Entry[[System.String, System.Private.CoreLib],[PubnubApi.IPubnubLog, PubnubPCL]][]
00007f832b826020        1      7786800 System.Collections.Generic.Dictionary`2+Entry[[System.String, System.Private.CoreLib],[PubnubApi.PNConfiguration, PubnubPCL]][]
00007f83294d45b8   235225     13172600 PubnubApi.EndPoint.TokenManager
00007f8325678080     4495     13808408 System.Int32[]
00007f832b80b6a0   470451     15054432 PubnubApi.ConcurrentDictionary`2[[System.String, System.Private.CoreLib],[System.Boolean, System.Private.CoreLib]]
00007f832b8276e0        2     15573600 System.Collections.Generic.Dictionary`2+Entry[[System.String, System.Private.CoreLib],[PubnubApi.ConcurrentDictionary`2[[System.String, System.Private.CoreLib],[System.Boolean, System.Private.CoreLib]], PubnubPCL]][]
00007f8325e89150   708563     17005512 System.Threading.Timer
00007f8326aada58   708775     17010600 System.Threading.TimerHolder
00007f83255c5290   708988     17015712 System.Object
00007f832b826f38   235225     18818000 System.Collections.Generic.Dictionary`2[[System.String, System.Private.CoreLib],[System.Net.HttpWebRequest, System.Net.Requests]]
00007f8325ba2890       17     33503088 System.Collections.Generic.Dictionary`2+Entry[[System.String, System.Private.CoreLib],[System.String, System.Private.CoreLib]][]
00007f83294d4898   708502     34008096 PubnubApi.EndPoint.TelemetryManager
00007f832a8c9368   470451     37636080 System.Collections.Generic.Dictionary`2[[System.String, System.Private.CoreLib],[System.Boolean, System.Private.CoreLib]]
00007f8325abac58    38012     39857914 System.Byte[]
00007f83266827b8   708749     45359936 System.Threading.TimerCallback
00007f8326682678   708860     68050560 System.Threading.TimerQueueTimer
00007f832567d2e0   740815     68704692 System.String
00007f83294d2820   708502    107692304 PubnubApi.PNConfiguration
Total 7963348 objects

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions