Size Of Null Object in C#

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

What the size of an object when it is instantiated to null?

I am placing a number of strongly typed objects into a collection. I want to keep the amount of memory that is utilized by each the objects to a minimum. Each object contains a field that is a reference type. I am trying to decide whether or not to instantiate this reference type when the object is first created.

How is memory allocated with a new object versus an object that set to null? If it matters, I am using C#.

Thanks,
Mark
 
Mark,

The size of an object *reference* is the system pointer size - 32 bits
on current .NET frameworks for Win32 but possibly 64 bits in the
future. That's regardless whether it's set to null or references an
object.

There's no such thing as a "null object".



Mattias
 
Mattias,

Thanks for the information about the pointer size. I understand from you that the pointer is the same size whether null or new is used to instantiate the object. But if the object contains a hashtable field, wouldn't the overall memory usage be less for an object instantiated to null.

ie:

Validator v = null; -vs - Validator v = new Validator();

Or maybe differently stated. When an object is instantiated to null, are its reference fields also instantiated?

With a collection containing many of these objects, would null provide better memory usage?

Thanks again,
Mark
 
Validator v = null;
sort of acts like a placeholder... it says that there's going to be a
validator v but not just yet

Validator v = new Validator();
on the other hand is an instance and would occupy the size on heap based on
its datamembers (fields, methods, properties etc)

yes its better memory usage to use null than instances if you dont use them
all at once...
but consider something like ArrayList... you add an object instance only
when you need to ... you can add any number of instances...its dynamic so
you dont have to worry bout resizing..
that i would say is the best when it comes to memory management

hope this helps...
 
Back
Top