Local variables and stack

What will happen in the following code snippet:

while (!Done)
{
     Data data = source.GetData();
     sink.ProcessData(data);
}

Is a new link datapushed onto the stack at each iteration of the loop (thus preventing garbage collection of object references data), or is this link reused at each iteration?

+3
source share
8 answers

The variable is "reused" as you say. Stack / local variables are just what is local to the method and NOT a scope. The area in which your variable is located dataonly keeps it from being visible outside this scope and, conversely, does NOT define a different stack slot.

, .

+2

( , ), ?

, "" , . , . , "" , , ​​. , , , , GC .

- , , KeepAlive .

+8

( ) . while ( - ).

+1

, - - :)

, , GC, .

+1

, , . ValueType, .

.

+1

:

    private class test
    {
        ~test()
        {
        }
    }

    static void Main()
    {
        while (true)
        {
            GC.Collect();
            test t = new test();
        }
    }

t . Finalizer , .

+1

, - :

sink.ProcessData(source.GetData());

, heap , , .

+1

, . ?

0

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


All Articles