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
  1. Home
  2. General Programming
  3. C#
  4. How instantly free up the memory for List<T> & DataSet

How instantly free up the memory for List<T> & DataSet

Scheduled Pinned Locked Moved C#
performancequestionannouncement
14 Posts 8 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.
  • L Lost User

    Watch your memory profile and see for yourself. GC runs as appropriate. Anything else, and you're just fighting the "managed memory" system. [Measure memory usage in your apps - Visual Studio | Microsoft Docs](https://docs.microsoft.com/en-us/visualstudio/profiling/memory-usage?view=vs-2019)

    "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

    J Offline
    J Offline
    josda1000
    wrote on last edited by
    #4

    true... at that point he should use unsafe code. possibly.

    Josh Davis
    This is what plays in my head when I finish projects.

    1 Reply Last reply
    0
    • M Mou_kol
      1. in one of my routine i used many list and dataset. i populated those with high volume of data. when event used end then clear memory for list like list1 = null and GC.Collect() in my routine but i am not sure that memory occupied by List1 is instantly clear or will be clear later. please share the knowledge. 2) in case of dataset i clear like

      dataset1.Dispose()

      dataset1=null

      GC.Collect()

      but i am not sure that memory occupied by dataset1 is instantly clear or will be clear later. please share the knowledge. 3) if i call GC.Collect() several time in my one routine does degrade the performance of my application ? i found one link which said best option to release memory for list this way first clear List and then call TrimExcess. is it the right one ? i used like

      list1=null & GC.Collect()

      which one is best to clear memory for list instantly ? please see my code below which works well.

      private void button2_Click(object sender, EventArgs e)
      {
      var before = System.Diagnostics.Process.GetCurrentProcess().VirtualMemorySize64;

              List lst1 = new List();
      
              for (int i = 0; i <= 1000000; i++)
              {
                  lst1.Add("Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test ");
              }
      
              var after = System.Diagnostics.Process.GetCurrentProcess().VirtualMemorySize64;
      
              lst1.Clear();
              lst1.TrimExcess();
      
              var current = System.Diagnostics.Process.GetCurrentProcess().VirtualMemorySize64;
          }
      

      please share best option to clear memory for list & dataset instantly. thanks

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #5

      In practice, TrimExcess does nothing to "free memory" - look at the Reference Sources if you aren't sure: Reference Source[^] You will find that in effect all it does is set the internal array to an empty array, it doesn't do anything "fancy" with the memory it no longer wants. Indeed, if you use TrimExcess to set a smaller size to the internal array, it will use more memory to do that, not less! Don't mess with the GC - leave it alone and let it come in when it needs to. The best way to free memory is to set all references to the object to null and let the memory management handle it in it's own time. It's probably a lot better than you are at doing this! :laugh:

      Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      M 1 Reply Last reply
      0
      • M Mou_kol
        1. in one of my routine i used many list and dataset. i populated those with high volume of data. when event used end then clear memory for list like list1 = null and GC.Collect() in my routine but i am not sure that memory occupied by List1 is instantly clear or will be clear later. please share the knowledge. 2) in case of dataset i clear like

        dataset1.Dispose()

        dataset1=null

        GC.Collect()

        but i am not sure that memory occupied by dataset1 is instantly clear or will be clear later. please share the knowledge. 3) if i call GC.Collect() several time in my one routine does degrade the performance of my application ? i found one link which said best option to release memory for list this way first clear List and then call TrimExcess. is it the right one ? i used like

        list1=null & GC.Collect()

        which one is best to clear memory for list instantly ? please see my code below which works well.

        private void button2_Click(object sender, EventArgs e)
        {
        var before = System.Diagnostics.Process.GetCurrentProcess().VirtualMemorySize64;

                List lst1 = new List();
        
                for (int i = 0; i <= 1000000; i++)
                {
                    lst1.Add("Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test ");
                }
        
                var after = System.Diagnostics.Process.GetCurrentProcess().VirtualMemorySize64;
        
                lst1.Clear();
                lst1.TrimExcess();
        
                var current = System.Diagnostics.Process.GetCurrentProcess().VirtualMemorySize64;
            }
        

        please share best option to clear memory for list & dataset instantly. thanks

        R Offline
        R Offline
        realJSOP
        wrote on last edited by
        #6

        Calling GC.Collect() may not immediately release the memory. If you want to make sure it does that, you can do do this:

        GC.Collect();
        GC.WaitForPendingFinalizers();

        Keep in mind that the WaitForPendingFinalizers method blocks the calling thread until it returns. Like everyone else has said, you should probably leave the garbage collector alone, and be more vigilant about references to the object(s) in question. As long as another object holds a reference to another object, that referenced object remains in memory.

        ".45 ACP - because shooting twice is just silly" - JSOP, 2010
        -----
        You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
        -----
        When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

        M 1 Reply Last reply
        0
        • M Mou_kol
          1. in one of my routine i used many list and dataset. i populated those with high volume of data. when event used end then clear memory for list like list1 = null and GC.Collect() in my routine but i am not sure that memory occupied by List1 is instantly clear or will be clear later. please share the knowledge. 2) in case of dataset i clear like

          dataset1.Dispose()

          dataset1=null

          GC.Collect()

          but i am not sure that memory occupied by dataset1 is instantly clear or will be clear later. please share the knowledge. 3) if i call GC.Collect() several time in my one routine does degrade the performance of my application ? i found one link which said best option to release memory for list this way first clear List and then call TrimExcess. is it the right one ? i used like

          list1=null & GC.Collect()

          which one is best to clear memory for list instantly ? please see my code below which works well.

          private void button2_Click(object sender, EventArgs e)
          {
          var before = System.Diagnostics.Process.GetCurrentProcess().VirtualMemorySize64;

                  List lst1 = new List();
          
                  for (int i = 0; i <= 1000000; i++)
                  {
                      lst1.Add("Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test ");
                  }
          
                  var after = System.Diagnostics.Process.GetCurrentProcess().VirtualMemorySize64;
          
                  lst1.Clear();
                  lst1.TrimExcess();
          
                  var current = System.Diagnostics.Process.GetCurrentProcess().VirtualMemorySize64;
              }
          

          please share best option to clear memory for list & dataset instantly. thanks

          J Offline
          J Offline
          jschell
          wrote on last edited by
          #7

          It is important to understand the difference between memory that is being used and memory that at one time was used. The first cannot be freed by any means. You must stop using it and when you do then it moves into the second category where it might be freed. If your allocations are below 85,000 bytes then any allocation request will do a garbage collection itself if there is not enough free memory to meet the needs of the request. This will happen (basically) up until you run out of memory because you are 'using' all of it. It will not free memory if you are using(pointing) to it. If the allocated size is larger than 85,000 bytes (bytes, not chars, ints, etc) then the memory handling is slightly different in that between allocating and free allocations, if those are not somewhat sequential one can end up with having enough free memory but not actually succeeding in getting it because the memory is fragmented. For more on that you must learn quite a bit about how "heaps" in general work (not just C#) and how those can become fragmented. Once you understand that then you can look at the C# "Large Object Heap". Your loop has 1,000,000 items and for a 64 bit machine you would need to have an item in every member of that list and each item would need to be about 10,000 bytes in size before I would suspect it to be a problem.

          Mou_kol wrote:

          button2_Click

          Regardless of the above however that method suggests a UI. And a UI list with a million items in it is a BAD design. Any UI with a list that attempts to contain more than 1000 items EVER, is a BAD design. And that might even be high. Learn how to do paging instead. Also learn how to provide a method that allows the user to provide criteria and search for what they want in the list.

          1 Reply Last reply
          0
          • L Luc Pattyn

            Hi, 1. one seldom needs to worry about memory management when using C# or other .NET programming languages. Do you have positive proof there is a memory problem? Does your app use gigabytes of data (assuming a PC with several GB of physical memory)? 2. A List basically is an array containing references (pointers) to objects of type T. The memory taken by the list itself therefore is only 8 bytes multiplied by its capacity, which automatically is a power of two, and larger than the maximum number of items your list ever held, unless you explicitly reduce its size by calling TrimExcess() or setting Capacity. 3. Normally the objects themselves take much more memory than the list that holds references to them. 4. If you think you need to worry about the memory taken by the list, make sure your listed items are referenced only by said list; if not, these objects will stay alive and hence occupy memory. 5. Calling GC.Collect() is to be avoided, it can severely reduce performance as it scans your entire object world trying to locate dying objects. And there seldom is a need, GC collection will happen automatically when .NET feels a need to free some memory. 6. The one thing you should do (and probably already are doing) is disposing of objects that offer a Dispose() method. :)

            Luc Pattyn [My Articles] Nil Volentibus Arduum

            M Offline
            M Offline
            Mou_kol
            wrote on last edited by
            #8

            i have used several list where storing data from large xml files. now clearing my list this way. calling GC.Collect() at end of operation. please tell me is this approach is good or not ?

            List1.clear();
            List1.TrimExcess()
            List1=null ;

            List2.clear();
            List2.TrimExcess()
            List2=null ;

            List3.clear();
            List3.TrimExcess()
            List3=null ;

            GC.Collect();

            L 1 Reply Last reply
            0
            • OriginalGriffO OriginalGriff

              In practice, TrimExcess does nothing to "free memory" - look at the Reference Sources if you aren't sure: Reference Source[^] You will find that in effect all it does is set the internal array to an empty array, it doesn't do anything "fancy" with the memory it no longer wants. Indeed, if you use TrimExcess to set a smaller size to the internal array, it will use more memory to do that, not less! Don't mess with the GC - leave it alone and let it come in when it needs to. The best way to free memory is to set all references to the object to null and let the memory management handle it in it's own time. It's probably a lot better than you are at doing this! :laugh:

              Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

              M Offline
              M Offline
              Mou_kol
              wrote on last edited by
              #9

              This explanation is not clear....please elaborate it if possible with example. Indeed, if you use TrimExcess to set a smaller size to the internal array, it will use more memory to do that, not less!

              OriginalGriffO 1 Reply Last reply
              0
              • R realJSOP

                Calling GC.Collect() may not immediately release the memory. If you want to make sure it does that, you can do do this:

                GC.Collect();
                GC.WaitForPendingFinalizers();

                Keep in mind that the WaitForPendingFinalizers method blocks the calling thread until it returns. Like everyone else has said, you should probably leave the garbage collector alone, and be more vigilant about references to the object(s) in question. As long as another object holds a reference to another object, that referenced object remains in memory.

                ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                -----
                You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                -----
                When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                M Offline
                M Offline
                Mou_kol
                wrote on last edited by
                #10

                what GC.WaitForPendingFinalizers(); does ? does it call garbage collector immediately or something else ?

                R 1 Reply Last reply
                0
                • M Mou_kol

                  This explanation is not clear....please elaborate it if possible with example. Indeed, if you use TrimExcess to set a smaller size to the internal array, it will use more memory to do that, not less!

                  OriginalGriffO Offline
                  OriginalGriffO Offline
                  OriginalGriff
                  wrote on last edited by
                  #11

                  Look at the reference source that I linked to - it's the actual source code for the List<T> class. Now look at what happens when you set a size half as big as it is at the moment, and follow that into the Capacity property setter. What does the code do when you halve the capacity?

                  Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                  1 Reply Last reply
                  0
                  • M Mou_kol

                    i have used several list where storing data from large xml files. now clearing my list this way. calling GC.Collect() at end of operation. please tell me is this approach is good or not ?

                    List1.clear();
                    List1.TrimExcess()
                    List1=null ;

                    List2.clear();
                    List2.TrimExcess()
                    List2=null ;

                    List3.clear();
                    List3.TrimExcess()
                    List3=null ;

                    GC.Collect();

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

                    Most likely not necessary, inefficient, redundant, and not good as explained before. :|

                    Luc Pattyn [My Articles] Nil Volentibus Arduum

                    1 Reply Last reply
                    0
                    • M Mou_kol
                      1. in one of my routine i used many list and dataset. i populated those with high volume of data. when event used end then clear memory for list like list1 = null and GC.Collect() in my routine but i am not sure that memory occupied by List1 is instantly clear or will be clear later. please share the knowledge. 2) in case of dataset i clear like

                      dataset1.Dispose()

                      dataset1=null

                      GC.Collect()

                      but i am not sure that memory occupied by dataset1 is instantly clear or will be clear later. please share the knowledge. 3) if i call GC.Collect() several time in my one routine does degrade the performance of my application ? i found one link which said best option to release memory for list this way first clear List and then call TrimExcess. is it the right one ? i used like

                      list1=null & GC.Collect()

                      which one is best to clear memory for list instantly ? please see my code below which works well.

                      private void button2_Click(object sender, EventArgs e)
                      {
                      var before = System.Diagnostics.Process.GetCurrentProcess().VirtualMemorySize64;

                              List lst1 = new List();
                      
                              for (int i = 0; i <= 1000000; i++)
                              {
                                  lst1.Add("Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test ");
                              }
                      
                              var after = System.Diagnostics.Process.GetCurrentProcess().VirtualMemorySize64;
                      
                              lst1.Clear();
                              lst1.TrimExcess();
                      
                              var current = System.Diagnostics.Process.GetCurrentProcess().VirtualMemorySize64;
                          }
                      

                      please share best option to clear memory for list & dataset instantly. thanks

                      D Offline
                      D Offline
                      Dave Kreskowiak
                      wrote on last edited by
                      #13

                      What nobody has asked or said anything about is "What are you using to determine if memory is being freed?" If you're looking in Task Manager to see how much memory your app is using, DO NOT DO THAT! Task Manager is lying to you. It's showing you how much memory is RESERVED for your app, not how much it's actually using. When you start a .NET app, the .NET Common Language Runtime (CLR) gets a block of memory from Windows and makes that block the "managed heap". The code for you app is loaded and every object your app creates is allocated using memory from the managed heap. When your app is freeing objects up, the memory is returned to the managed heap, NOT to Windows. If the CLR needs more memory for your app, its asks Windows for another block of memory. If Windows needs more memory, it can ask the CLR to return any free memory its can spare, which the CLR will happily return. Task Manager is showing you the memory the CLR is holding onto, not how much your app is actually using. If you want to see how much your app is using, you have to use PerfMon and the .NET Memory counters.

                      Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                      Dave Kreskowiak

                      1 Reply Last reply
                      0
                      • M Mou_kol

                        what GC.WaitForPendingFinalizers(); does ? does it call garbage collector immediately or something else ?

                        R Offline
                        R Offline
                        realJSOP
                        wrote on last edited by
                        #14

                        GC.WaitForPendingFinalizers Method (System) | Microsoft Docs[^]

                        ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                        -----
                        You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                        -----
                        When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                        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