VB.Net 新对象:它们去了哪里?
每隔一段时间,在长时间的编码旅行之后,我就必须掌握一门语言的基础知识。在 VB.Net 中,我理解将变量声明为对象类型或多或少会创建对其的引用。
采取以下代码:
Dim obj1 as Object
Dim obj2 as Object
obj1 = new Object
obj2 = new Object
obj1 = obj2
一组非常基本的任务,但我试图弄清楚每一步到底发生了什么。这是我的理解:第 1 行,obj1 被声明为对象类型,编译器为 obj1 创建一个引用来保存,但是 第 1 行末尾的 obj1 引用是什么?(是 Nothing
和 null
同义?)第 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先是空/什么都没有的问题。在新对象创建之前,obj1 和 obj2 的值为
Nothing
,但这与 C# 的null
并不完全相同。 VB.Net 的Nothing
更接近于 C# 的default(T)
概念。在这种情况下,它们都是同一件事,但值得了解其中的区别,因为当您使用值类型时,null
和Nothing
的行为非常不同。接下来要理解的是第 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#'snull
. VB.Net'sNothing
is a closer analog to C#'sdefault(T)
concept. In this case it all comes to the same thing, but it's worth knowing the distinction becausenull
andNothing
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.
在 VB 中,它指向
Nothing
,这与 C# 的null
相同(在本例中,您可以将其视为指向内存位置零)。是的,这正是发生的情况。由于垃圾收集的变幻莫测,您永远无法知道何时会实际收集它,而且实际上,无法保证它会被收集。
让 GC 担心这类事情,不要试图“帮助”它,除非你真的(老兄,我认真的意思是真的)需要。
In VB parlance it's pointing to
Nothing
, which is the same as C#'snull
(which in this instance you can think of as pointing to memory location zero).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.
第 1 行之后,
obj1
将保存对null
值的引用 -Nothing
。VB.NET 中的
Nothing
与 C# 中的null
相同,至少在引用类型方面是如此(请参阅 Joel Coehoorn 的评论)。第 5 行 -
obj1
引用的对象是obj2
引用的某个对象。此时,第一个对象确实不再可访问,并且将被垃圾收集。After line 1,
obj1
will hold a reference to thenull
value -Nothing
.Nothing
in VB.NET is the same asnull
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 thatobj2
references. At this point the first object is indeed no longer accessible and will get garbage collected.在 .net 中,当您创建变量时,您会创建引用或值类型。所有示例都是引用变量,当程序进入函数时,它会创建保存局部变量所需的空间(即保存引用所需的空间)。
ILdasm
然后在托管堆而不是“堆”上创建对象并将它们分配给本地对象。
然后该函数返回
垃圾收集器由运行时的突发奇想调用(您可以强制它的手),并将按生成收集所有无法访问的对象。
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
It then create objects on the managed heap not the "heap" and assigns them to the locals.
The function then returns
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.