VB.Net 新对象:它们去了哪里?

发布于 2024-12-12 02:26:21 字数 634 浏览 0 评论 0原文

每隔一段时间,在长时间的编码旅行之后,我就必须掌握一门语言的基础知识。在 VB.Net 中,我理解将变量声明为对象类型或多或少会创建对其的引用。

采取以下代码:

Dim obj1 as Object
Dim obj2 as Object
obj1 = new Object
obj2 = new Object
obj1 = obj2

一组非常基本的任务,但我试图弄清楚每一步到底发生了什么。这是我的理解:第 1 行,obj1 被声明为对象类型,编译器为 obj1 创建一个引用来保存,但是 第 1 行末尾的 obj1 引用是什么?(是 Nothingnull 同义?)第 2 行与第 1 行相同,只是变量不同。第3行,编译器在堆中为新对象分配空间,并将引用传递给obj1 1来保存。第 4 行与第 3 行相同。我一直不太清楚的部分是第 5 行。 obj1 被分配给与 obj2 相同的引用,因此它们都指向内存中的同一个对象。那么第 3 行中最初分配给 obj1 的对象会发生什么情况呢? 一旦 obj1 获取与 obj2 相同的引用,是否会导致内存中留下的第一个新对象无法访问它(或至少在垃圾回收开始之前)

Every once and a while, I have to get the basics to a language after long coding excursions. In VB.Net, I understand declarying a variable as an object type more or less creates a reference to it.

Take the following code:

Dim obj1 as Object
Dim obj2 as Object
obj1 = new Object
obj2 = new Object
obj1 = obj2

A very elementary set of tasks, but I am trying to figure out what exactly is going each step of the way. This is my understanding: Line 1, obj1 is declared as an Object type and the compiler creates a reference for obj1 to hold, but what is obj1 referencing at the end of line 1?(are nothing and null synonymous?) Line 2 is the same as line 1 except its a different variable. Line 3, the compiler allocates space in the heap for a new object and passes the reference to obj1 1 to hold. Line 4 is the same as line 3. The part that I've never been quite clear on is the the 5th line. obj1 is assigned to the same reference as obj2 so they are both pointing the same object in memory. So what happens to the object that obj1 was originally assigned in line 3? Once obj1 takes the same reference as obj2, does that leave the first new object left in memory with no way to access it (or at least until garbage collection starts)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

伴随着你 2024-12-19 02:26:21

首先是空/什么都没有的问题。在新对象创建之前,obj1 和 obj2 的值为 Nothing,但这与 C# 的 null 并不完全相同。 VB.Net 的 Nothing 更接近于 C# 的 default(T) 概念。在这种情况下,它们都是同一件事,但值得了解其中的区别,因为当您使用值类型时,nullNothing 的行为非常不同。

接下来要理解的是第 5 行中的赋值。此时,来自 obj2 变量的引用被复制到 obj1 变量。这很重要,因为它们不再是相同的引用,而是碰巧指向(引用)同一位置的两个引用。在这段代码中这很容易理解,但我见过它在更复杂的地方让人们陷入困境......假设其中一个变量是在方法中分配的。

最后一点是,是的,最初由 obj1 变量引用的对象现在符合垃圾回收的条件。您无需再担心它。

First up is null/Nothing question. The value of obj1 and obj2 before the new objects are created is Nothing, but this is not precisely the same as C#'s null. VB.Net's Nothing is a closer analog to C#'s default(T) concept. In this case it all comes to the same thing, but it's worth knowing the distinction because null and Nothing behave very differently when you're working with value types.

The next thing to understand here is the assignment in line 5. At this point, the reference from the obj2 variable is copied to the obj1 variable. This is important because they are no longer the same reference, but rather two references that happen to point (refer) to the same place. This is easy enough to understand in this code, but I've seen it trip people up in more complex places... say one of the variables is assigned in a method.

The final point is that, yes, the object originally referred to by the obj1 variable is now eligible for garbage collection. You don't need to worry about it any more.

几味少女 2024-12-19 02:26:21

第 1 行末尾的 obj1 引用什么?

在 VB 中,它指向 Nothing,这与 C# 的 null 相同(在本例中,您可以将其视为指向内存位置零)。

