SignalR objects are not being deleted from the heap even when the hub connection is disposed

Drew

I have a web application that sends messages from the server to the front end (Javascript) client using SignalR. However, there is also a back end (.NET) client that creates a proxy to the hub. The back end client processes messages and sends them to the hub, and the hub then sends these messages to the front end client.

Here is a snippet of the back end client that creates the hub connection and proxy to send messages to the hub:

HubConnection hubConnection = new HubConnection(serverUrl);
IHubProxy hubProxy = hubConnection.CreateHubProxy(hubName);
Task t = Task.Run(() => hubConnection.Start(new LongPollingTransport()));
t.WaitAndUnwrap();
if (hubProxy != null && hubConnection.State == ConnectionState.Connected)
{
    await hubProxy.Invoke("MessageClients", messageArgs);
}

There is obviously more to the code, but that covers the essential part.

The key part is, every time a message is created, this code is called. That means there is a HubConnection object created for every message that needs to be sent to the hub, and then to the front end client. After making a memory dump, I realized a lot of objects remain on the heap and is causing a memory leak. I figured it's because I'm not disposing the hub connection, since one of the main culprits is Microsoft.AspNet.SignalR.Transports.LongPollingTransport, with over a thousand objects on the heap after playing around with the website for about an hour (doing things that would create these SignalR messages).

So I thought disposing HubConnection would fix things:

using (HubConnection hubConnection = this.CreateHubConnection())
{
    if (hubConnection != null)
    {
        IHubProxy hubProxy = this.StartConnection(hubConnection);
        if (hubProxy != null && hubConnection.State == ConnectionState.Connected)
        {
            await hubProxy.Invoke("MessageClients", messageArgs);
        }
    }
}

private HubConnection CreateHubConnection()
{
    // do some basic auth set up
    HubConnection hubConnection = new HubConnection(this.serverUrl);
    hubConnection.Headers.Add('authToken', basicAuth);
    return hubConnection;
}

