Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Garbage collector Will behave on value type and Reference type

Tags:

c#

asp.net

How Garbage collector Will behave on value type and Reference type, when it will free memory value type and reference type. i am little bit confused about this can anyone explain about this, at the same time garbage collector remove free the Memory of value type or Reference type Which remove first

like image 507
ramsTecH1 Avatar asked Dec 26 '12 09:12

ramsTecH1


People also ask

Are Value types garbage collected?

They are not collected because they are the things that are alive that keep almost everything else alive. And obviously they are not deallocated by compacting the GC heap; they're deallocated by popping the stack.

How does a garbage collector work?

The garbage collector considers unreachable objects garbage and releases the memory allocated for them. During a collection, the garbage collector examines the managed heap, looking for the blocks of address space occupied by unreachable objects.

What is difference between value type and reference type?

Variables of reference types store references to their data (objects), while variables of value types directly contain their data. With reference types, two variables can reference the same object; therefore, operations on one variable can affect the object referenced by the other variable.

What is the advantage of using a reference type over a value type?

Yes the main advantage of Reference type will be the ability to work with the exact same Object. Classes are like Object factories, they create the Object and store it and a reference to it in memory. For example lets say we had a Car Class.


1 Answers

You're thinking about the problem the wrong way. Stop thinking about "value types" and "reference types". Instead, start thinking about variables, and whether those variables are short lived or long lived.

The purpose of the garbage collected heap is to reclaim the storage associated with long-lived variables. The purpose of the stack is to reclaim the storage associated with short-lived variables.

People will try to tell you that "value types go on the stack" and "references go on the heap" and so on, and this is confusing nonsense. Variables go on the stack or the heap (or registers -- everyone forgets about registers) and variables can be of value type or reference type.

You keep asking "which will the garbage collector remove first?" That question cannot be answered. The garbage collected heap makes no guarantees whatsoever about the order in which memory is reclaimed. The short-lived storage -- the stack -- will be reclaimed as activation frames are popped off the stack. However, the C# language permits the garbage collector to clean up storage that is referenced by short-lived storage before the frame is popped off the stack if the runtime can determine that the reference will not be accessed again. Basically, when storage is reclaimed is an implementation detail of the runtime, subject to change at any moment.

like image 50
Eric Lippert Avatar answered Oct 21 '22 02:10

Eric Lippert