How to release memory in C# application

Dec 8 2005 12:50 PM
I am writing a program that reads in very large binary files.  These files are often 1GB in size.  The program is searching a directory and trying to read in multiple of these files.  My server has 4GB of memory, so obviously, I need to release the memory once I am done with a file so I can move on.  Otherwise, I hit my memory threshhold very quickly.  My code looks like this:

DirectoryInfo dir = new DirectoryInfo(mydir);
foreach (FileInfo f in dir.GetFiles("*.std"))
{
  // Create a filestream for our new file.
 
FileStream fs = s.file_open(f.FullName);  // calls a simple method that opens a filestream object.

  // Determine the length of the filestream, and create the appropriate variables.
 
double tmp_length = fs.Length;
 
int length = (int)tmp_length;
 
byte[] byteArray = new byte[0];
 
string PART_TYP = null;
 
byteArray = new byte[length];

  fs.Read(byteArray, 0, length);
  fs.Close();
 
for (int j=0; j<byteArray.Length; j++)
  {
      // Go do some stuff with this binary data...
  }
}

So, my question is, after my for loop, how do I "clean up" the byteArray array.  I've tried calling GC.Collect() and that has no effect.  After a couple of files, VS throws an out of memory error and because the process is using all the RAM.

This is probably a really easy question, I've just never dealt with memory allocation before.

Any help would be VERY VERY much appreciated as I have been struggling with this for quite a while.

Thanks a lot!

 


Answers (5)