ASP.NET: Should I worry about memory leaks in certain situations? [WITH#]

I am worried about garbage collection when two objects refer to each other ...

public class A
{   public readonly B _b;
    public A()
    {   _b = new B(this);
    }
}

public class B
{   public readonly A _a;
    public B(A objA)
    {   _a = objA;
    }
}

In this case, the third class may refer to A...

public class Foo
{   public A _a = new A(); // A and B are both created here.
    public void Bar()
    {   _a = new A();
    }   // Create a new A (and B)
}

Typically, the garbage collector has objects that no longer have active links. But in this case, objects A and B never lose all of their active links, since they always refer to each other.

When an object Fooreplaces the current A(s B) with new A(s B), will the garbage collector be able to clear the old A(s B) without starting an infinite loop with circular object references?

, ASP.NET. - , , .
:
& ; < - - .
& ; < - - .

.NET ?

+3
4

.NET GC , , . . A ↔ B, , .

, GC . CLR , . , . , . , . , , . GC , CLR. , . , .

- http://msdn.microsoft.com/en-us/magazine/cc163491.aspx

+7

A β†’ B, B β†’ A A B, GC , .

ASP.NET . . , , IIS , . , IIS , , . ; SOA, - WCF, -.

+2

.NET .

, VB6 GC.
, , " ".

+1

:

When the garbage collector starts, it starts with things like memory registers and static fields, etc. They are known as Roots.

From there, the garbage collector follows links from the root to other instances of the objects. This creates a graph of all objects accessible from the roots.

If your objects are inaccessible from the roots, they are considered unacceptable and can be cleaned by the garbage collector.

+1
source

Source: https://habr.com/ru/post/1780088/


All Articles