内存泄漏问题 [英] Memory leakage problem

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

问题描述



由于使用了字节数组,我的winService(用C#编写)正在消耗1.5GB(从几MB开始,然后增长)的内存.

我试过了:

Hi,

My winService (written in C#) is consuming 1.5GB (starts from few MBs and then grows) of memory due to usage of Byte arrays.

I have tried:

Bytearrays=null;



但对我没用.

请注意:我正在使用一些第三方Dll(非托管代码).

请帮忙?



but didn''t work for me.

please note: I am using some third party Dlls (unmanaged code).

Please help??

推荐答案

作为最后的选择(不推荐),尝试:
As a last resort (not recommended) try :
GC.Collect(2);


首先,是什么告诉您您正在维修的设备正在使用1.5GB的RAM?如果您在任务管理器中查找,那是在骗你.

简单来说,.NET CLR为您的应用程序保留内存,称为托管堆.您在应用程序中分配的所有对象(例如数组)均从托管堆中分配.当您释放该对象时,内存将返回到堆,而不是Windows!由于您的应用程序过去使用了大量的内存,因此.NET CLR将尝试挂在该内存上,以便将来可以快速分配任何对象.

但是,如果Windows需要返回内存,则.NET CLR非常乐意将任何未使用的Manage Heap内存返回给Windows.

任务管理器从Windows角度而不是.NET CLR角度查看内存使用情况.

因此,如果您想查看应用程序真正使用了多少内存,则必须使用PerfMon和.NET内存计数器或内存分析工具,例如
First, what''s telling you that you''re service is useing 1.5GB of RAM?? If you''re lookin in Task Manager, it''s lying to you.

In simple terms, the .NET CLR RESERVES memory for your application, called the Managed Heap. Any objects you allocate in your app, like an array are allocated out of the Managed Heap. When you free that object, the memory is returned to the Heap, NOT to Windows! Since your application used a ton of memory in the past, the .NET CLR will try to hang on to that memory so that future allocations of any objects happen quickly.

But, if Windows needs the memory back, the .NET CLR is more than happy to return any unused Manage Heap memory back to Windows.

Task Manager looks at memory usage from Windows point of view, not from the .NET CLR.

So, if you want to see how much memory you app is REALLY using, you have to use PerfMon and the .NET Memory counters or a memory profiling tool, such as these[^], or if you''re using Visual Studio 2010, you can use the built in profilers.


以下是Mehdi Gholam提到的解决方案1,它是解释和另外.

完成使用完后,立即尝试将bytearray = null设置为空.

您没有提到何时使用字节数组以及何时将其设置为Null.

另外,您可能还需要从GC收集第2代对象.请参考
发现了大对象堆 [
Along with solution 1 mentioned by Mehdi Gholam below is explanation and addition.

Try to set bytearray = null as soon as you are completing consuming it.

You have not mentioned when are you consuming bytearray and when are you setting it to Null.

In addition you also might need to Collect generation 2 objects from GC. Refer Large Object Heap Uncovered[^].

Also let us know how are you calling/passing bytearray to unmanaged code, are you passing any buffer/length. Tried to pass buffer/length.

Thanks
Rushi


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

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