954,180 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Cannot Get Memory To Release

Hi all!

I have been having the hardest time trying to get memory to release from an application I am developing. I know I could have written the code below without the classes but I wanted to makes sure that I understood how to pass references and not values in C#.

If you look at the coments, you will see that the application is still holding onto the memory even through I have left the class that was using all of the memory. I have tried setting fileSorter = null, fileArray = null but that did not work either.

Can anyone tell me why I am not able to release memory back to the system?

using System;
 
namespace ConsoleApplication1
{
 class Class1
 {
  [STAThread]
  static void Main(string[] args)
  {
   // Mem in use by ConsoleApplication1: 6,320K   
   FileSorter fileSorter = new FileSorter();
   fileSorter.SortFile();
 
   // Mem in use by ConsoleApplication1: 564,176K
  }
 }
 public class FileSorter
 {
  public void SortFile()
  {
   string[] fileArray = null;
   FileHandler fileHandler = new FileHandler();
   StringHandler stringHandler = new StringHandler();
 
   // Mem in use by ConsoleApplication1: 6,408K   
   fileHandler.FileToArray(ref fileArray, @"c:\tmp\Really Big File.txt");
 
   // Mem in use by ConsoleApplication1: 561,084K
   // File being sorted is only 114,437K
   // I expected a memory usage of about 120,845K
   stringHandler.SortArray(ref fileArray);
 
   // Mem in use by ConsoleApplication1: 561,136K
   fileHandler.ArrayToFile(ref fileArray, @"c:\tmp\Really Big File Sorted.txt");
 
   // The file has now been sorted.  How do I dispose of fileArray properly before I leave this method?
  }
 }
 
 public class FileHandler
 {
  public void FileToArray(ref string[] outputArray, string filePath)
  {
   using (System.IO.StreamReader sr = new System.IO.StreamReader(filePath))
   {
    outputArray = System.Text.RegularExpressions.Regex.Split(sr.ReadToEnd(), Environment.NewLine);
    sr.Close();
   }
  }
 
  public void ArrayToFile(ref string[] inputArray, string filePath)
  {
   using (System.IO.StreamWriter sw = new System.IO.StreamWriter(filePath))
   {
    foreach (string inputLine in inputArray)
    {
     sw.WriteLine(inputLine);
    }
    sw.Close();
   }
  }
 }
 
 public class StringHandler
 {
  public void SortArray(ref string[] inputArray)
  {
   Array.Sort(inputArray);
  }
 }
}
steve65
Newbie Poster
1 post since May 2006
Reputation Points: 10
Solved Threads: 0
 

I thought that once the Method has finished all of its work, the garbage collection would take care of itself?

Ie, I thought that once the method ends, any object that was instantiated would be disposed of properly. Is this not occurring?

check this out:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfusingstatement.asp

alc6379
Cookie... That's it
Team Colleague
2,820 posts since Dec 2003
Reputation Points: 186
Solved Threads: 147
 

If not, perhaps the

.Dispose();


method will dispose of it?

tayspen
<Insert title here>
Team Colleague
1,622 posts since Jul 2005
Reputation Points: 84
Solved Threads: 99
 

If not, perhaps the

.Dispose();
method will dispose of it?

That was my first thought, actually. But, then I found theusing directive, and it looks like it does the Dispose() for you.

Keep in mind, I'm still somewhat new at C#, but there was some reason for including using rather than disposing it directly. I'll have to grab one of the books I've bought to figure out why...

alc6379
Cookie... That's it
Team Colleague
2,820 posts since Dec 2003
Reputation Points: 186
Solved Threads: 147
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You