Groups | Blog | Home
all groups > c# > june 2004 >

c# : Memory costs for declared but not used objects


William Stacey [MVP]
6/9/2004 8:27:10 PM
No. The type declaration allocates four bytes to hold the ref for the
reference type (a few more bytes may be needed.) The "new" creates the
reference type in the heap and stores the pointer to it in the var. Without
the "new" your not instanciating an object.
private string s = null; // an int set to zero.
private string s = "win"; // the int is set to point to string object
instance in the heap (actual amount, not sure. I think length int, plus
chars, plus null terminated.)


--
William Stacey, MVP

[quoted text, click to view]
Barbara
6/9/2004 11:29:20 PM
Hello
If I declare an object, ie "private TextBox tb;" and then do not use it in
program, is it
using same memory as "private TextBox tb=new TextBox();"?


John Baro
6/10/2004 10:29:57 AM

[quoted text, click to view]

No
It is allocated a "slot" on the stack which points to "null" on the heap.
When you instantiate it ( = new TextBox();) it creates a new object
(TextBox) on the heap and points to it.

HTH
JB

Michael S
6/10/2004 12:02:20 PM

[quoted text, click to view]

... but as the reference is private the optimizer will simply remove the
declaration, hence there is no cost at all.

Best Regards
- Michael S

John Baro
6/11/2004 8:57:32 AM

[quoted text, click to view]

IF the ref is not instantiated later on.

[quoted text, click to view]

AddThis Social Bookmark Button