Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
1.24/5 (3 votes)
If I make any object for example :
Emp obj= new Emp();

So who clr , system(OS) or garbage collector will allocate memory to obj object in heap ?

1. Extract 1 :Which concludes garbage collector is responsible.
From micorsoft
link -<a href="https://msdn.microsoft.com/en-us/library/f144e03t(v=vs.110).aspx">
</a>
Extract :When the application creates the next object, the garbage collector allocates memory for it in the address space immediately following the first object. As long as address space is available, the garbage collector continues to allocate space for new objects in this manner.

2.Extract 2: Which concludes clr will be responsible for memory allocation and gc will only be called if heap is full.

link -
[^]

Can anybody really tell me if gc is really responsible for allocating memory to an object.

What I have tried:

1. Extract 1 :Which concludes garbage collector is responsible.
From micorsoft
link -<a href="https://msdn.microsoft.com/en-us/library/f144e03t(v=vs.110).aspx">
</a>
Extract :When the application creates the next object, the garbage collector allocates memory for it in the address space immediately following the first object. As long as address space is available, the garbage collector continues to allocate space for new objects in this manner.

2.Extract 2: Which concludes clr will be responsible for memory allocation and gc will only be called if heap is full.

link -
[^]
Posted
Updated 24-Apr-16 0:46am
v2

1 solution

Both: the Garbage Collector is part of the CLR and works to clean up after you and free memory. It is in control of the Heap and it's free memory lists, so it also does the allocation of memory: Automatic Memory Management[^]
The system allocates the memory to start with when your app starts (and for each thread you start as they all have independent stacks which are not controlled by the GC), and the GC is responsible for it after that.
Listen to what MSDN says, not a third party like Telerik - and if you really need to know the details, I'm sure the GC is buried somewhere deep in the Reference Source[^]
 
Share this answer
 
Comments
praveen iThesisArt 24-Apr-16 6:37am    
So , when i write code :
Emp obj= new Emp();
Memory for obj will be allocated by gc?
OriginalGriff 24-Apr-16 6:50am    
Assuming Emp is a class and not a struct, yes.
If it's a struct, then it's a Value type not a Reference, so it will go on the stack, not the heap.
praveen iThesisArt 24-Apr-16 6:52am    
So if emp is a class, memory will be allocated by GC?
OriginalGriff 24-Apr-16 6:57am    
Yes.
praveen iThesisArt 24-Apr-16 9:47am    
Then who will allocate de-allocate memory in stack (value type as in case of struct) ? 'coz gc doesnt work over stack . So is it clr or compiler or processor of a system will take call on this?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900