.dispose()方法是否有任何作用? [英] Does the .dispose() method do anything at all?

查看:132
本文介绍了.dispose()方法是否有任何作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前一天,当我意识到自己对清理资源一无所知时,我正在尝试一些方法来消除应用程序中的某些内存泄漏.我做了一些研究,并希望仅调用.dispose()就能解决我的所有问题.我们的数据库中有一个表,其中包含约65,000条记录.显然,当我从数据适配器填充数据集时,内存使用率可能会很高.当我在数据集上调用dispose方法时,我惊讶地发现没有一个内存被释放.为什么会这样呢?清除数据集也无济于事.

I was experimenting with ways to get rid of some memory leaks within my application the other day when I realized that I know virtually nothing about cleaning up my resources. I did some research, and hoped that just calling the .dispose() would solve all of my problems. We have a table in our database that contains about 65,000 records. Obviously when I fill my dataset from the data adapter, the memory usage can get pretty high. When I called the dispose method on the dataset, I was surprised to find out that NONE of the memory got released. Why did this happen? Clearing the dataset doesn't help either.

推荐答案

IDisposable ,因此处置尽管在某些情况下可以使用a>来降低内存压力,但并不是用来减少内存压力的,而是用于确定性清除的.

IDisposable and thus Dispose is not used to reduced memory pressure, although in some cases it might, but instead used for deterministic cleanup.

考虑到这一点,您将构造一个对象,该对象保持与数据库服务器的活动和开放连接.此连接使用您的计算机和服务器上的资源.

Consider this, you construct an object that maintains an active and open connection to your database server. This connection uses resources, both on your machine, and the server.

您当然可以在处理完对象后将其保留为对象,最终它会被垃圾收集器拾取,但是假设您要确保至少释放了资源,因此完成连接后,连接将关闭.这是 IDisposable 的地方.

You could of course just leave the object be when you're done with it, and eventually it'll get picked up by the garbage collector, but suppose you want to make sure at least the resources gets freed, and thus the connection closed, when you're done with it. This is where IDisposable.Dispose comes into play.

它用于清理对象管理的资源.

It is used to clean up resources managed by the object.

但是,它不会释放分配给对象的托管内存.这仍然留给垃圾收集器,它将在以后的某个时间开始执行.

It will, however, not free the managed memory allocated to the object. This is still left to the garbage collector, that will kick in at some later time to do that.

您实际上是否有内存问题,还是只是在任务管理器或类似工具中查看内存使用情况,然后说有点高"?

Do you actually have a memory problem, or do you just look at the memory usage in Task Manager or similar and go "that's a bit high."?

如果是后者,那么您应该暂时保留它.如果您的可用内存较少,.NET将更频繁地运行垃圾回收,因此,除非您遇到内存溢出情况,或者可能怀疑您很快就会出现内存溢出情况,否则您将不会有任何问题

If the latter, then you should just leave it be for now. .NET will run garbage collection more often if you have less memory available, so unless you're in a situation where you get, or might suspect you will get soon, a memory overflow condition, you're probably not going to have any problems.

让我解释一下减少跑步次数"的意思.

Let me explain what I mean by "run less often".

如果您的计算机中有8GB的内存,并且仅运行Windows和记事本,则该内存中的大部分将可用.现在,当您运行程序时,即使将较小的数据块加载到内存中,您也可以长时间这样做,并且内存使用量将稳定增长.确切地说,GC何时启动并尝试减少您的内存占用,我不知道,但是我几乎可以向您保证,您会怀疑为什么它会变得如此之高.

If you have 8GB of memory in your machine, and only have Windows and Notepad running, most of that memory will be available. When you now run your program, even if it loads minor data blocks into memory, you can keep doing that for a long time, and memory usage will steadily grow. Exactly when the GC will kick in and try to reduce your memory footprint I don't know, but I can almost guarantee you that you will wonder why it gets so high.

仅出于争论的目的,请说您的程序最终将使用2GB的内存.

Let's just for the sake of the argument say that your program will eventually use 2GB of memory.

现在,如果您在可用内存较少的计算机上运行程序,GC将更频繁地出现,并且将以较低的限制开始运行,这可能使内存使用量保持在500MB以下,甚至更低.

Now, if you run your program on a machine that has less memory available, GC will occur more often, and will kick in on a lower limit, which might keep the memory usage below 500MB or possibly even less.

这里要注意的重要部分是,为了让您准确了解实际应用程序需要多少内存,您不能依靠任务管理器或类似方法来对其进行测量,您需要更有针对性.

The important part to note here is that in order for you to get an accurate picture of how much memory application actually requires, then you can't rely on Task Manager or similar ways to measure it, you need something more targetted.

这篇关于.dispose()方法是否有任何作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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