释放记忆 [英] Freeing Memory

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

问题描述

无论如何明确地告诉垃圾收集器你已经完成了

一个对象?在我的应用程序中,我有一个观点,我确信应该

没有对一组对象的引用(好吧,我删除我假设的是

最后一次引用它们)我希望他们成为GCed。但对于一些

的原因,我的应用程序正在泄漏内存,这是我能想到的唯一地方

这个问题可能会出错。我只想说

object.GetRidOfThisStupidObject()或GetRidOfThisStupidObject(对象)

并让它做它的事情,无论天气还是没有其他

引用它(不是我能想象它们在哪里)。不幸的是

我的应用程序也是一项服务,所以内存泄漏就不会这样做。


谢谢,

Josh Powers

解决方案

Josh,


无法做到这一点。如果你能够,它将使

编程接下来是不可能的。想象一个系统,其中引用可以随时设置为null。这将是一场噩梦。


是什么让你觉得你在泄漏记忆?如果这是任务

经理,那么这不是一个准确的数字来得出这样的结论

on。


此外,当有压力时,GC会发生。如果您的

引用被释放,那么将收集该对象。


该对象是否具有对COM对象的任何引用或不受管理的

处理?如果是这样,你应该在它上面实现IDisposable接口,

并在完成课程后调用Dispose。


最后,你是将要做一些剖析和/或调试

来弄清楚究竟发生了什么。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" yehoshua" < BA **** @ yahoo.com>在消息中写道

news:11 ********************** @ o13g2000cwo.googlegr oups.com ...

无论如何都要明确告诉垃圾收集器你用一个对象完成了吗?在我的应用程序中,我有一个观点,我确信应该没有对一组对象的引用(好吧,我删除了我认为是对它们的最后一个引用)并且我希望它们是GCed 。但是出于一些原因,我的应用程序正在泄漏内存,这是我能想到的唯一可能出错的地方。我只想说
object.GetRidOfThisStupidObject()或GetRidOfThisStupidObject(object)
并让它做它的事情,无论天气如何都没有其它的参考(不是我可以想象他们在哪里)。不幸的是,我的应用程序也是一项服务,因此内存泄漏不会发生。

谢谢,
Josh Powers



"约书亚" < BA **** @ yahoo.com>写道:

有没有明确告诉垃圾收集器
你完成了一个对象?在我的应用程序中,我有一个观点
我确信不应该引用一组
对象(我删除了我认为是对它们的最后一个
引用)并且我想要他们是GCed。


如果没有引用,那么没有变量仍然可以引用

对象。在这种情况下,您不再拥有名称对于对象,所以

无论如何你都不能把它作为参数传递给任何方法!

但由于某种原因,我的应用程序正在泄漏内存而且
是我能想到的唯一可能出错的地方。




你怎么知道你在泄漏记忆?


P.


这不是内存泄漏,如果你的对象不再生根,它会得到

收集(非确定性),通过调用GC.Collect()释放对象

是你永远不应该做的事情。它所做的只是扰乱了GC和最终确定的过程,它减慢了你的应用程序,你什么都没有得到回报。如果你不能接受这个,你应该远离像.NET这样的托管

环境。

Willy。


"约书亚" < BA **** @ yahoo.com>在消息中写道

news:11 ********************** @ o13g2000cwo.googlegr oups.com ...

无论如何都要明确告诉垃圾收集器你用一个对象完成了吗?在我的应用程序中,我有一个观点,我确信应该没有对一组对象的引用(好吧,我删除了我认为是对它们的最后一个引用)并且我希望它们是GCed 。但是出于一些原因,我的应用程序正在泄漏内存,这是我能想到的唯一可能出错的地方。我只想说
object.GetRidOfThisStupidObject()或GetRidOfThisStupidObject(object)
并让它做它的事情,无论天气如何都没有其它的参考(不是我可以想象他们在哪里)。不幸的是,我的应用程序也是一项服务,因此内存泄漏不会发生。

谢谢,
Josh Powers



Is there anyway to explicitly tell the garbage collector you are done
with an object? In my app I have a point where I am sure there should
be no references to an set of objects (well i remove what i assume is
the last reference to them) and I want them to be GCed. But for some
reason my app is leaking memory and that is the only place I can think
of that something could be going wrong. I want to just say
object.GetRidOfThisStupidObject() or GetRidOfThisStupidObject(object)
and have it do its thing, regardless of weather or not there are other
references to it (not that I can imagine where they are). Unfortunatly
my app is a service too so memory leaks just wont do.

Thanks,
Josh Powers

解决方案

Josh,

There is no way to do this. If you were able to, it would make
programming next to impossible. Imagine a system where references could be
set to null at any time. It would be a nightmare.

What gives you the impression you are leaking memory? If it is the task
manager, then that''s not an accurate number to draw this kind of conclusion
on.

Also, a GC will occur when there is pressure to do so. If your
references are released, then the object will be collected.

Does the object have any references to COM objects or to unmanaged
handles? If so, you should be implementing the IDisposable interface on it,
and calling Dispose when you are done with the class.

In the end, you are going to have to do some profiling and/or debugging
to figure out what is really going on.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"yehoshua" <ba****@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...

Is there anyway to explicitly tell the garbage collector you are done
with an object? In my app I have a point where I am sure there should
be no references to an set of objects (well i remove what i assume is
the last reference to them) and I want them to be GCed. But for some
reason my app is leaking memory and that is the only place I can think
of that something could be going wrong. I want to just say
object.GetRidOfThisStupidObject() or GetRidOfThisStupidObject(object)
and have it do its thing, regardless of weather or not there are other
references to it (not that I can imagine where they are). Unfortunatly
my app is a service too so memory leaks just wont do.

Thanks,
Josh Powers



"yehoshua" <ba****@yahoo.com> wrote:

Is there anyway to explicitly tell the garbage collector
you are done with an object? In my app I have a point
where I am sure there should be no references to an set
of objects (well i remove what i assume is the last
reference to them) and I want them to be GCed.
If there are no references, then no variables can still refer to the
object. In that case, you no longer have a "name" for the object, so
you couldn''t pass it to any method as a parameter anyway!
But for some reason my app is leaking memory and that
is the only place I can think of that something could be
going wrong.



How do you know that you are leaking memory?

P.


It''s not a memory leak, if your object is no longer rooted it will get
collected (non deterministically), freeing objects by calling GC.Collect()
is something you should never do. All it does is disturbing the process of
GC and finalization, it slows down your application and you get nothing
back. If you can''t accept this you should stay away from a managed
environment like .NET.
Willy.

"yehoshua" <ba****@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...

Is there anyway to explicitly tell the garbage collector you are done
with an object? In my app I have a point where I am sure there should
be no references to an set of objects (well i remove what i assume is
the last reference to them) and I want them to be GCed. But for some
reason my app is leaking memory and that is the only place I can think
of that something could be going wrong. I want to just say
object.GetRidOfThisStupidObject() or GetRidOfThisStupidObject(object)
and have it do its thing, regardless of weather or not there are other
references to it (not that I can imagine where they are). Unfortunatly
my app is a service too so memory leaks just wont do.

Thanks,
Josh Powers



这篇关于释放记忆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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