什么都没有内存?

时间:2022-05-08 23:00:23

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

我只是想知道,如果我有一个变量并且我为它分配Nothing(或Null),变量占用多少内存?

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?

变量是否不使用内存?还是整数的大小,4个字节?基本上我认为这意味着没有分配值,因此在内存中没有任何值,所以它不应该没有内存。但是存储的信息是变量什么都没有,所以这些信息必须占用内存,对吗? .NET和本地语言有区别吗?或者在价值和参考类型之间?

2 个解决方案

#1


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.

一般来说:对Null的引用仅占用堆栈上引用本身的空间。在64位系统上应该是8字节。

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.

在您的特定情况下:注意盒装值和未装箱值之间的差异!盒装整数是对Integer类实例的引用。实例未创建(Nothing),因此不占用空间。引用需要8个字节。

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.

如果您使用的是未装箱的值(int),则需要占用int(struct)的空间,这正好是4个字节。请注意,这里没有涉及参考。

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

使用“常规”类而不是Integer的特殊情况将是一个更简单的示例。例如,考虑一下

Object o = new Object()

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

这需要在堆栈上占用8个字节,即使o本身为空。

#2


2  

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

正如@Tim Schmelter在评论中所说,赋值为Nothing是C#中默认(T)的VB.NET等价物。

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

整数总是占用4个字节,32位。无论你投入哪个价值都无关紧要。

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.

但是,如果您有一个引用,它将在32位进程中占用4个字节,在64位进程中占用8个字节,无论您将哪个值放入其中。但是,Integer或System.Int32不是引用类型。

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.

这里没有任何东西并不意味着“没有参考”(正如我原先想的那样),只是你将类型的默认值分配给变量。在这种情况下,Integer的默认值为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.

因此,您的变量占用4个字节,因为它是System.Int32。您拥有的代码只会为其赋值0。

#1


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.

一般来说:对Null的引用仅占用堆栈上引用本身的空间。在64位系统上应该是8字节。

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.

在您的特定情况下:注意盒装值和未装箱值之间的差异!盒装整数是对Integer类实例的引用。实例未创建(Nothing),因此不占用空间。引用需要8个字节。

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.

如果您使用的是未装箱的值(int),则需要占用int(struct)的空间,这正好是4个字节。请注意,这里没有涉及参考。

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

使用“常规”类而不是Integer的特殊情况将是一个更简单的示例。例如,考虑一下

Object o = new Object()

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

这需要在堆栈上占用8个字节,即使o本身为空。

#2


2  

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

正如@Tim Schmelter在评论中所说,赋值为Nothing是C#中默认(T)的VB.NET等价物。

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

整数总是占用4个字节,32位。无论你投入哪个价值都无关紧要。

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.

但是,如果您有一个引用,它将在32位进程中占用4个字节,在64位进程中占用8个字节,无论您将哪个值放入其中。但是,Integer或System.Int32不是引用类型。

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.

这里没有任何东西并不意味着“没有参考”(正如我原先想的那样),只是你将类型的默认值分配给变量。在这种情况下,Integer的默认值为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.

因此,您的变量占用4个字节,因为它是System.Int32。您拥有的代码只会为其赋值0。