重新分配对象时会发生什么?

时间:2022-08-21 15:19:24

When an object is instantiated with:

使用以下实例化对象时:

Foo objFoo = new Foo();

What happens to it's memory allocation when it's reference, objFoo, is reassigned?

当它的引用objFoo被重新分配时,它的内存分配会发生什么?

objFoo = new Foo();

What about when assigned to null?

分配给null时怎么办?

objFoo = null

3 个解决方案

#1


5  

What happens to the currently using memory when we assign another instance to the object.

当我们将另一个实例分配给对象时,当前正在使用的内存会发生什么。

objFoo is a variable, not an object.

objFoo是一个变量,而不是一个对象。

objFoo = new Foo(); creates a new object (new Foo()) and assigns it to the objFoo variable.

objFoo = new Foo();创建一个新对象(new Foo())并将其分配给objFoo变量。

If you invoke objFoo = new Foo(); and there are no other variables referring to the same old object, then the old object will be eligible to be garbage collected.

如果你调用objFoo = new Foo();并且没有其他变量引用同一个旧对象,那么旧对象将有资格被垃圾收集。

Garbage collection does not happen immediately, but when it happens it would free the memory used by the old object.

垃圾收集不会立即发生,但是当它发生时,它将释放旧对象使用的内存。

Take a look at this MSDN reference for more information about garbage collection.

有关垃圾回收的更多信息,请查看此MSDN参考。

What happens to the memory when null is assigned to the object. ie., objFoo = null

将null分配给对象时,内存会发生什么。即。,objFoo = null

Same as my previous note. If no other variable references the old object, the object will be eligible for garbage collection.

与我之前的笔记相同。如果没有其他变量引用旧对象,则该对象将有资格进行垃圾回收。

In the first case i can access the new object where as in the second case, accessing value will give me null reference Exception.

在第一种情况下,我可以访问新对象,在第二种情况下,访问值将给我空引用异常。

The variable objFoo now does not refer to anything, that is why you are getting the exception when you try to access the object that it is referencing.

变量objFoo现在不引用任何内容,这就是当您尝试访问它引用的对象时获取异常的原因。

#2


2  

