Memory allocation in C#
-
Tuesday, April 06, 2010 7:14 AM
Hi All,
I want to clarify How exactly memory is allocated/deallocated in following situations
class A
{
//Some method implementations
}
class B
{
A obja1 = new A();
public void Test()
{
A obja2 = new A();
}
}
1) When first declare class A it is allocated in Heap ?
2) when we declare class B it also goes inside heap ?
3) a reference/pointer named obja1 is created in heap which points to class A heap memory location ?
4) a reference/pointer named obja1 is created in stack which points to class A heap memory location ?
5) There will be two memory location created inside heap for class A for each object or both object points to same location.
6) When we talk about garbage collection will it be object (pointer) to heap or memory location in heap itself?
Please clarify
Thanks
All Replies
-
Tuesday, April 06, 2010 7:33 AM
Hi All ,
please take a look at this link I think that's the best article that can illustrate heap vs stack allocation and can answer all your questions
A man's dreams are an index to his greatness -
Tuesday, April 06, 2010 9:05 AM
Thanks Kamel for your reply,
I have gone through the article.It is very good article.For clarification I putted my questions
Article says object is nothing but pointer inside stack or in heap (if it is directly declared in class not in any method)
If above is true then how garbage collection is related to object itself?
Thanks
-
Tuesday, April 06, 2010 9:40 AM
ok let's try to go step by step
1) The CLR includes it's own file loader ,memory manager which is the garbage collector
2) Each compiler targetting CLR in addition to emitting IL,is required to emit full metada into every managed module (It's just a set of data tables that describe what is defined in the module ).In addition metadata also has tables indicating what the managed module references, such as imported types and their members .( Metadata allows an object's fields to be serialized into a memory block,sent to another machine ,and then deserialized,re-creating the object's state on the remote machine )
3) So as a conclusion Metadata allows the garabge collector to track the luife time of objects . For any object ,the garbage collector can determine the type of the object and , from the metadata, know wich fields within that object refer to others object .
Hope this can enlighten you . ..!
A man's dreams are an index to his greatness -
Tuesday, April 06, 2010 10:51 AM
Thanks Kamel once again
As per my understanding I reached to following answers.
1) When first declare class A it is allocated in Heap ?
Yes
2) when we declare class B it also goes inside heap ?
Yes
3) a reference/pointer named obja1 is created in heap which points to class A heap memory location ?
Yes as per article (link you given) variable goes where it is declared so it will go to heap
4) a reference/pointer named obja2 is created in stack which points to class A heap memory location ?
Yes obja2 will go inside stack same reason
5) There will be two memory location created inside heap for class A for each object or both object points to same location.
whenever we use new keyword it will create new copy of same memory inside heap so each object will point to separate memory if it is instantiated using new
6) When we talk about garbage collection will it be object (pointer) to heap or memory location in heap itself?
Actually there is no link of GC with object/pointers which resides inside stack .GC linkto memory location only where object points to inside heap
am I right ?
Thanks
-
Tuesday, April 06, 2010 2:02 PM
>> 6) When we talk about garbage collection will it be object (pointer) to heap or memory location in heap itself?
try to immagine GC as a monitor or a proxy that must take under control the situation and need to perform some verification in order to know what objects are no more needed and must be deallocated ,So it will not be neither object pointer to heap or memory location in heap itself . try to take a look at this links 1 and this one 2
Hope this help ....!
A man's dreams are an index to his greatness

