944,198 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 7956
  • C# RSS
May 3rd, 2006
0

Cannot Get Memory To Release

Expand Post »
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?

C# Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
steve65 is offline Offline
1 posts
since May 2006
May 3rd, 2006
0

Re: Cannot Get Memory To Release

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.
Team Colleague
Reputation Points: 186
Solved Threads: 147
Cookie... That's it
alc6379 is offline Offline
2,519 posts
since Dec 2003
May 3rd, 2006
0

Re: Cannot Get Memory To Release

If not, perhaps the
C# Syntax (Toggle Plain Text)
  1. .Dispose();
method will dispose of it?
Team Colleague
Reputation Points: 84
Solved Threads: 99
<Insert title here>
tayspen is offline Offline
1,542 posts
since Jul 2005
May 3rd, 2006
0

Re: Cannot Get Memory To Release

Quote originally posted by tayspen ...
If not, perhaps the
C# Syntax (Toggle Plain Text)
  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...
Team Colleague
Reputation Points: 186
Solved Threads: 147
Cookie... That's it
alc6379 is offline Offline
2,519 posts
since Dec 2003

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: c# or vb.net - how earn more?
Next Thread in C# Forum Timeline: How to use good oo-structure?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC