•
•
•
•
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
![]() |
•
•
Join Date: May 2006
Posts: 1
Reputation:
Rep Power: 0
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?
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);
}
}
}•
•
Join Date: Dec 2003
Location: Nashville, TN
Posts: 2,333
Reputation:
Rep Power: 11
Solved Threads: 102
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?
.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
•
•
Join Date: Dec 2003
Location: Nashville, TN
Posts: 2,333
Reputation:
Rep Power: 11
Solved Threads: 102
•
•
•
•
Originally Posted by tayspen
If not, perhaps themethod will dispose of it?.Dispose();
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
![]() |
•
•
•
•
•
•
•
•
DaniWeb C# Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Other Threads in the C# Forum
- Previous Thread: c# or vb.net - how earn more?
- Next Thread: How to use good oo-structure?



Linear Mode