| | |
Cannot Get Memory To Release
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2006
Posts: 1
Reputation:
Solved Threads: 0
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?
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)
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); } } }
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
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
If not, perhaps the method will dispose of it?
C# Syntax (Toggle Plain Text)
.Dispose();
Firefox
Ewido
Tune up windows
Get detailed system information
My Fixes
Member - Alliance of Security Analysis Professionals - Since 2006
Ewido
Tune up windows
Get detailed system information
My Fixes
Member - Alliance of Security Analysis Professionals - Since 2006
•
•
•
•
Originally Posted by tayspen
If not, perhaps themethod will dispose of it?C# Syntax (Toggle Plain Text)
.Dispose();
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
![]() |
Similar Threads
Other Threads in the C# Forum
- Previous Thread: c# or vb.net - how earn more?
- Next Thread: How to use good oo-structure?
Views: 5701 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access ado.net algorithm array barchart bitmap box broadcast button buttons c# chat check checkbox class client color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development drawing encryption enum event excel file files form format ftp function gdi+ httpwebrequest image index input install java label list listbox listener login mandelbrot math mouseclick mysql networking object oracle path photoshop picturebox pixelinversion post prime programming radians regex remote remoting resource richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation view webbrowser windows winforms wpf xml






