如何C#管理分配由新的运营商在循环中的记忆? [英] How does C# manage memory allocated by a new operator inside a loop?

查看:146
本文介绍了如何C#管理分配由新的运营商在循环中的记忆?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:

 为(i = 0;我小于10;我++)
{
   MyClass的=新MyClass的();
   //做的东西有MyClass的
}
 

问题:

  1. 如何将所有已分配通过执行10分配在这种情况下,存储器检索?
  2. 将我的内存占用是在执行的到底是什么?
  3. 在C ++中删除功能,人有更多的控制权这一点,但在这种情况下,第二次迭代,MyClass的只会把一个新的分配,并继续前进?
解决方案

由于有下一次迭代*后,每个新对象的更多引用,他们有资格进行垃圾回收。但是因为你不知道的的说,垃圾收集的事情发生,没有直接答复,内存占用可能是到底是什么。

参阅 MSDN:垃圾回收了解更多详细信息

<子> *除非构造增加了一个参考对象的地方,它会坚持下去。

For example:

for (i=0;i<10;i++)
{
   myclass = new myclass();
   // do stuff with myclass
}

Questions:

  1. How will all the memory that has been allocated by doing 10 allocations in this case be retrieved?
  2. What will my memory footprint be at the end of execution?
  3. With delete functionality in C++, one had more control over this but in this case, for the second iteration, myclass would simply take a new allocation and move on?

解决方案

Since there are no more references to each new object after the next iteration*, they're eligible to be garbage-collected. But because you don't know when said garbage collection is going to happen, there's no straight answer as to what the memory footprint might be in the end.

Refer to MSDN: Garbage Collection for more details.

* Unless the constructor adds a reference to the object somewhere it'll stick.

这篇关于如何C#管理分配由新的运营商在循环中的记忆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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