一旦 obj1 与 obj2 获取相同的引用,是否会留下第一个
新对象留在内存中,无法访问它(或者至少直到
垃圾收集开始)

是的,这正是发生的情况。由于垃圾收集的变幻莫测,您永远无法知道何时会实际收集它,而且实际上,无法保证它会被收集。

让 GC 担心这类事情,不要试图“帮助”它,除非你真的(老兄,我认真的意思是真的)需要。

What is obj1 referencing at the end of line 1?

In VB parlance it's pointing to Nothing, which is the same as C#'s null (which in this instance you can think of as pointing to memory location zero).

Once obj1 takes the same reference as obj2, does that leave the first
new object left in memory with no way to access it (or at least until
garbage collection starts)

Yes, that's exactly what happens. With the vagaries of garbage collection, you can never tell when this will actually be collected, and indeed, there is no guarantee that it will ever be collected.

Let the GC worry about that sort of stuff, don't try to "help" it, unless you really (and dude I seriously mean really) need to.

夏花。依旧 2024-12-19 02:26:21

第 1 行之后,obj1 将保存对 null 值的引用 - Nothing

VB.NET 中的 Nothing 与 C# 中的 null 相同,至少在引用类型方面是如此(请参阅 Joel Coehoorn 的评论)。

第 5 行 - obj1 引用的对象是 obj2 引用的某个对象。此时,第一个对象确实不再可访问,并且将被垃圾收集。

After line 1, obj1 will hold a reference to the null value - Nothing.

Nothing in VB.NET is the same as null in C#, at least when it comes to reference types (see the comment by Joel Coehoorn).

Line 5 - the object that obj1 references to is the some one that obj2 references. At this point the first object is indeed no longer accessible and will get garbage collected.

揽清风入怀 2024-12-19 02:26:21

在 .net 中,当您创建变量时,您会创建引用或值类型。所有示例都是引用变量,当程序进入函数时,它会创建保存局部变量所需的空间(即保存引用所需的空间)。


ILdasm

.method public static void  Main() cil managed
{
  .entrypoint
  .custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01 00 00 00 ) 
  // Code size       32 (0x20)
  .maxstack  1
  .locals init ([0] object obj1,
           [1] object obj2)
  IL_0000:  nop

然后在托管堆而不是“堆”上创建对象并将它们分配给本地对象。

 IL_0001:  newobj     instance void [mscorlib]System.Object::.ctor()
  IL_0006:  call       object [mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::GetObjectValue(object)
  IL_000b:  stloc.0
  IL_000c:  newobj     instance void [mscorlib]System.Object::.ctor()
  IL_0011:  call       object [mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::GetObjectValue(object)
  IL_0016:  stloc.1
  IL_0017:  ldloc.1
  IL_0018:  call       object [mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::GetObjectValue(object)
  IL_001d:  stloc.0

然后该函数返回

 IL_001e:  nop
  IL_001f:  ret
} // end of method Module1::Mai

垃圾收集器由运行时的突发奇想调用(您可以强制它的手),并将按生成收集所有无法访问的对象。

In .net when you create a variable you create a reference or a value type. All of your examples are reference variables when the program enters the function it creates the necessary space to hold the locals (ie the space necessary to hold references).


ILdasm

.method public static void  Main() cil managed
{
  .entrypoint
  .custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01 00 00 00 ) 
  // Code size       32 (0x20)
  .maxstack  1
  .locals init ([0] object obj1,
           [1] object obj2)
  IL_0000:  nop

It then create objects on the managed heap not the "heap" and assigns them to the locals.

 IL_0001:  newobj     instance void [mscorlib]System.Object::.ctor()
  IL_0006:  call       object [mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::GetObjectValue(object)
  IL_000b:  stloc.0
  IL_000c:  newobj     instance void [mscorlib]System.Object::.ctor()
  IL_0011:  call       object [mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::GetObjectValue(object)
  IL_0016:  stloc.1
  IL_0017:  ldloc.1
  IL_0018:  call       object [mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::GetObjectValue(object)
  IL_001d:  stloc.0

The function then returns

 IL_001e:  nop
  IL_001f:  ret
} // end of method Module1::Mai

The Garbage collector is invoked by the whim of the runtime( you can force its hand) and will collect all of the unreachable objects by generation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文