Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,

I am reading a large file upto 10-20 MB in a string in 1 go in below line

string[] lines = System.IO.File.ReadAllLines(file_name);

I save each line to database , and dispose the connection.

Now when i again do this operation , the windows app crashes with out of memory exception.

I see during first read operation memory increases and is not freed when operation completes.

How can i free the memory allocated to this string.
Thanks,
Shreyas
Posted

Keep your connection open and just change your command object for inserting, if you have at least 2Gb memory on your system then a 10-20Mb file read in full is nothing.
 
Share this answer
 
No, you cannot. You don't have control over the memory deallocation. It is done by the Garbage Collector at some moment of time after some object become unreachable, and you don't control when exactly it happens. Please see:
http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29[^].

Perhaps the file you read with ReadAllLines is just too big. Instead, you can use the class System.IO.StreamReader and read the data line by line using System.IO.StreamReader.ReadLine:
http://msdn.microsoft.com/en-us/library/system.io.streamreader.aspx[^],
http://msdn.microsoft.com/en-us/library/system.io.streamreader.readline.aspx[^].

It would help you to divide a file by smaller chunks and do something with those chunks one at a time.

—SA
 
Share this answer
 
Comments
srastogi85 23-Sep-13 10:14am    
Thanks Sergey,
I am planning to modify the code based on below lines , by having using clause and streamreader
, i think this might solve the issue for big files. Please suggest you views for this ?

using (StreamReader sr = new StreamReader("TestFile.txt"))
{
string line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
Sergey Alexandrovich Kryukov 23-Sep-13 10:18am    
That is the right usage. What will you do with the data read is your business.
Will you accept my answer formally now?
—SA
srastogi85 23-Sep-13 10:20am    
yes sure sir :) thanks a lot
Sergey Alexandrovich Kryukov 23-Sep-13 11:06am    
You are very welcome.
—SA
srastogi85 23-Sep-13 10:26am    
This still didn't solved the problem

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