User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C# section within the Software Development category of DaniWeb, a massive community of 456,470 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,792 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C# advertiser: Programming Forums
Views: 3570 | Replies: 3
Reply
Join Date: May 2006
Posts: 1
Reputation: steve65 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
steve65 steve65 is offline Offline
Newbie Poster

Question Cannot Get Memory To Release

  #1  
May 3rd, 2006
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);
  }
 }
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2003
Location: Nashville, TN
Posts: 2,333
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Rep Power: 11
Solved Threads: 102
Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: Cannot Get Memory To Release

  #2  
May 3rd, 2006
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/de...gstatement.asp
Last edited by alc6379 : May 3rd, 2006 at 6:53 pm.
Alex Cavnar, aka alc6379
Reply With Quote  
Join Date: Jul 2005
Location: FL.
Posts: 1,536
Reputation: tayspen is on a distinguished road 
Rep Power: 7
Solved Threads: 98
Colleague
tayspen's Avatar
tayspen tayspen is offline Offline
<Insert title here>

Re: Cannot Get Memory To Release

  #3  
May 3rd, 2006
If not, perhaps the
.Dispose();
method will dispose of it?
Firefox
Ewido
Tune up windows
Get detailed system information
My Fixes

Member - Alliance of Security Analysis Professionals - Since 2006
Reply With Quote  
Join Date: Dec 2003
Location: Nashville, TN
Posts: 2,333
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Rep Power: 11
Solved Threads: 102
Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: Cannot Get Memory To Release

  #4  
May 3rd, 2006
Originally Posted by tayspen
If not, perhaps the
.Dispose();
method will dispose of it?


That was my first thought, actually. But, then I found the using 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...
Alex Cavnar, aka alc6379
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C# Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C# Forum

All times are GMT -4. The time now is 2:27 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC