Cannot Get Memory To Release

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2006
Posts: 1
Reputation: steve65 is an unknown quantity at this point 
Solved Threads: 0
steve65 steve65 is offline Offline
Newbie Poster

Cannot Get Memory To Release

 
0
  #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?

  1. using System;
  2.  
  3. namespace ConsoleApplication1
  4. {
  5. class Class1
  6. {
  7. [STAThread]
  8. static void Main(string[] args)
  9. {
  10. // Mem in use by ConsoleApplication1: 6,320K
  11. FileSorter fileSorter = new FileSorter();
  12. fileSorter.SortFile();
  13.  
  14. // Mem in use by ConsoleApplication1: 564,176K
  15. }
  16. }
  17. public class FileSorter
  18. {
  19. public void SortFile()
  20. {
  21. string[] fileArray = null;
  22. FileHandler fileHandler = new FileHandler();
  23. StringHandler stringHandler = new StringHandler();
  24.  
  25. // Mem in use by ConsoleApplication1: 6,408K
  26. fileHandler.FileToArray(ref fileArray, @"c:\tmp\Really Big File.txt");
  27.  
  28. // Mem in use by ConsoleApplication1: 561,084K
  29. // File being sorted is only 114,437K
  30. // I expected a memory usage of about 120,845K
  31. stringHandler.SortArray(ref fileArray);
  32.  
  33. // Mem in use by ConsoleApplication1: 561,136K
  34. fileHandler.ArrayToFile(ref fileArray, @"c:\tmp\Really Big File Sorted.txt");
  35.  
  36. // The file has now been sorted. How do I dispose of fileArray properly before I leave this method?
  37. }
  38. }
  39.  
  40. public class FileHandler
  41. {
  42. public void FileToArray(ref string[] outputArray, string filePath)
  43. {
  44. using (System.IO.StreamReader sr = new System.IO.StreamReader(filePath))
  45. {
  46. outputArray = System.Text.RegularExpressions.Regex.Split(sr.ReadToEnd(), Environment.NewLine);
  47. sr.Close();
  48. }
  49. }
  50.  
  51. public void ArrayToFile(ref string[] inputArray, string filePath)
  52. {
  53. using (System.IO.StreamWriter sw = new System.IO.StreamWriter(filePath))
  54. {
  55. foreach (string inputLine in inputArray)
  56. {
  57. sw.WriteLine(inputLine);
  58. }
  59. sw.Close();
  60. }
  61. }
  62. }
  63.  
  64. public class StringHandler
  65. {
  66. public void SortArray(ref string[] inputArray)
  67. {
  68. Array.Sort(inputArray);
  69. }
  70. }
  71. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2003
Posts: 2,414
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Solved Threads: 123
Team Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: Cannot Get Memory To Release

 
0
  #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 Quick reply to this message  
Join Date: Jul 2005
Posts: 1,542
Reputation: tayspen is on a distinguished road 
Solved Threads: 98
Team Colleague
tayspen's Avatar
tayspen tayspen is offline Offline
<Insert title here>

Re: Cannot Get Memory To Release

 
0
  #3
May 3rd, 2006
If not, perhaps the
  1. .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 Quick reply to this message  
Join Date: Dec 2003
Posts: 2,414
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Solved Threads: 123
Team Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: Cannot Get Memory To Release

 
0
  #4
May 3rd, 2006
Originally Posted by tayspen
If not, perhaps the
  1. .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 Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 5701 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC