Click here to Skip to main content
13,097,225 members (65,639 online)
Rate this:
Please Sign up or sign in to vote.
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 19-Aug-12 15:30pm
Updated 19-Aug-12 15:37pm
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.
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,
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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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:[^].

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:[^].

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:[^].

Another advanced feature is using the Garbage Collector (CG) API. Please see:[^].

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:[^],[^].

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

Good luck,
Kenneth Haugland 19-Aug-12 23:39pm
Thank you, Kenneth.
Abhinav S 19-Aug-12 23:54pm
Thank you, Abhinav.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.170813.1 | Last Updated 19 Aug 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100