Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. How to free up memory

How to free up memory

Scheduled Pinned Locked Moved C#
helpquestiongraphicsperformancetutorial
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    T Willey
    wrote on last edited by
    #1

    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

    N L V 3 Replies Last reply
    0
    • T T Willey

      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

      N Offline
      N Offline
      Nader Elshehabi
      wrote on last edited by
      #2

      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 1 Reply Last reply
      0
      • N Nader Elshehabi

        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 Offline
        T Offline
        T Willey
        wrote on last edited by
        #3

        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

        N 1 Reply Last reply
        0
        • T T Willey

          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

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          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

          T 1 Reply Last reply
          0
          • T T Willey

            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

            N Offline
            N Offline
            Nader Elshehabi
            wrote on last edited by
            #5

            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:

            1 Reply Last reply
            0
            • L Luc Pattyn

              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

              T Offline
              T Offline
              T Willey
              wrote on last edited by
              #6

              Thanks for the heads up Luc. I write C# very infrequent, so these are nice tips to know.

              Tim

              1 Reply Last reply
              0
              • T T Willey

                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

                V Offline
                V Offline
                Vikram kshatriya
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups