There are huge files about 100Mb. I want to load them into memory (RAM), process and save somewhere.
At the same time I want that a limit of memory usage exists. Example, 100Mb, to my app don't use more then this memory limit. If the limit is exceeded the file is processed parts.
My understanding of this:
var line = file.ReadLine();
var allowed = true;
while( allowed && line != null )
{
var newObject = new SomeObject( line );
list.add( newObject );
// Checking the memory
allowed = CheckUsedMemory();
line = file.ReadLine()
}
How to limit the use of RAM? How to implement the CheckUsedMemory method? Thank you.
UPD
Thank you everybody for good advices.
You can try with:
long usedMemory = GC.GetTotalMemory(true);
or
long usedMemory = GC.GetTotalMemory(false);
The first will force a garbage collecting (cleaning) of the memory, so it's slower (milliseconds)
Then read this to see how much memory your machine has:
How do you get total amount of RAM the computer has?
Remember that if you are running as a 32 bits app, you can't use all the memory, and that other processes could be using the memory!