泛型的内存管理 [英] Generics memory management

查看:85
本文介绍了泛型的内存管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有关于问题是内存的强类型泛型管理

 名单,其中,INT> ints1 =新的名单,其中,INT>();
ints1.Add(1); ints1.Add(2); ints1.Add(3);

INT [] INTS2 =新INT [10]();
ints2.Add(6); ints2.Add(7); ints2.Add(8);
 

问题1:我presume为ints1被初始化一个新的关键字(新名单,其中,INT>())它成为一个引用类型。哪里值1,2,3存储在内存中(它们存储在堆栈或堆)?

问题2:我知道名单℃之间的事情; INT> INT [] 名单,其中,INT> 可以扩展它的大小在任何规模的运行时间,这在编译时被限制在 INT [] 来。所以,如果值1,2,3和存储在堆栈中,如果一个新的项目被添加到名单,其中,INT> 说4,它不会是连续的第3对,所以如何将ints1知道4的内存位置?

解决方案
  

我presume为ints1被初始化一个新的关键字新名单,其中,INT>()就成了引用类型

这presumption不正确。您可以使用新关键字的值类型呢!

  INT X =新INT();
 

使用新不会使任何一个引用类型。你可以用新引用类型或值类型。什么是新的表示的是,的存储将被分配并构造将被称为的。

在使用新上的值型的情况下,所分配的存储是临时存储。的引用,临时存储被传递到构造,然后将现在初始化结果复制到它的最终目的地,如果有一个。 (新,通常使用与分配,但它不是必须的。)

在引用类型的情况下,存储分配两次:长期存储分配给的实例的和短期存储分配给的引用长期储存实例的。参考传递给构造函数,它初始化长期储存。参考随后从短期贮存复制到其最终目的地,如果有一个

是什么让名单,其中,INT> 引用类型是名单,其中,T> 被声明为一类。

  

是1,2,3存储在内存中的值在哪里(它们存储在堆栈或堆)?

我们一直在努力使内存管理器,可以让你不关心那里的东西都存储。值存储在任一短期记忆池(实施为堆栈或寄存器)或一长期存储器池(实施为一个垃圾收集堆)。存储是取决于价值的知寿命的分配。如果该值被称为是短命然后其存储分配在短期池。如果该值的没有的已知短命的,那么它必须被分配在长期的游泳池。

在1,2,3的名单可以长生不老拥有;我们不知道该列表是否是要经受住当前激活帧。因此,存储器来存储1,2,3被分配在长期池

不要相信值类型总是分配在栈上的谎言。显然,这不可能是真实的,因为这样一类或包含数字阵列无法当前栈帧生存!值类型的分配上是有道理的,他们已知的生命周期的游泳池。

  

名单,其中,INT> 可以扩展它的大小在任何规模的运行时不像 INT []

正确的。这是教育怎么看名单,其中,T> 做到这一点。它简单地分配T的阵列大于它的需要。如果它发现它猜对过小,它分配一个新的,更大的数组并复制旧数组内容到新的。 A 名单,其中,T>!大约是一帮阵副本只是一个方便的包装

  

如果1,2,3存储在堆栈中的值,和一个新的第4项被添加到列表中,那么这将是不连续的前三个

正确的。这就是为什么存储值1,2,3不被分配在堆栈上的原因之一。存储实际上是在堆中分配的数组。

  

所以如何将列表了解项目4的内存位置?

列表分配一个数组,它是的太大的。当您添加一个新的项目,它坚持它变成了过于大阵未使用的空间。当阵列用完的​​空间,它分配一个新的数组。

I have question regarding how memory is managed for strong type Generics

List<int> ints1 = new List<int>();
ints1.Add(1); ints1.Add(2); ints1.Add(3);

int[] ints2 = new int[10]();
ints2.Add(6); ints2.Add(7); ints2.Add(8);

Question 1: I presume as ints1 is initialised with a new keyword (new List<int>()) it becomes a reference type. Where are the values 1,2,3 are stored in memory (are they stored in stack or on heap)?

Question 2: I know of a thing between List<int> and int[], List<int> can scale its size to any size at run time, which is restricted in int[] at compile time. So, if the values 1,2,3 and stored in stack, if a new item is added to List<int> say 4, it wouldn't be continuous to the first 3 right, so how will ints1 know the memory location of 4?

解决方案

I presume as ints1 is initialised with a new keyword new List<int>() it becomes a reference type

This presumption is incorrect. You can use the "new" keyword on a value type too!

int x = new int();

Using "new" does not make anything a reference type. You can use "new" with reference types or value types. What "new" indicates is that storage is going to be allocated and a constructor is going to be called.

In the case of using "new" on a value type, the allocated storage is temporary storage. A reference to that temporary storage is passed to the constructor, and then the now-initialized result is copied to its final destination, if there is one. ("new" is usually used with an assignment but it need not be.)

In the case of a reference type, storage is allocated twice: long-term storage is allocated for the instance and short-term storage is allocated for the reference to long-term storage of the instance. The reference is passed to the constructor, which initializes the long-term storage. The reference is then copied from short-term storage to its final destination, if there is one.

What makes List<int> a reference type is that List<T> is declared as a class.

Where are the values 1,2,3 are stored in memory (are they stored in stack or on heap)?

We've worked hard to make a memory manager that lets you not care where things are stored. Values are stored in either a short-term memory pool (implemented as the stack or registers) or a long-term memory pool (implemented as a garbage-collected heap). Storage is allocated depending on the known lifetime of the value. If the value is known to be short-lived then its storage is allocated on the short-term pool. If the value is not known to be short-lived then it must be allocated on the long-term pool.

The 1, 2, 3 owned by the list could live forever; we do not know whether that list is going to outlive the current activation frame or not. Therefore the memory to store the 1, 2, 3 is allocated on the long-term pool.

Do not believe the lie that "value types are always allocated on the stack". Obviously that cannot be true because then a class or array containing a number could not survive the current stack frame! Value types are allocated on the pool that makes sense for their known lifetime.

List<int> can scale its size to any size at runtime unlike int[]

Correct. It is educational to see how List<T> does that. It simply allocates an array of T larger than it needs. If it discovers that it guessed too small, it allocates a new, larger array and copies the old array contents to the new one. A List<T> is just a convenient wrapper around a bunch of array copies!

if the values 1,2,3 were stored in stack, and a new item 4 is added to the list, then it wouldn't be continuous to the first three.

Correct. That's one reason why the storage for values 1, 2, 3 are not allocated on the stack. The storage is actually an array allocated on the heap.

so how will the list know the memory location of item 4?

The list allocates an array that is too big. When you add a new item, it sticks it into unused space in the too-big array. When the array runs out of room, it allocates a new array.

这篇关于泛型的内存管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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