private IHubProxy StartConnection(HubConnection hubConnection)
{
    IHubProxy hubProxy = hubConnection.CreateHubProxy(this.hubName);
    Task t = Task.Run(() => hubConnection.Start(new LongPollingTransport())
    t.WaitAndUnwrap();
    return hubProxy;
}

But after running the process and attaching WinDbg to it, when I do -!dumpheap -stat -type Microsoft.AspNet.SignalR.Transports, again I see Microsoft.AspNet.SignalR.Transports.LongPollingTransport up there with similar high figures for objects on the heap + memory. Shouldn't the using statement cause HubConnection to be disposed and therefore removed from the heap? What should I be doing to fix this memory leak?

Drew

It turns out that disposing the hub connection does work. I was conflating two concepts: disposing unmanaged resources and deleting objects from the heap.

I thought that when the using statement for the HubConnection object finishes (i.e., when dispose is called on HubConnection), WinDbg would no longer show that object in the memory dump. But since the memory dump is based on what's on the heap, and since disposing an object doesn't delete the object from the heap (just allows it to be released and deleted by the garbage collector), it was still showing in WinDbg.

I played around with the application more until the garbage collector did its work, and found that the objects were deleted later.

I should also note that my original question emphasized the type Microsoft.AspNet.SignalR.Transports.LongPollingTransport, and that was because I was not disposing the LongPollingTransport object either (when I do hubConnection.Start(new LongPollingTransport()). So my code now looks like the following:

using (HubConnection hubConnection = this.CreateHubConnection())
{
    if (hubConnection != null)
    {
        using (LongPollingTransport lpTransport = newLongPollingTransport())
        {
            IHubProxy hubProxy = this.StartConnection(hubConnection, lpTransport);
            if (hubProxy != null && hubConnection.State == ConnectionState.Connected)
            {
                await hubProxy.Invoke("MessageClients", messageArgs);
            }
        }
    }
}

private HubConnection CreateHubConnection()
{
    // do some basic auth set up
    HubConnection hubConnection = new HubConnection(this.serverUrl);
    hubConnection.Headers.Add('authToken', basicAuth);
    return hubConnection;
}

private IHubProxy StartConnection(HubConnection hubConnection, HttpBasedTransport transport)
{
    IHubProxy hubProxy = hubConnection.CreateHubProxy(this.hubName);
    Task t = Task.Run(() => hubConnection.Start(transport));
    t.WaitAndUnwrap();
    return hubProxy;
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Signalr - endless $.connection.hub.disconnected event?

分類Dev

Crashing when objects are deleted

分類Dev

How to call SignalR hub when application start?

分類Dev

Detect lost connection from SignalR Core connection

分類Dev

Laravel record being deleted when calling Factory

分類Dev

How to protect PyPI package from being deleted

分類Dev

Signalr-無限の$ .connection.hub.disconnectedイベント?

分類Dev

When should a ManualResetEvent be disposed?

分類Dev

Is the value of a C# decimal stored on the heap even when it is a local variable?

分類Dev

Prevent connection string from being committed to repository

分類Dev

Maven Clean: excluding directory inside target from being deleted

分類Dev

iOS: Prevent file in the .cacheDirectory from being deleted on full storage warning

分類Dev

How to prevent files from being deleted on idling in GAE?

分類Dev

Prevent first row (dynamic table) from being deleted jquery

分類Dev

Getting connection returns false even when the device has connection

分類Dev

SignalR not working when client behind a high latency connection

分類Dev

Is there a way to connect to SignalR Hub built in ASP. NET from other application?

分類Dev

Is calling a SignalR Hub from the Application Service Layer a bad practice in ASP.Net Boilerplate?

分類Dev

Access SignalR Hub Context.Items from an singleton service in ASP.NET Core

分類Dev

Is it good practice to copy objects from the stack to the heap by setting a dereferenced pointer?

分類Dev

SignalR Get Connection Id

分類Dev

Python continue looping over a list even when a connection error occurs

分類Dev

Access SignalR Hub without Constructor Injection

分類Dev

Why is the memory created via unique_ptr not being deleted properly when reset() is called?

分類Dev

EF Core DbContext being disposed of before Async methods are complete

分類Dev

Is gc always invoked, even when heap space is continuously available at run-time?

分類Dev

Is gc always invoked, even when heap space is continuously available at run-time?

分類Dev

How to switch between Japanese input sources without all text being deleted from previous input source?

分類Dev

Values are being null while getting it from Redis for complex objects

Related 関連記事

  1. 1

    Signalr - endless $.connection.hub.disconnected event?

  2. 2

    Crashing when objects are deleted

  3. 3

    How to call SignalR hub when application start?

  4. 4

    Detect lost connection from SignalR Core connection

  5. 5

    Laravel record being deleted when calling Factory

  6. 6

    How to protect PyPI package from being deleted

  7. 7

    Signalr-無限の$ .connection.hub.disconnectedイベント?

  8. 8

    When should a ManualResetEvent be disposed?

  9. 9

    Is the value of a C# decimal stored on the heap even when it is a local variable?

  10. 10

    Prevent connection string from being committed to repository

  11. 11

    Maven Clean: excluding directory inside target from being deleted

  12. 12

    iOS: Prevent file in the .cacheDirectory from being deleted on full storage warning

  13. 13

    How to prevent files from being deleted on idling in GAE?

  14. 14

    Prevent first row (dynamic table) from being deleted jquery

  15. 15

    Getting connection returns false even when the device has connection

  16. 16

    SignalR not working when client behind a high latency connection

  17. 17

    Is there a way to connect to SignalR Hub built in ASP. NET from other application?

  18. 18

    Is calling a SignalR Hub from the Application Service Layer a bad practice in ASP.Net Boilerplate?

  19. 19

    Access SignalR Hub Context.Items from an singleton service in ASP.NET Core

  20. 20

    Is it good practice to copy objects from the stack to the heap by setting a dereferenced pointer?

  21. 21

    SignalR Get Connection Id

  22. 22

    Python continue looping over a list even when a connection error occurs

  23. 23

    Access SignalR Hub without Constructor Injection

  24. 24

    Why is the memory created via unique_ptr not being deleted properly when reset() is called?

  25. 25

    EF Core DbContext being disposed of before Async methods are complete

  26. 26

    Is gc always invoked, even when heap space is continuously available at run-time?

  27. 27

    Is gc always invoked, even when heap space is continuously available at run-time?

  28. 28

    How to switch between Japanese input sources without all text being deleted from previous input source?

  29. 29

    Values are being null while getting it from Redis for complex objects

ホットタグ

アーカイブ