Problems with disposing rescources
-
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>
-
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>
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 ...
-
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 ...
I will try this! Thanks!! :-)
-
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>
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!
-
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!
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