#1 Members - Reputation: 220
Posted 26 September 2012 - 02:43 PM
I just created a small test program to see how it would worked after i compiled it. The program was created in Visual Studio using C#. The main thing it does, is adding content from a web page (by reading html source code) to a list.
At some point in the program, i try to remove items from the list (to clear memory usage). However, i run into a problem. I want to remove all items in a list that meet a certain criterium. My code is as follows:
foreach item in list:
> if item.status meets criterium
> list.remove(item)
However, this code doesn't work. The error i get is that i am not allowed to alter the list inside the for loop.
How could i fix this problem? Right now my (very simple) program uses about 2 GB of memory after running for an hour or so.
Thank you very much for your ideas and advice!
PS. if removing items of the list probably won't fix my memory issues i would be happy to hear any other ideas or tips.
#2 Members - Reputation: 190
Posted 26 September 2012 - 03:09 PM
Solution #1 iterate through a cloned list:
[source lang="java"]List<String> list;List<int> cloneList = new List<int>(list);foreach item in cloneList: if(item.status meets criterium) list.remove(item)[/source]
Solution #2 Store the items to remove in another list:
[source lang="java"]List<String> list;List<String> removeList;foreach item in list: if(item.status meets criteria) removeList.add(item)foreach item in removeList: list.remove(item);[/source]
Solution number one will work if you don't mind using a clone/copy function. Sometimes coping can be expensive though but probably not in your case.
Solution number two does only the minimum amount of memory usage and copying based on the implementation of the add function.
#3 GDNet+ - Reputation: 626
Posted 26 September 2012 - 03:09 PM
Another option is while looping through your data list create a second reference list of items to be deleted.
Then delete everything you want to delete outside of your foreach loop.
#4 Members - Reputation: 1867
Posted 26 September 2012 - 04:04 PM
list = list.Where(x => x meets criteria).ToList();
Alternatively you can do this as well:
for (int i=0; i < list.Count; ) if (list[i] meets criteria) list.RemoveAt(i); else ++i;
(edit)
Even better, there is a RemoveAll method:
http://msdn.microsoft.com/en-us/library/wdka673a.aspx
Edited by Nypyren, 26 September 2012 - 04:17 PM.
#5 Members - Reputation: 1436
Posted 26 September 2012 - 05:10 PM
After a minute of Google-fu, it seems that there's no other way in C# but to iterate the list using the for-loop. It'd be easier for you if you iterate backwards.
Using Nypyren's code
for (int i=list.Count-1; i >= 0; --i) {
if (list[i] meets criteria) {
list.RemoveAt(i);
}
}
Edited by alnite, 26 September 2012 - 05:12 PM.
#6 Members - Reputation: 220
Posted 29 September 2012 - 09:41 AM
#7 Members - Reputation: 305
Posted 29 September 2012 - 10:58 PM
#8 Members - Reputation: 1006
Posted 30 September 2012 - 04:15 AM
[source lang="csharp"]for (int i=list.length-1; i>=0; i--){ var item = list[i]; if (item.condition) list.RemoveAt(i);}[/source]
#9 Members - Reputation: 305
Posted 30 September 2012 - 01:57 PM
*Slaps forehead* Probably shouldn't have posted in the very early AM.Yeah, you can't remove items from a list while iterating over it with an iterator. You can however do this:
[source lang="csharp"]for (int i=list.length-1; i>=0; i--){ var item = list[i]; if (item.condition) list.RemoveAt(i);}[/source]
#10 Members - Reputation: 220
Posted 08 October 2012 - 01:47 PM
Thank you very much for your responses.






