0

I just wondered, if I have a variable and I assign Nothing (or Null) to it, how much memory does the variable occupy?

For example

Dim i as Integer = Nothing

Does the variable use no memory? Or the size of the integer, 4 byte? Basically I think that it means the value is not assigned and therefore there is no value anywhere in memory so it should take no memory. However there is the information stored that the variable is nothing, so this information must take memory, right? Is there a difference between .NET and native languages? Or between value and reference types?

Jens
  • 6,275
  • 2
  • 25
  • 51
  • Can you really declare a value of type `Integer` to hold `Nothing` in vb.net? – Lasse V. Karlsen May 07 '14 at 09:53
  • A value type always has a value. `Nothing` just uses the default value for that type, in this case `0`. `Dim foo As Foo = Nothing` doesn't store anything. You can use a `Nullable(Of Int32)` instead. – Tim Schmelter May 07 '14 at 09:54
  • Ok, that makes sense, but what about reference types that are nullable? – Jens May 07 '14 at 09:56
  • @jens: as commented above a reference type that is `null`/`Nothing` doesn't need to store anything on the heap since there is nothing to store. – Tim Schmelter May 07 '14 at 09:58
  • You're mixing terms, "nullable" is only used about value types when you wrap them in `Nullable(Of T)`. – Lasse V. Karlsen May 07 '14 at 09:58

2 Answers2

2

As @Tim Schmelter says in the comment, assigning a value of Nothing is the VB.NET equivalent for default(T) in C#.

An Integer will always occupy 4 bytes, 32 bits. Doesn't matter which value you put into it.

However, if you have a reference, it will occupy 4 bytes in a 32-bit process and 8 in a 64-bit process, also regardless of which value you put into it. An Integer, or System.Int32, however, is not a reference type.

Nothing here doesn't mean "no reference" (as I originally thought), just that you're assigning the default value for the type into the variable. In this case, the default for Integer is 0.

So, your variable occupies 4 bytes because it is an System.Int32. The code you have will just assign the value 0 to it.

Community
  • 1
  • 1
Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
  • It's not an "automagical conversion", `Nothing` is just the VB.NET version of `default` in C#. So `Nothing` is not only `null` in C# but also `default`. – Tim Schmelter May 07 '14 at 10:00
  • Thank you. This explains a lot. So one has to be always careful with value and reference types, I guess. – Jens May 07 '14 at 10:04
  • @Jens: i suspect that your priorities are wrong. Choose the appropriate type not that which needs 4 bytes more or less. http://c2.com/cgi/wiki?PrematureOptimization – Tim Schmelter May 07 '14 at 10:10
  • @TimSchmelter That wasn't the intention of the question. It was more a curiosity question in regards to how the technical stuff is handled that I don't see. My comment before meant that it's just another one of the value type<->reference type differences that are good to know. I am not optimizing any code in this regard. – Jens May 07 '14 at 10:14
1

Generally speaking: A reference to Null takes only the space of the reference itself on the stack. Which should be 8 byte on a 64 bit system.

In your particular case: Note the difference between boxed and unboxed values! A boxed integer is a reference to an instance of the Integer class. The instance was not created (Nothing), so it takes no space. The reference takes 8 bytes.

If you were using an unboxed value (int), it would take the space of an int (struct), which is exactly 4 bytes. Note that there is no reference involved here.

It would be an easier example to use a 'regular' class instead of the special case with Integer. For instance, consider

Object o = new Object()

This takes 8 bytes on the stack, even though o itself is empty.

mafu
  • 31,798
  • 42
  • 154
  • 247
  • `int` is a primitive struct, so you might want to avoid calling it a class. `Nullable` is also a struct. – Adam Houldsworth May 07 '14 at 09:59
  • This makes sense that the reference takes 8 bytes without anything else for the actual object. Out of curiosity, in the case of a null reference, what is the value of the pointer (it is a pointer, right? ;-))? – Jens May 07 '14 at 10:02
  • @Jens Don't forget that a reference is just a value type as well. `null` or `Nothing` on a reference type vs. a reference that references something will still take up the same space. – Adam Houldsworth May 07 '14 at 10:03
  • @Jens The value is 0. It is a pointer-ish thing, but we call it *reference*. It is more restricted than some pointer would be. – mafu May 07 '14 at 10:05
  • Ok, that sums it up quite well and answers my question. Thank you. – Jens May 07 '14 at 10:07