Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: .NET2.0 C# memory
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 16:30pm
Swinkaran2.3K
Edited 19-Aug-12 16:37pm
v2
Comments
Sergey Alexandrovich Kryukov at 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 at 19-Aug-12 21:38pm
   
thanks. What are you trying to say? Could you please explain me?
Sergey Alexandrovich Kryukov at 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 at 19-Aug-12 23:20pm
   
Thanks SA. I will have look at your solution and links.
Sergey Alexandrovich Kryukov at 19-Aug-12 23:32pm
   
My pleasure.
And please don't forget to accept the answer formally (green button) -- now or later.
--SA
Rate this: bad
good
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:
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
  Permalink  
v2
Comments
Kenneth Haugland at 19-Aug-12 23:39pm
   
5'ed!
Sergey Alexandrovich Kryukov at 20-Aug-12 0:18am
   
Thank you, Kenneth.
--SA
Abhinav S at 19-Aug-12 23:54pm
   
5!
Sergey Alexandrovich Kryukov at 20-Aug-12 0:18am
   
Thank you, Abhinav.
--SA
Rate this: bad
good
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.
  Permalink  

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

  Print Answers RSS
0 Kornfeld Eliyahu Peter 169
1 George Jonsson 145
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,382


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 19 Aug 2012
Copyright © CodeProject, 1999-2014
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