为什么在对象的范围完成后对象数组没有被释放? [英] Why is object array not getting deallocated after scope of the object is completed?

查看:74
本文介绍了为什么在对象的范围完成后对象数组没有被释放?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有对象'class1object',它是class2的成员。我正在初始化它,如下所示。



I have object 'class1object' that is a member of class2. I am initializing it as shown below.

class2{

class1 [] class1object1;

public class2()
{
 class1object1=new class1[1000000];
}

public void Start()
{
}
}

我在另一个函数中使用Class2object在Class3中启动并调用它100次。





I am using Class2object in another function Start in Class3 and calling it a 100 times.


Class3
{
public static void Start()
{
      for(int i=0;i<100;i++)
    {  Class2 class2object=new Class2();
      class2object.Start();
     }
}
}



最后,类4中的Start作为静态调用从class4调用。


Finally the function Start in Class3 is called from a class4 as a static call.

Class4
{
  void Start()
{
   Class3.Start();
}

}

现在我遇到的问题是数组class1object1我从未清除并在内存中不断增长。我使用Redgate分析器来观察这个。 class1object1的大小为4MB,每次运行时,此对象使用的内存增加4MB。为什么垃圾收集在某些方面没有踢?我让它运行直到它使用了大约500MB的内存。类object1中没有非托管成员甚至认为它在一个不安全的代码对象中。





任何想法或建议?



我尝试了什么:



我试过redgate profiler。我尝试在c#中使用非托管内存,但无法使用stackalloc,因为我需要将变量存储在函数中并将其传递给另一个函数。

Now the problem I am seeing is the array class1object1 i never cleared and keeps growing in memory. I used Redgate profiler to observer this. the size of class1object1 is 4MB and every run, the memory used by this object increases by 4MB. Why isnt garbage collection kicking in some point? I got it running till the it used about 500MB of memory. There are no unmanaged members in class object1 even thought its in an unsafe code object.


Any ideas or suggestions?

What I have tried:

I tried redgate profiler. I tried to use unmanaged memory in c# but couldnt do it with stackalloc because i need to store the variables in a function and pass it to another function.

推荐答案

简短问题是:这不是内存管理的工作原理。我猜,你的错误观念源于经典旧C ++的经验,其中走出函数范围确实会导致破坏。



在.NET中,你正在处理内存基于GC的管理;和对象破坏也基于这种架构。内存的破坏和回收基于可达无法访问引用的概念,这是一个非常复杂的标准。当某些引用变得无法访问时,会跟随对象,但不会立即进行。相反,它根据GC的内部行为发生;因此,您无法控制调用析构函数并回收内存的时刻。这就是为什么析构函数很少用.NET应用程序编写的原因。


例如,这里解释了这一点:垃圾收集(计算机科学) - 维基百科,免费的百科全书



另一个相关问题是可能的内存泄漏问题。首先,检测内存泄漏通常是假阳性观察。您是否从Windows任务管理器中得出结论?如果是这样,请不要相信它。



内存泄漏仍然存在,但整个概念并不是那么简单。由于非托管代码错误而导致的意外泄漏是非常不可能的。但它们可能来自一般代码设计中的错误。我在过去的答案中讨论了这个问题:

摆脱导致内存不足的公共静态列表的最佳方法

推迟循环中的变量会导致内存泄漏?

Garbage collectotion负责所有内存管理

MDI表单中的内存管理

WPF DataBinding中的内存泄漏



-SA
Short question is: this is not how memory management works. I would guess, your misconception stems from experience in classical old C++, where going out of the function scope really cause destruction.

In .NET, you are dealing with memory management based on GC; and object destruction is also based on this architecture. The destruction and reclaiming of memory is based on the concept of reachable or unreachable references, which is a pretty complication criterion. When some reference becomes unreachable, object destruction follows, but not immediately. Rather, it happens according to the GC's internal behavior; so you don't have control over the moment of time when the destructor is called and memory is reclaimed. That's why destructors are pretty rarely written in .NET applications.

This is explained, for example, here: Garbage collection (computer science) — Wikipedia, the free encyclopedia.

Another, related problem is the problem of possible memory leaks. First of all, "detection" of memory leak is often a false-positive observation. Do you draw your conclusion from Windows Task Manager? If so, don't trust it.

Memory leaks are still possible, but the whole concept is not as trivial. The "accidental" leaks, very usual due to unmanaged code bugs, are very unlikely. But they can come from mistakes in general code design. I discussed this problem in my past answers:
Best way to get rid of a public static List Causing an Out of Memory,
deferring varirable inside the loop can cuase memory leak?,
Garbage collectotion takes care of all the memory management,
Memory management in MDI forms,
Memory leak in WPF DataBinding.

—SA


这篇关于为什么在对象的范围完成后对象数组没有被释放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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