.NET 中集合的内存分配
这可能是一个骗局。我没有找到足够的信息。
我正在讨论 .Net 中集合的内存分配。 集合中分配的元素的内存在哪里?
List<int> myList = new List<int>();
变量 myList 分配在堆栈上,它引用在堆上创建的 List 对象。
问题是当 int 元素添加到 myList 时,它们将在哪里创建?
有人能指出正确的方向吗?
This might be a dupe. I did not find enough information on this.
I was discussing memory allocation for collections in .Net.
Where is the memory for elements allocated in a collection?
List<int> myList = new List<int>();
The variable myList is allocated on stack and it references the List object created on heap.
The question is when int elements are added to the myList, where would they be created ?
Can anyone point the right direction?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这些元素将在堆上创建。堆栈上唯一存在的东西是指向列表的指针(引用)(
List>>
是引用类型)The elements will be created on the heap. The only thing that lives on the stack is the pointer (reference) to the list (
List<>
is a reference type)这些元素也将驻留在堆中(在数组中,这就是 List 内部的工作方式)。
原则上,只有局部变量和参数在堆栈上分配,其他所有内容都在堆上分配(除非您使用诸如
stackalloc
之类的罕见事物,但您无需担心这一点)The elements will also reside in the heap (in an array, that's how List works internally).
In principle, only local variables and arguments are be allocated on the stack and everything else goes on the heap (unless you use rare things such as
stackalloc
, but you don't need to worry about that)