在内存中/运行时(在CLR中)如何表示C#对象引用? [英] how are C# object references represented in memory / at runtime (in the CLR)?

查看:134
本文介绍了在内存中/运行时(在CLR中)如何表示C#对象引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇如何在运行时(在.NET CLR中)在内存中表示C#对象引用.我想到的一些问题是:

I'm curious to know how C# object references are represented in memory at runtime (in the .NET CLR). Some questions that come to mind are:

  1. 对象引用占用多少内存?在类的范围和方法的范围中定义时,它是否有所不同?

  1. How much memory does an object reference occupy? Does it differ when defined in the scope of a class vs the scope of a method? Does where it live differ based on this scope (stack vs heap)?

对象引用中维护的实际数据是什么?它仅仅是指向它所引用的对象的内存地址还是它的更多内容?这是否基于在类或方法的范围内定义而有所不同?

What is the actual data maintained within an object reference? Is it simply a memory address that points to the object it refers to or is there more to it? Does this differ based on whether it is defined within the scope of a class or method?

与上述问题相同,但是这次是在讨论对引用的引用时,就像对象引用通过引用传递给方法一样. 1和2的答案如何变化?

Same questions as above, but this time when talking about a reference to a reference, as in when a object reference is passed to a method by reference. How do the answers to 1 and 2 change?

推荐答案

如果您了解C/C ++指针,则最容易理解此答案.指针只是一些数据的内存地址.

This answer is most easily understood if you understand C/C++ pointers. A pointer is a simply the memory address of some data.

  1. 对象引用应该是指针的大小,在32位CPU上通常为4个字节,在64位CPU上通常为8个字节.无论在何处定义,它都是相同的.它的生存位置确实取决于它的定义位置.如果它是一个类的字段,它将驻留在对象所属的堆中.如果它是一个静态字段,则它位于堆的特殊部分中,该部分不会进行垃圾回收.如果它是一个局部变量,它将保存在堆栈中.

  1. An object reference should be the size of a pointer, which is normally 4 bytes on a 32-bit CPU, and 8 bytes on a 64-bit CPU. It is the same regardless of where it is defined. Where it lives does depend on where it is defined. If it is a field of a class, it will reside on the heap in the object it is part of. If it is a static field, it is located in a special section of the heap that is not subject to garbage collection. If it is a local variable, it lives on the stack.

对象引用只是一个指针,可以将其可视化为包含内存中对象地址的int或long形式.无论在何处定义,它都是相同的.

An object reference is simply a pointer, which can be visualized as an int or long containing the address of the object in memory. It is the same regardless of where it is defined.

这被实现为指向指针的指针.数据是相同的-只是一个内存地址.但是,在给定的内存地址处没有对象.而是有另一个内存地址,它是对对象的原始引用.这就是允许修改参考参数的原因.通常,参数的方法完成后会消失.由于对对象的引用不是参数,因此将保留对该引用的更改.对引用的引用将消失,但该引用不会消失.这是传递参考参数的目的.

This is implemented as a pointer to a pointer. The data is the same - just a memory address. However, there is no object at the given memory address. Instead, there is another memory address, which is the original reference to the object. This is what allows a reference parameter to be modified. Normally, a parameter disappears when its method completes. Since the reference to the object is not a parameter, then changes to this reference will remain. The reference to a reference will disappear, but not the reference. This is the purpose for passing reference parameters.

您应该知道的一件事是,值类型存储在适当的位置(没有内存地址,而是将它们直接存储在内存地址所在的位置-参见#1).将它们传递给方法时,将创建一个副本,并在该方法中使用该副本.通过引用传递它们时,将传递一个内存地址,该地址在内存中定位值类型,从而可以对其进行更改.

One thing you should know, value types are stored in place (there is no memory address, instead they are stored directly where the memory address would be - See #1). When they are passed to a method, a copy is made and that copy is used in the method. When they are passed by reference, a memory address is passed which locates the value type in memory, allowing it to be changed.

正如dlev所指出的那样,这些答案不是一成不变的规则,因为没有规则说这是必须的. .NET可以随意实现这些问题.不过,这是最有可能实现它的方法,因为这是Intel CPU在内部工作的方式,因此使用任何其他方法都可能效率低下.

As dlev pointed out, these answers are not the hard and fast rule, since there is no rule that says this is how it must be. .NET is free to implement these questions however it wants. This is the most likely way to implement it though, as this is how the Intel CPU's work internally, so using any other method would likely be inefficient.

希望我没有让您感到困惑,但是请随时询问您是否需要澄清.

Hope I didn't confuse you too much, but feel free to ask if you need clarification.

这篇关于在内存中/运行时(在CLR中)如何表示C#对象引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