C#中有一种简单的方法来检测进程中的内存泄漏吗? [英] Is there a simple way in C# to detect memory leakage in a process ?

查看:223
本文介绍了C#中有一种简单的方法来检测进程中的内存泄漏吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Everyone。



基于system.diagnostics.process,我想知道是否无法监控进程的内存使用情况并确定是否内存增长不如预期?

这是为了能够在特定的内存条件下调用gc.collect()。



您对此有何看法?

非常感谢您提前。

祝你好运。

MiQi



我尝试了什么:



我目前使用过gc.Collect但是我想根据工作内存状态进行调整。

解决方案

请勿调用GC.Collect()。我向您保证,除了GC对您的应用程序的了解之外,您不了解应用程序内存习惯的行为。



垃圾收集器是非常擅长它的工作,并且是自我调整。它了解您的应用程序分配行为并调整自身以获得最佳内存管理和性能。通过调用GC.Collection(),您实际上是反对该数据并降低应用程序的性能。



此外,强制收集不会修复内存泄漏问题。找出漏洞并解决问题。 .NET Memory Profilers是这项工作的最佳工具。



此外,您在任务管理器中看到的计数器对您有用。这不是您的应用实际使用的内存量。这是您的应用程序保留的.NET CLR的多少。 .NET CLR从Windows分配内存块并将其添加到托管堆。 (这是您在任务管理器中看到的。)当您的应用程序加载并运行时,将从托管堆分配对象。当对象超出范围时,GC回收内存,因为它返回到托管堆,而不是WINDOWS。



现在,如果Windows想要内存回来,它只是要求.NET CLR尽可能地返回它,而CLR非常好地做到这一点。



所有这些都是在后台完成的,没有你搞砸它。


这样的东西:

处理myProcess = Process.GetProcessById(pid); 
var memoryUsage = Math.Max(myProcess.WorkingSet64,myProcess.PrivateMemorySize64);
var memoryMb =( int )(memoryUsage /(1024L * 1024L));


Hello Everyone.

Based on system.diagnostics.process, I wonder if it would not be possible to monitor the memory usage of a process and determine if the memory is growing not as expected ?
This is to be able to invoke the gc.collect() at specific memory conditions.

What is your view on that ?
Thank you very much in advance.
Best regards.
MiQi

What I have tried:

I have currently used gc.Collect but I would like to tune it based on the working memory status.

解决方案

DO NOT CALL GC.Collect(). I guarantee you that you don't know anything more about the behavior of your applications memory habits over and above what the GC knows about your app.

The garbage collector is very good at it's job and is self-tuning. It learns about your applications allocation behavior and tunes itself for the best memory management and performance. By calling GC.Collection(), you are actually going against that data and reducing the performance of your app.

Also, forcing a Collect does NOT fix memory leak issues. Figure out what's leaking and fix the problem instead. .NET Memory Profilers are the best tools for this job.

Also, the counters you see in Task Manager are LYING to you. That is not how much memory your app is actually using. It's how much the .NET CLR as RESERVED for your application. The .NET CLR allocates blocks of memory from Windows and adds them to the Managed Heap. (This is what you see in Task Manager.) As your application is loaded and runs, objects are allocated from the Managed Heap. As objects go out of scope, the GC reclaims that memory as it's returned back to the Managed Heap, NOT TO WINDOWS.

Now, if Windows wants memory back, it just asks the .NET CLR to return whatever it can and the CLR does this very nicely.

All of this is done in the background without you screwing around with it.


something likes this:

Process myProcess = Process.GetProcessById(pid);
var memoryUsage = Math.Max(myProcess.WorkingSet64, myProcess.PrivateMemorySize64);
var memoryMb = (int)(memoryUsage / (1024L * 1024L));


这篇关于C#中有一种简单的方法来检测进程中的内存泄漏吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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