Freeing up memory

Started by
8 comments, last by Jaap85 11 years, 6 months ago
Hello everybody,

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.

My personal blog on game development!

Black Wolf Game Development

Advertisement
There are 2 different ways to approach this problem and the solution will depend on what your requirements are:

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.
You could check for your criteria before you add it to the list and filter it out that way.

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.
It's more readable to LINQ it. The following code will replace "list" with a list that only contains the items you want to keep:

list = list.Where(x => x meets criteria).ToList();


Alternatively you can do this as well:


for (int i=0; i < list.Count; )
if (list meets criteria)
list.RemoveAt(i);
else ++i;


(edit)

Even better, there is a RemoveAll method:

http://msdn.microsoft.com/en-us/library/wdka673a.aspx
In Java, you have to use the Iterator method. Foreach in Java wouldn't let you remove an item while iterating the list.

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 meets criteria) {
list.RemoveAt(i);
}
}
Thank you very much for all the solutions. I will give it a try and see what happens!

My personal blog on game development!

Black Wolf Game Development

To make a guess, close the web connection, then try removing from the list. There's no reason why System.Collections.List or System.Collections.Generic.List<T> themselves should stop you from removing items (aside from out-of-bounds, not-found errors and the like).
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;
if (item.condition)
list.RemoveAt(i);
}
[/source]

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;
if (item.condition)
list.RemoveAt(i);
}
[/source]

*Slaps forehead* Probably shouldn't have posted in the very early AM.
Sorry for my slow response (have been a bit busy with things other than programming the past few days) but i tried it out and it works! Where my program used to use up to 2GB, it now uses only 40 MB of processor power. Much better!

Thank you very much for your responses.

My personal blog on game development!

Black Wolf Game Development

This topic is closed to new replies.

Advertisement