Sign in to follow this  
Jaap85

Freeing up memory

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 :) :
[CODE]
for (int i=list.Count-1; i >= 0; --i) {
if (list[i] meets criteria) {
list.RemoveAt(i);
}
}[/CODE] Edited by alnite

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
[quote name='jefferytitan' timestamp='1349000156' post='4985306']
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]
[/quote]
*Slaps forehead* Probably shouldn't have posted in the very early AM.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this