Skip to content

How to release memory in C# application

tylerstaszak

Asked Dec 8, 2005, 7:50 AM EST5 answers
Share
LinkedInX
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!

5 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    If you set null to byte array, i think .net cleans it up automatically. But since byteArray is local variable, it should be disposed anyhow. You could try to give system time to free memory with Threading.Thread.Sleep method?

    byteArray = null;

    Usually you should check if a class implements IDisposable interface; when class does that, it has Dispose-method that enables eplicit object disposion (not the case with Array class).
    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp//is "disposion" even a word?;)
    0
  • 0
    Setting byteArray = null had no effect.
    0
  • 0
    Bummer. Do you get some sort of "out of memory" error messages? Could you handle the files in smaller pieces? I never dealed with file sized that great, hope someone else would answer you too.
    0
  • 0
     What are you doing with the ByteArray, is it a serialized control of some sorts. If so I would check the object you are deserializing. I Serialize my screen object to a file in my app. and then load it back into the application. I can save as many serialized objects ranging from 2000 to 4996 bytes in a single document and then deserialize them back with no problems.
    I do this when killing the array;

    ByteArray = new Byte[]{};
    ByteArray = null;


    Just a suggestion.
    Larry
    0
  • 0
    >>tylerstaszak: Setting byteArray = null had no effect.
    0