views:

159

answers:

4
private button btnNew=new button();
btnNew.addclickhandler(this);
private DataGrid grid;
private void onClick(event click) {grid=new DataGrid();}

Hello ,I write a code like this sample ,I want to know that every time a user click on btnNew,what is going on in heap and stack memory?for example does a new block in heap memory assign to this grid?Or an older block remove and this new block replace it ?Or an older block remains in heap memory and also new block assign to it.

Is this block of code allocate a huge memory on several click? *The DataGrid could be replace with any component I want to know about this type of new statement usage and memory allocation * sorry, for my bad english!

.

A: 

Using the new statement allocates memory on the heap. In general new memory is allocated. If the btnNew pointer was the only pointer associated with a button object, it should become a target to the garbage collector. So the memory will be freed again. For multiple clicks the same will happen, but you should be aware that the garbage collector does not work in real time. So in a high-frequency loop allocating large objects - "new" can become a problem in c#.

FFox
Using `new` with a reference type allocates memory on the heap. If you `new` a struct it is allocated on the stack.
Brian Rasmussen
@Brian: "new" on a value type doesn't allocate storage in the first place, and the storage for a value type need not be on the stack.
Eric Lippert
@Eric: You're right my wording wasn't specific enough. Thanks.
Brian Rasmussen
@Eric: What I meant to say is that `new` doesn't necessarily allocate memory on the heap. Local value types are stored on the stack and in that case `new` just invokes the constructors as I understand it.
Brian Rasmussen
A: 

if button is a class (ref type), it is allocated on the heap. (value-types are allocated on the stack in the current CLR implementation, unless they are contained by another reference type or captured in a closure - in which case they are on the heap.).

The garbage collector has pre-allocated segments of memory of different sizes corresponding to generations 0, 1 and 2. WHen you new up an object, it is allocated in generation 0. And this allocation is really fast, since it is just moving a pointer by a delta = size of the object. The CLR clears the values in the object to default values as a prereq step before executing the ctor.

Periodically all threads are paused and the garbage collector runs. It creates a graph of reachable objects by traversing "roots". All unreachable objects are discarded. The generation segments are moved around / compacted to avoid fragmentation. Gen 0 is collected more frequently than 1 and so on... (since Gen-0 objects are likely to be short-lived objects). After the collection, the app threads resume.

For more on this, refer to documents explaining the garbage collector and generations. Here's one.

Gishu
Value types are not allocated on the stack. A correct statement is that value types that are local variables or temporary storages such that the local variables are not closed-over outer locals of an anonynmous method or lambda expression or hoisted locals of an iterator block are stored on the stack in the Microsoft implementation of C# running on the desktop CLR. Value type variables that are fields of reference types or hoisted locals are stored on the heap. There is no requirement that *any* implementation store anything on "the stack". The stack is an implementation detail.
Eric Lippert
And of course, at runtime, value types might not be allocated at all. Value types that are equal to or smaller to the register size could be enregistered, meaning that they are stored on neither the heap nor the stack.
Eric Lippert
@EricL - thanks for the updates to my answer. I wasn't precise (about contained value types not being on the stack) in my answer because the original question didn't indicate that the OP was interested in value types. However it can be misconstrued by another reader... so editing my post to include this. Thanks.
Gishu
+4  A: 

what is going on with respect to heap and stack memory?

since the button is reference type and declared in global will be allocated in heap, not in stack.

Is a new block in heap memory assigned to this button?

yes if memory is available, else unreached references will be removed and this one is allocated

Does this block of code allocate a large amount of memory on a single click?

No, but it will, if you add thousand buttons

Check out this cool article Memory in .NET - what goes where by Jon Skeet to understand the memory internals better..

Cheers

Ramesh Vel
care to give a reason for downvote??
Ramesh Vel
It seem that I have given this down vote, but I can't recall it doing this, because I hadn't read your post. Perhaps a careless click has caused this. Please edit your answer, so I can remove this down vote. Even better, after reading your answer I believe you deserve an up vote.
Steven
@Steven, edited.. :)
Ramesh Vel
+1  A: 

This is a huge topic. This is akin to asking "you type www.amazon.com into a browser. What happens next?" To answer that question fully you have to explain the architecture of the entire internet. To answer your question fully you have to understand the entire memory model of a modern operating system.

You should start by reading about the fundamentals of memory and garbage collection, here:

http://msdn.microsoft.com/en-us/library/ee787088.aspx

and then ask more specific questions about things you don't understand.

Eric Lippert
@EricL-thank u ,I read this article and it was useful for me.I forgot to mention that the object is ref type.
Mina