清算在C#中回收内存 [英] Clearing & reclaiming memory in C#

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

问题描述

我正在使用C#.Net开发Windows应用程序.实际上,这是一个安装在DBMS中的插件.该插件的目的是读取DBMS中的所有记录(一条记录是一个对象),匹配提供的条件,并将它们作为XML文件传输到我的本地文件系统中.我的问题与内存使用有关.一切正常.但是,每次读取记录时,它都会占用内存,并且在达到一定限制后,由于内存不足,插件将停止工作.

我正在处理大约10k-20k的记录(对象). C#中是否有任何与内存相关的方法,可在将每条记录写入XML文件后立即清除它们的内存.我尝试了所有基本的内存处理方法,例如clear(),flush(),gc()和& finalize()/但没有用.

请考虑以下他:
记录是一个对象,我不能更改它并使用其他有效的数据结构.
每次阅读记录时,我都会将它们写入XML.

I am developing a windows application using C# .Net. This is in fact a plug-in which is installed in to a DBMS. The purpose of this plug-in is to read all the records (a record is an object) in DBMS, matching the provided criteria and transfer them across to my local file system as XML files. My problem is related to usage of memory. Everything is working fine. But, each time I read a record, it occupies the memory and after a certain limit the plug in stops working, because of out of memory.

I am dealing with around 10k-20k of records (objects). Is there any memory related methods in C# to clear the memory of each record as soon as they are written to the XML file. I tried all the basic memory handling methods like clear(), flush(), gc(), & finalize()/ But no use.

Please consider he following:
Record is an object, I cannot change this & use other efficient data structures.
Each time I read a record I write them to XML. and repeat this again & again.

推荐答案

CLI是受托管的平台.这意味着您不直接分配,但更重要的是,您不控制回收内存.就C#而言,您始终使用新"运算符分配托管堆内存,而无需清理内存.这是通过垃圾收集机制完成的.请参阅:
http://en.wikipedia.org/wiki/Garbage_collection_(computer_science) [ http://en.wikipedia.org/wiki/Flat_memory_model [ http://msdn.microsoft.com/en-us/library/f58wzh21%28v = vs.100%29.aspx [ ^ ].

另一个高级功能是使用垃圾收集器(CG)API.请参阅:
http://msdn.microsoft.com/en-us/library/system.gc.aspx [ ^ ].

我强烈建议不要这样做.垃圾回收很难改善手动控制;只会让事情变得更糟.

最后,可以在CLI应用程序中间接使用非托管内存,例如通过P/Invoke.这超出了问题的主题,但想法是:这是由非托管代码本身独立完成的;您的托管代码仅调用适当的方法.无论如何,非托管资源的回收都是通过System.IDisposable.Dispose完成的,但是此接口不仅用于此目的.在C#中,尽可能使用using语句很重要.请参阅:
http://msdn.microsoft.com/en-us/library/system.idisposable%28v = vs.100%29.aspx [ http://msdn.microsoft.com/en-us/library/yh598w02%28v = vs.100%29.aspx [ ^ ].

我建议您在尝试进行编程之前至少阅读基本的入门文档,以避免完全沮丧和麻烦.

祝你好运,
—SA
CLI is a managed platform. It means that you don''t directly allocate, but, more importantly, you do not control reclaiming memory. In terms of C#, you always allocate managed heap memory with "new" operators and never need to clean up the memory. It is done through the mechanism of Garbage Collection. Please see:
http://en.wikipedia.org/wiki/Garbage_collection_(computer_science)[^].

You should also understand that the managed pointers (memory handles, known in C# as references) are complex objects very different from unmanaged pointers. The objects allocated in memory could be automatically moved during run time to by the platform, to optimize memory layout, avoid fragmentation, etc. Any program assuming fixed location of any object in linear memory (used, for example, in Windows architecture) is incorrect. This is not usually a problem, because most code use memory only through handles.
See also: http://en.wikipedia.org/wiki/Flat_memory_model[^].

The advanced feature is using pointers in managed memory, which only possible in an unsave statement, but pointer operations are done through the special process of pinning of a fragment of memory. In C#, this is done via the fixed statement. Please see:
http://msdn.microsoft.com/en-us/library/f58wzh21%28v=vs.100%29.aspx[^].

Another advanced feature is using the Garbage Collector (CG) API. Please see:
http://msdn.microsoft.com/en-us/library/system.gc.aspx[^].

I strongly discourage doing it. Garbage collection is very hard to improve controlling it manually; you can only make things worse.

Finally, it''s possible to use unmanaged memory in a CLI application indirectly, for example, via P/Invoke. This goes beyond the topic of the question, but the idea is: this is done independently by the unmanaged code itself; your managed code only calls appropriate methods. As a matter or rule, reclaiming of unmanaged resource is done via System.IDisposable.Dispose, but this interface is used not only for this purpose. In C#, it''s important to use using statement whenever possible. Please see:
http://msdn.microsoft.com/en-us/library/system.idisposable%28v=vs.100%29.aspx[^],
http://msdn.microsoft.com/en-us/library/yh598w02%28v=vs.100%29.aspx[^].

I would advice to read at least basic introductory documentation before trying to do programming, to avoid total frustration and troubles.

Good luck,
—SA


了解托管代码和非托管代码之间的区别,不必过多担心为托管代码释放资源.
让框架为您执行此操作.
Understand the difference between managed and unmanaged code and don''t worry too much about freeing up resources for managed code.
Let the framework do this for you.


这篇关于清算在C#中回收内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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