How to free up memory
-
I have this program I wrote that will search the entire c drive and make a treeview dialog of all the folders with the memory taken up by the folders. I can understand that this process should take up some memory, but once it finishes it should release the memory because all you are doing now is looking at the treeview which has been all filled out already with all the information that it will ever have. I guess I'm trying to say that once the dialog comes up, how can I free some of the memory that my program has taken up? On my machine it has taken up to 30k of memory, and held it until I close it. Here is the code. Thanks in advance for any help. If this is too much code, let me know and I will edit my post. using System; using System.Drawing; using System.IO; using System.Windows.Forms; namespace MemoryManagmentDialog { /// /// Description of MainForm. /// public class MainForm : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; private System.Windows.Forms.TreeView treeView1; public static double GetDirectorySize (DirectoryInfo Di, TreeNode ParentNode, bool IsFirst) { double DirSize = 0; try { FileInfo[] fiAr = Di.GetFiles (); foreach (FileInfo fi in fiAr) { DirSize = DirSize + fi.Length; } DirectoryInfo[] diAr = Di.GetDirectories(); if (IsFirst == true) { foreach (DirectoryInfo di in diAr) { DirSize = DirSize + GetDirectorySize (di, ParentNode, false); } } else { TreeNode SubNode = new TreeNode (Di.Name); foreach (DirectoryInfo di in diAr) { DirSize = DirSize + GetDirectorySize (di, SubNode, false); } ParentNode.Nodes.Add (SubNode); SubNode.Text = Di.Name + " - [ " + SizeToString (DirSize, SubNode) + " ]"; } } catch { TreeNode SubNode = new TreeNode (Di.Name); ParentNode.Nodes.Add (SubNode); SubNode.Text = Di.Name + " ** [ Error ]"; SubNode.BackColor = Color.Red; SubNode.ForeColor = Color.White; } return DirSize; } public static string SizeToString (Double dbl, TreeNode tn) { string Str; if (0 <= dbl && dbl < 1000) { Str = dbl.ToString() + " b"; tn.BackColor = Color.Orange; return Str; } else if (1000 < dbl && dbl < 1000000) { Str = (dbl / 1000).ToString() + " KB"; tn.BackColor = Color.PaleGreen; return Str; } else if (1000000 < dbl && dbl < 1000000000) { Str = (dbl / 1000000).ToString() + " MB
-
I have this program I wrote that will search the entire c drive and make a treeview dialog of all the folders with the memory taken up by the folders. I can understand that this process should take up some memory, but once it finishes it should release the memory because all you are doing now is looking at the treeview which has been all filled out already with all the information that it will ever have. I guess I'm trying to say that once the dialog comes up, how can I free some of the memory that my program has taken up? On my machine it has taken up to 30k of memory, and held it until I close it. Here is the code. Thanks in advance for any help. If this is too much code, let me know and I will edit my post. using System; using System.Drawing; using System.IO; using System.Windows.Forms; namespace MemoryManagmentDialog { /// /// Description of MainForm. /// public class MainForm : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; private System.Windows.Forms.TreeView treeView1; public static double GetDirectorySize (DirectoryInfo Di, TreeNode ParentNode, bool IsFirst) { double DirSize = 0; try { FileInfo[] fiAr = Di.GetFiles (); foreach (FileInfo fi in fiAr) { DirSize = DirSize + fi.Length; } DirectoryInfo[] diAr = Di.GetDirectories(); if (IsFirst == true) { foreach (DirectoryInfo di in diAr) { DirSize = DirSize + GetDirectorySize (di, ParentNode, false); } } else { TreeNode SubNode = new TreeNode (Di.Name); foreach (DirectoryInfo di in diAr) { DirSize = DirSize + GetDirectorySize (di, SubNode, false); } ParentNode.Nodes.Add (SubNode); SubNode.Text = Di.Name + " - [ " + SizeToString (DirSize, SubNode) + " ]"; } } catch { TreeNode SubNode = new TreeNode (Di.Name); ParentNode.Nodes.Add (SubNode); SubNode.Text = Di.Name + " ** [ Error ]"; SubNode.BackColor = Color.Red; SubNode.ForeColor = Color.White; } return DirSize; } public static string SizeToString (Double dbl, TreeNode tn) { string Str; if (0 <= dbl && dbl < 1000) { Str = dbl.ToString() + " b"; tn.BackColor = Color.Orange; return Str; } else if (1000 < dbl && dbl < 1000000) { Str = (dbl / 1000).ToString() + " KB"; tn.BackColor = Color.PaleGreen; return Str; } else if (1000000 < dbl && dbl < 1000000000) { Str = (dbl / 1000000).ToString() + " MB
T.Willey wrote:
the treeview which has been all filled out already with all the information that it will ever have.
Sure! And where do you think this information is stored -for repainting, etc...- until the TreeView is disposed?:-D
Regards:rose:
-
T.Willey wrote:
the treeview which has been all filled out already with all the information that it will ever have.
Sure! And where do you think this information is stored -for repainting, etc...- until the TreeView is disposed?:-D
Regards:rose:
I was thinking that once it was done searching the drive then it wouldn't need to consume as much memory. If I'm wrong, then I think it will be fine, I was just trying to optomise it. Thanks again Nader. I never did get the other program you were helping me with to work, and the person I was doing it for found another way, so I stopped trying after like 7 revisions.
Tim
-
I have this program I wrote that will search the entire c drive and make a treeview dialog of all the folders with the memory taken up by the folders. I can understand that this process should take up some memory, but once it finishes it should release the memory because all you are doing now is looking at the treeview which has been all filled out already with all the information that it will ever have. I guess I'm trying to say that once the dialog comes up, how can I free some of the memory that my program has taken up? On my machine it has taken up to 30k of memory, and held it until I close it. Here is the code. Thanks in advance for any help. If this is too much code, let me know and I will edit my post. using System; using System.Drawing; using System.IO; using System.Windows.Forms; namespace MemoryManagmentDialog { /// /// Description of MainForm. /// public class MainForm : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; private System.Windows.Forms.TreeView treeView1; public static double GetDirectorySize (DirectoryInfo Di, TreeNode ParentNode, bool IsFirst) { double DirSize = 0; try { FileInfo[] fiAr = Di.GetFiles (); foreach (FileInfo fi in fiAr) { DirSize = DirSize + fi.Length; } DirectoryInfo[] diAr = Di.GetDirectories(); if (IsFirst == true) { foreach (DirectoryInfo di in diAr) { DirSize = DirSize + GetDirectorySize (di, ParentNode, false); } } else { TreeNode SubNode = new TreeNode (Di.Name); foreach (DirectoryInfo di in diAr) { DirSize = DirSize + GetDirectorySize (di, SubNode, false); } ParentNode.Nodes.Add (SubNode); SubNode.Text = Di.Name + " - [ " + SizeToString (DirSize, SubNode) + " ]"; } } catch { TreeNode SubNode = new TreeNode (Di.Name); ParentNode.Nodes.Add (SubNode); SubNode.Text = Di.Name + " ** [ Error ]"; SubNode.BackColor = Color.Red; SubNode.ForeColor = Color.White; } return DirSize; } public static string SizeToString (Double dbl, TreeNode tn) { string Str; if (0 <= dbl && dbl < 1000) { Str = dbl.ToString() + " b"; tn.BackColor = Color.Orange; return Str; } else if (1000 < dbl && dbl < 1000000) { Str = (dbl / 1000).ToString() + " KB"; tn.BackColor = Color.PaleGreen; return Str; } else if (1000000 < dbl && dbl < 1000000000) { Str = (dbl / 1000000).ToString() + " MB
In .NET you typically dont have to worry continuously about memory consumption; objects that are in use normally stay alive, and objects that are not needed anymore, although not deleted immediately (as would be the case with unmanaged C++ destructors), get discarded when the garbage collector sees fit. Also 30KB is a really small amount on most of todays systems. It still pays off to keep memory consumption in mind when performance is important, so one should avoid making an excessive number of objects. I see no problems with your application; there are some strings (as in all apps), and the FileInfo and DirectoryInfo arrays go out of scope pretty soon, so they are collectable. If you want to save memory, you can: - empty collections (e.g. ListView.Clear) - set unneeded object references to null (so they become collectible even before they go out of scope). Both of these of course only if your program logic allows it. As a last resort, you could invoke the garbage collector explicitly, but in general, this is not a good thing to do. Normally just let it do it's thing... :)
Luc Pattyn
-
I was thinking that once it was done searching the drive then it wouldn't need to consume as much memory. If I'm wrong, then I think it will be fine, I was just trying to optomise it. Thanks again Nader. I never did get the other program you were helping me with to work, and the person I was doing it for found another way, so I stopped trying after like 7 revisions.
Tim
T.Willey wrote:
I never did get the other program you were helping me with to work
That's bad news!! I hope you make it next time. Best wishes, and you are welcome anytime.
Regards:rose:
-
In .NET you typically dont have to worry continuously about memory consumption; objects that are in use normally stay alive, and objects that are not needed anymore, although not deleted immediately (as would be the case with unmanaged C++ destructors), get discarded when the garbage collector sees fit. Also 30KB is a really small amount on most of todays systems. It still pays off to keep memory consumption in mind when performance is important, so one should avoid making an excessive number of objects. I see no problems with your application; there are some strings (as in all apps), and the FileInfo and DirectoryInfo arrays go out of scope pretty soon, so they are collectable. If you want to save memory, you can: - empty collections (e.g. ListView.Clear) - set unneeded object references to null (so they become collectible even before they go out of scope). Both of these of course only if your program logic allows it. As a last resort, you could invoke the garbage collector explicitly, but in general, this is not a good thing to do. Normally just let it do it's thing... :)
Luc Pattyn
-
I have this program I wrote that will search the entire c drive and make a treeview dialog of all the folders with the memory taken up by the folders. I can understand that this process should take up some memory, but once it finishes it should release the memory because all you are doing now is looking at the treeview which has been all filled out already with all the information that it will ever have. I guess I'm trying to say that once the dialog comes up, how can I free some of the memory that my program has taken up? On my machine it has taken up to 30k of memory, and held it until I close it. Here is the code. Thanks in advance for any help. If this is too much code, let me know and I will edit my post. using System; using System.Drawing; using System.IO; using System.Windows.Forms; namespace MemoryManagmentDialog { /// /// Description of MainForm. /// public class MainForm : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; private System.Windows.Forms.TreeView treeView1; public static double GetDirectorySize (DirectoryInfo Di, TreeNode ParentNode, bool IsFirst) { double DirSize = 0; try { FileInfo[] fiAr = Di.GetFiles (); foreach (FileInfo fi in fiAr) { DirSize = DirSize + fi.Length; } DirectoryInfo[] diAr = Di.GetDirectories(); if (IsFirst == true) { foreach (DirectoryInfo di in diAr) { DirSize = DirSize + GetDirectorySize (di, ParentNode, false); } } else { TreeNode SubNode = new TreeNode (Di.Name); foreach (DirectoryInfo di in diAr) { DirSize = DirSize + GetDirectorySize (di, SubNode, false); } ParentNode.Nodes.Add (SubNode); SubNode.Text = Di.Name + " - [ " + SizeToString (DirSize, SubNode) + " ]"; } } catch { TreeNode SubNode = new TreeNode (Di.Name); ParentNode.Nodes.Add (SubNode); SubNode.Text = Di.Name + " ** [ Error ]"; SubNode.BackColor = Color.Red; SubNode.ForeColor = Color.White; } return DirSize; } public static string SizeToString (Double dbl, TreeNode tn) { string Str; if (0 <= dbl && dbl < 1000) { Str = dbl.ToString() + " b"; tn.BackColor = Color.Orange; return Str; } else if (1000 < dbl && dbl < 1000000) { Str = (dbl / 1000).ToString() + " KB"; tn.BackColor = Color.PaleGreen; return Str; } else if (1000000 < dbl && dbl < 1000000000) { Str = (dbl / 1000000).ToString() + " MB
HI, With the try,catch and throw you can also use "finally" and within the finally block you can release the resources that have occupied memory. thanks, vikram
Vicky