When you remove the last reference to an object (in this case by setting the variable to either a new object or null, it's basically the same thing) it gets marked as being available to be garbage collected.

删除对象的最后一个引用(在这种情况下,通过将变量设置为新对象或null,它基本上是相同的东西)它被标记为可用于垃圾收集。

It doesn't mean the memory gets freed straight away (though that may happen) so if you had some non-managed way of accessing the memory it would still show as being "there" for a while.

这并不意味着内存会被立即释放(尽管可能会发生),所以如果你有一些非托管方式来访问内存,它仍会显示为“有一段时间”。

At some point (which you can't determine) the memory is freed.

在某些时候(你无法确定),内存被释放。

If you had other variables/lists etc. referencing the object then it's not eligible for garbage collection.

如果您有其他变量/列表等引用该对象,则它不符合垃圾回收的条件。

#3


1  

At start, objFoo contains the reference to the first object's address in memory. When you create and assign the second object new Foo(), it creates a new object in memory and puts the new reference in objFoo. The old object, if not anymore referenced elsewhere in your code, becomes then a candidate to the garbage collector. You don't "override" the memory, you just use another memory range for the new object.

在开始时,objFoo包含对内存中第一个对象地址的引用。当您创建并分配第二个对象new Foo()时,它会在内存中创建一个新对象并将新引用放在objFoo中。旧代码,如果不再在代码中的其他位置引用,则成为垃圾收集器的候选对象。您没有“覆盖”内存,只需为新对象使用另一个内存范围。

For your second point, it's almost the same. When you assign the null reference to the objFoo variable, the previous object will become a candidate to the GC if no other variables reference it. Since your variable actually do reference a null value and not a valid Foo object referenced in memory, you get this error.

对于你的第二点,它几乎是一样的。将空引用分配给objFoo变量时,如果没有其他变量引用它,则前一个对象将成为GC的候选对象。由于您的变量实际上引用了一个空值而不是内存中引用的有效Foo对象,因此会出现此错误。

In any case, you don't know exactly when the GC is gonna free the memory.

在任何情况下,你都不知道GC什么时候会释放内存。

#1


5  

What happens to the currently using memory when we assign another instance to the object.

当我们将另一个实例分配给对象时,当前正在使用的内存会发生什么。

objFoo is a variable, not an object.

objFoo是一个变量,而不是一个对象。

objFoo = new Foo(); creates a new object (new Foo()) and assigns it to the objFoo variable.

objFoo = new Foo();创建一个新对象(new Foo())并将其分配给objFoo变量。

If you invoke objFoo = new Foo(); and there are no other variables referring to the same old object, then the old object will be eligible to be garbage collected.

如果你调用objFoo = new Foo();并且没有其他变量引用同一个旧对象,那么旧对象将有资格被垃圾收集。

Garbage collection does not happen immediately, but when it happens it would free the memory used by the old object.

垃圾收集不会立即发生,但是当它发生时,它将释放旧对象使用的内存。

Take a look at this MSDN reference for more information about garbage collection.

有关垃圾回收的更多信息,请查看此MSDN参考。

What happens to the memory when null is assigned to the object. ie., objFoo = null

将null分配给对象时,内存会发生什么。即。,objFoo = null

Same as my previous note. If no other variable references the old object, the object will be eligible for garbage collection.

与我之前的笔记相同。如果没有其他变量引用旧对象,则该对象将有资格进行垃圾回收。

In the first case i can access the new object where as in the second case, accessing value will give me null reference Exception.

在第一种情况下,我可以访问新对象,在第二种情况下,访问值将给我空引用异常。

The variable objFoo now does not refer to anything, that is why you are getting the exception when you try to access the object that it is referencing.

变量objFoo现在不引用任何内容,这就是当您尝试访问它引用的对象时获取异常的原因。

#2


2  

When you remove the last reference to an object (in this case by setting the variable to either a new object or null, it's basically the same thing) it gets marked as being available to be garbage collected.

删除对象的最后一个引用(在这种情况下,通过将变量设置为新对象或null,它基本上是相同的东西)它被标记为可用于垃圾收集。

It doesn't mean the memory gets freed straight away (though that may happen) so if you had some non-managed way of accessing the memory it would still show as being "there" for a while.

这并不意味着内存会被立即释放(尽管可能会发生),所以如果你有一些非托管方式来访问内存,它仍会显示为“有一段时间”。

At some point (which you can't determine) the memory is freed.

在某些时候(你无法确定),内存被释放。

If you had other variables/lists etc. referencing the object then it's not eligible for garbage collection.

如果您有其他变量/列表等引用该对象,则它不符合垃圾回收的条件。

#3


1  

At start, objFoo contains the reference to the first object's address in memory. When you create and assign the second object new Foo(), it creates a new object in memory and puts the new reference in objFoo. The old object, if not anymore referenced elsewhere in your code, becomes then a candidate to the garbage collector. You don't "override" the memory, you just use another memory range for the new object.

在开始时,objFoo包含对内存中第一个对象地址的引用。当您创建并分配第二个对象new Foo()时,它会在内存中创建一个新对象并将新引用放在objFoo中。旧代码,如果不再在代码中的其他位置引用,则成为垃圾收集器的候选对象。您没有“覆盖”内存,只需为新对象使用另一个内存范围。

For your second point, it's almost the same. When you assign the null reference to the objFoo variable, the previous object will become a candidate to the GC if no other variables reference it. Since your variable actually do reference a null value and not a valid Foo object referenced in memory, you get this error.

对于你的第二点,它几乎是一样的。将空引用分配给objFoo变量时,如果没有其他变量引用它,则前一个对象将成为GC的候选对象。由于您的变量实际上引用了一个空值而不是内存中引用的有效Foo对象,因此会出现此错误。

In any case, you don't know exactly when the GC is gonna free the memory.

在任何情况下,你都不知道GC什么时候会释放内存。