Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
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.
Posted
Updated 19-Aug-12 15:37pm
v2
Comments
Sergey Alexandrovich Kryukov 19-Aug-12 21:34pm    
Why?! Didn't bother to read what .NET is all about? Seriously? Not a word? This is the only possible explanation of this question.
--SA
Swinkaran 19-Aug-12 21:38pm    
thanks. What are you trying to say? Could you please explain me?
Sergey Alexandrovich Kryukov 19-Aug-12 22:56pm    
Please see my answer, it's comprehensive enough. Read on related topics -- this is critically important to understand.

Good luck,
--SA
Swinkaran 19-Aug-12 23:20pm    
Thanks SA. I will have look at your solution and links.
Sergey Alexandrovich Kryukov 19-Aug-12 23:32pm    
My pleasure.
And please don't forget to accept the answer formally (green button) -- now or later.
--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
 
Share this answer
 
v2
Comments
Kenneth Haugland 19-Aug-12 23:39pm    
5'ed!
Sergey Alexandrovich Kryukov 20-Aug-12 0:18am    
Thank you, Kenneth.
--SA
Abhinav S 19-Aug-12 23:54pm    
5!
Sergey Alexandrovich Kryukov 20-Aug-12 0:18am    
Thank you, Abhinav.
--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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900