Find references to the object in runtime

后端 未结 6 779
梦谈多话
梦谈多话 2020-12-15 18:02

I have an object, which lives forever. I am deleteing all references I can see, to it after using it, but it still not collected. Its life cycle is pretty sophisticated so I

相关标签:
6条回答
  • 2020-12-15 18:23

    It is not collected because you haven't removed all references to it. The GC will only mark objects for collection if they have no roots in the application.

    What means are you using to check up on the GC to see if it has collected your object?

    0 讨论(0)
  • 2020-12-15 18:25

    You'll have to use Windbg and Sosex extension.

    The !DumpHeap and !GCRoot commands can help you to identify the instance, and all remaining references that keep it alive.

    0 讨论(0)
  • 2020-12-15 18:30

    I solved a similar issue with the SOS extension (which apparently does no longer work with Visual Studio 2013, but works fine with older versions of Visual Studio).

    I used following code to get the address of the object for which I wanted to track references:

    public static string GetAddress(object o)
    {
        if (o == null)
        {
            return "00000000";
        }
        else
        {
            unsafe
            {
                System.TypedReference tr = __makeref(o);
                System.IntPtr ptr = **(System.IntPtr**) (&tr);
                return ptr.ToString ("X");
            }
        }
    }
    

    and then, in Visual Studio 2012 immediate window, while running in the debugger, type:

    .load C:\Windows\Microsoft.NET\Framework\v4.0.30319\sos.dll
    

    which will load the SOS.dll extension.

    You can then use GetAddress(x) to get the hexadecimal address of the object (for instance 8AB0CD40), and then use:

    !do 8AB0CD40
    !GCRoot -all 8AB0CD40
    

    to dump the object and find all references to the object.

    Just keep in mind that if the GC runs, it might change the address of the object.

    0 讨论(0)
  • 2020-12-15 18:34

    I've been using .NET Memory Profiler to do some serious memory profiling on one of our projects. It's a great tool to look into the memory management of your app. I don't get paid for this info :) but it just helped me alot.

    0 讨论(0)
  • 2020-12-15 18:36

    Try using a memory profiler, (e.g. ants) it will tell you what is keeping the object alive. Trying to 2nd guess this type of problem is very hard.

    Red-gate gives 14 days trial that should be more then enough time to tack down this problem and decide if a memory profiler provides you with long term value.

    There are lots of other memory profilers on the market (e.g. .NET Memory Profiler) most of them have free trials, however I have found that the Red-Gate tools are easy to use, so tend try them first.

    0 讨论(0)
  • 2020-12-15 18:41

    The garbage collection in .NET is not a counting scheme (such as COM), but a mark-and-sweep implementation. Basically, the GC runs at "random" times when it feels the need to do so, and the collection of the objects is therefore not deterministic.

    You can, however, manually trigger a collection (GC.Collect()), but you may have to wait for finalizers to run then (GC.WaitForPendingFinalizers()). Doing this in a production app, however, is discouraged, because it may affect the efficiency of the memory management (GC runs too often, or waits for finalizers to run). If the object still exists, it actually still has some live reference somewhere.

    0 讨论(0)
提交回复
热议问题