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. Problems with disposing rescources

Problems with disposing rescources

Scheduled Pinned Locked Moved C#
sysadmindata-structuresperformancehelptutorial
5 Posts 3 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.
  • K Offline
    K Offline
    Kaare Tragethon
    wrote on last edited by
    #1

    I'm a newbie in computer programming so please have this in mind. I have a class called Pinger which does basic pinging of hosts on a network. I'm currently implementing a network scanner so I create an array (254) of Pinger's. The problem occurs when I try to dispose the array after I've done the scan. I dispose every Pinger in the array - the Pinger class contains Dispose methods (GC.SuppressFinalize). Still, the array of Pinger's still uses a lot of rescources on my system and I can clearly see the memory beeing freed when I close my application. Does anyone have any idea on how to correctly get rid of the rescources being used (after I'm finished using them)? I have the complete project, but couldn't find a way to upload it so if anyone want's to see it let me know how I can send you the project.....

        private void AllPingsFinished()
        {
            //Clears the counter for the list-view
            counterForInsert = 0;
    
            //Loops through all the pingers in the array (which contains data)
            foreach (Pinger str in \_pinger)
            {
                if (str != null)
                {
                    //Disposes the current pinger (if not null)
                    str.Dispose();
                }
            }
        }pre>
    
    L L 2 Replies Last reply
    0
    • K Kaare Tragethon

      I'm a newbie in computer programming so please have this in mind. I have a class called Pinger which does basic pinging of hosts on a network. I'm currently implementing a network scanner so I create an array (254) of Pinger's. The problem occurs when I try to dispose the array after I've done the scan. I dispose every Pinger in the array - the Pinger class contains Dispose methods (GC.SuppressFinalize). Still, the array of Pinger's still uses a lot of rescources on my system and I can clearly see the memory beeing freed when I close my application. Does anyone have any idea on how to correctly get rid of the rescources being used (after I'm finished using them)? I have the complete project, but couldn't find a way to upload it so if anyone want's to see it let me know how I can send you the project.....

          private void AllPingsFinished()
          {
              //Clears the counter for the list-view
              counterForInsert = 0;
      
              //Loops through all the pingers in the array (which contains data)
              foreach (Pinger str in \_pinger)
              {
                  if (str != null)
                  {
                      //Disposes the current pinger (if not null)
                      str.Dispose();
                  }
              }
          }pre>
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      I don't think that you can remove object from Collection while it's looping In Other word // This is Wrong foreach ( string str in MyList) { MyList.remove(str); } // This is Right List ListForDetele = new List(); foreach (string str in MyList) { ListForDelete.Add(str); } foreach(string str in ListForDelete) { MyList.Remove(str); } Have a good day ,

      I know nothing , I know nothing ...

      K 1 Reply Last reply
      0
      • L Lost User

        I don't think that you can remove object from Collection while it's looping In Other word // This is Wrong foreach ( string str in MyList) { MyList.remove(str); } // This is Right List ListForDetele = new List(); foreach (string str in MyList) { ListForDelete.Add(str); } foreach(string str in ListForDelete) { MyList.Remove(str); } Have a good day ,

        I know nothing , I know nothing ...

        K Offline
        K Offline
        Kaare Tragethon
        wrote on last edited by
        #3

        I will try this! Thanks!! :-)

        1 Reply Last reply
        0
        • K Kaare Tragethon

          I'm a newbie in computer programming so please have this in mind. I have a class called Pinger which does basic pinging of hosts on a network. I'm currently implementing a network scanner so I create an array (254) of Pinger's. The problem occurs when I try to dispose the array after I've done the scan. I dispose every Pinger in the array - the Pinger class contains Dispose methods (GC.SuppressFinalize). Still, the array of Pinger's still uses a lot of rescources on my system and I can clearly see the memory beeing freed when I close my application. Does anyone have any idea on how to correctly get rid of the rescources being used (after I'm finished using them)? I have the complete project, but couldn't find a way to upload it so if anyone want's to see it let me know how I can send you the project.....

              private void AllPingsFinished()
              {
                  //Clears the counter for the list-view
                  counterForInsert = 0;
          
                  //Loops through all the pingers in the array (which contains data)
                  foreach (Pinger str in \_pinger)
                  {
                      if (str != null)
                      {
                          //Disposes the current pinger (if not null)
                          str.Dispose();
                      }
                  }
              }pre>
          
          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Hi, it is wise to call Dispose() on an object you no longer need, so it can release its unmanaged resources in a deterministic way. However that does not take care of the managed resources, i.e. the object is still alive (as your array is alive and holds a reference to it). e.g. if each Pinger holds a thread, then that thread (and its stack memory) are still allocated (unless you cleaned them too in Dispose). so it would also be good to remove the reference from the array as soon as you don't need them.

          Kaare Tragethon wrote:

          I can clearly see

          do you? how? Task Manager is not the right place to get accurate information on this subject. It shows the working set, not how much memory the app NEEDS. The differences are: - the app may need to hold that amount of data; - or the app may have asked that amount of memory because that was the fastest way to get things done, but in reality lots of memory are unused INSIDE the app (instead of inside the Window memory pool). Test: minimize your main form. Does that dratically reduce the "memory requirement"? if yes, point proven. More comments: - why do you need 254 Pingers all at once? do they really all work in parallel? do you have that many threads? AFAIK TCP/IP will choke. I tend to use say 8 threads and let each of them ping the addresses one after another (one job list, each thread picks the next job in turn). - I tend to use a plural noun for a collection, so I would write foreach(Pinger pinger in pingers)... :)

          Luc Pattyn


          I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


          Local announcement (Antwerp region): Lange Wapper? Neen!


          K 1 Reply Last reply
          0
          • L Luc Pattyn

            Hi, it is wise to call Dispose() on an object you no longer need, so it can release its unmanaged resources in a deterministic way. However that does not take care of the managed resources, i.e. the object is still alive (as your array is alive and holds a reference to it). e.g. if each Pinger holds a thread, then that thread (and its stack memory) are still allocated (unless you cleaned them too in Dispose). so it would also be good to remove the reference from the array as soon as you don't need them.

            Kaare Tragethon wrote:

            I can clearly see

            do you? how? Task Manager is not the right place to get accurate information on this subject. It shows the working set, not how much memory the app NEEDS. The differences are: - the app may need to hold that amount of data; - or the app may have asked that amount of memory because that was the fastest way to get things done, but in reality lots of memory are unused INSIDE the app (instead of inside the Window memory pool). Test: minimize your main form. Does that dratically reduce the "memory requirement"? if yes, point proven. More comments: - why do you need 254 Pingers all at once? do they really all work in parallel? do you have that many threads? AFAIK TCP/IP will choke. I tend to use say 8 threads and let each of them ping the addresses one after another (one job list, each thread picks the next job in turn). - I tend to use a plural noun for a collection, so I would write foreach(Pinger pinger in pingers)... :)

            Luc Pattyn


            I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


            Local announcement (Antwerp region): Lange Wapper? Neen!


            K Offline
            K Offline
            Kaare Tragethon
            wrote on last edited by
            #5

            Thanks a LOT to both of you! I will definetively try a combination of both your answers (max 8 pingers, and add them to a list)! Best regards

            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