天真的记忆管理问题。 [英] Naive memory management questions.

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

问题描述



我有一个asp.net,c#,sql server网站,这些卷开始很快就会上涨。我被要求考虑改善应用程序的内存使用量。我之前没有看过这些,所以这些可能是

天真的问题:


1.如果我每次都需要关闭sql连接关闭或

处理数据集,命令或任何其他对象?


2.我应该使用数据集的数据集吗?


3.是否有任何其他物品我应该关闭或处理,或者只是回复垃圾收集器?


4。我如何检查内存泄漏以及最有可能的
罪魁祸首?


任何能让我加快速度的建议都会很大/>
赞赏。


John South

Pangbourne
www.WhereCanWeGo.com

Hi
I''ve an asp.net, c#, sql server website and the volumes are starting to
rise quickly. I''ve been asked to look at improving the memory usage of
the application. I''ve not looked at this before so these are probably
naive questions:

1. If I close the sql connection every time do I need to close or
dispose of the dataset, command or any other objects?

2. Should I use data adaptor instaed of dataset?

3. Are there any other objects that I should close or dispose, or just
reply on the garbage collector?

4. How can I check for memory leaks and what are the most likely
culprits?

Any suggestions to get me up to speed in this would be greatly
appreciated.

John South
Pangbourne
www.WhereCanWeGo.com

推荐答案

我的一些指导方针我试图遵循这些:


1.使用连接池。通过这种方式,您可以控制

连接数量而不会牺牲您的

性能


2.获取只有您需要的数据。


3.只要有可能,请使用阅读器而不是数据集,ike

下降。


4.尽量不要将对象放在session或viewstate中,因为这会消耗

内存


5.一般情况下你应该只处理对象使用非托管

资源(如连接)。 DataSet不使用非托管资源

,实际上它们的设计是以断开连接的方式使用。


6.对某些数据使用某种缓存经常被要求并且不会改变
。在像你这样的情况下这可能听起来很愚蠢,但

认为你只能存储一份你的数据副本,而不必为每个实例从服务器上获取它们。


垃圾收集器一般不会允许内存泄漏(仅在

非正常资源未正确释放的情况下)但是它

可能会受到内存碎片的影响。 .NET垃圾收集器为此提供了
配置,但重启工作服务将始终是好的。有关MSDN垃圾收集器的文章很好


ms-help://MS.MSDNQTR.2005APR.1033/dndotnet/html/dotnetgcbasics.htm#dotnetgcbasics_topic3


希望这些会有所帮助。

Some guidelines that I ''m trying to follow are these:

1. Use connection pooling. This way you can have a control on the
number of connections open without sacrificing much from your
performance

2. Fetch only the data you need.

3. Use a reader instead of a dataset whenever this is possible, ike
drop downs.

4. Try not to put objects in session or viewstate as this consumes
memory

5. In general you should dispose only objects that use unmanaged
resources (like connections). DataSets do not use unmanaged resources
and in fact they were designed to be used in a disconnected manner.

6. Use some kind of caching for data that is requested often and does
not change. This may sound stupid in situations like yours, but
consider that you can have only one copy of your data stored, without
having to fetch them from the server for every instance.

The garbage collector in general will not permit memory leaks ( only in
case of unmanaged resources that are not deallocated properly ) but it
can suffer from memory fragmentation. .NET garbage collector has
provisioning for that but a restart of the worker service will always
do good. There is a nice article about the garbage collector in MSDN

ms-help://MS.MSDNQTR.2005APR.1033/dndotnet/html/dotnetgcbasics.htm#dotnetgcbasics_topic3

Hope these will help.




" Tasos Vogiatzoglou" <电视***** @ gmail.com>在消息中写道

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

"Tasos Vogiatzoglou" <tv*****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
我试图遵循的一些指导原则如下:

1.使用连接池。通过这种方式,您可以控制打开的连接数,而不会牺牲您的性能。


连接池是默认设置。没有?如果您正在使用SqlConnection我认为

连接池自动发生。

垃圾收集器一般不会允许内存泄漏(仅在
无法管理的情况下)没有正确解除分配的资源)
Some guidelines that I ''m trying to follow are these:

1. Use connection pooling. This way you can have a control on the
number of connections open without sacrificing much from your
performance
Connection pooling is the default. No? If you''re using SqlConnection I think
connection pooling happens automagically.
The garbage collector in general will not permit memory leaks ( only in
case of unmanaged resources that are not deallocated properly )




然而,垃圾收集器无法阻止你无意中将b $ b挂在到你不再需要的物体。听起来很傻,但在复杂系统中可能是一个问题。



However, the garbage collector cannot prevent you from inadvertently
"hanging on" to objects you no longer need. Sounds silly, but can be a
problem in complex systems.




< Jo **** ******@gmail.com>在消息中写道

新闻:11 ********************** @ z14g2000cwz.googlegr oups.com ...

<Jo**********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
1.如果每次关闭sql连接,是否需要关闭或处理数据集,命令或任何其他对象?


正如Tasos建议的那样,使用SqlDataReader而不是DataSet。是的,你

应该在完成后关闭它。

2.我应该使用数据集的数据集吗?


SqlDataReader对于仅向前只读访问来说是最快的。

4.我如何检查内存泄漏以及最可能的内容
罪魁祸首?
1. If I close the sql connection every time do I need to close or
dispose of the dataset, command or any other objects?
As Tasos recommended, use SqlDataReader instead of DataSet. And yes, you
should close it when finished.
2. Should I use data adaptor instaed of dataset?
SqlDataReader is fastest for forward-only read-only access.
4. How can I check for memory leaks and what are the most likely
culprits?




最有可能是罪魁祸首,你把它放入Session会忘记的东西。此外,

您手动连接某些事件的对象。

http://www.red-gate.com/products/ANT...iler/index.htm

http://www.red-gate.com/products/ants_load/ index.htm



Most likely culprits, stuff you put into Session and forgot about. Also,
objects where you manually wired up some events.

http://www.red-gate.com/products/ANT...iler/index.htm

http://www.red-gate.com/products/ants_load/index.htm


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

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