Jump to content

  • Log In with Google      Sign In   
  • Create Account


Freeing up memory


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 Jaap85   Members   -  Reputation: 239

Like
1Likes
Like

Posted 26 September 2012 - 02:43 PM

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.

Sponsor:

#2 slicksk8te   Members   -  Reputation: 190

Like
0Likes
Like

Posted 26 September 2012 - 03:09 PM

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.

#3 yewbie   GDNet+   -  Reputation: 665

Like
0Likes
Like

Posted 26 September 2012 - 03:09 PM

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.

#4 Nypyren   Crossbones+   -  Reputation: 3636

Like
5Likes
Like

Posted 26 September 2012 - 04:04 PM

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[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 alnite   Crossbones+   -  Reputation: 2036

Like
0Likes
Like

Posted 26 September 2012 - 05:10 PM

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[i] meets criteria) {
		list.RemoveAt(i);
	}
}

Edited by alnite, 26 September 2012 - 05:12 PM.


#6 Jaap85   Members   -  Reputation: 239

Like
0Likes
Like

Posted 29 September 2012 - 09:41 AM

Thank you very much for all the solutions. I will give it a try and see what happens!

#7 Narf the Mouse   Members   -  Reputation: 318

Like
0Likes
Like

Posted 29 September 2012 - 10:58 PM

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).

#8 jefferytitan   Members   -  Reputation: 1585

Like
0Likes
Like

Posted 30 September 2012 - 04:15 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]

#9 Narf the Mouse   Members   -  Reputation: 318

Like
0Likes
Like

Posted 30 September 2012 - 01:57 PM

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]

*Slaps forehead* Probably shouldn't have posted in the very early AM.

#10 Jaap85   Members   -  Reputation: 239

Like
0Likes
Like

Posted 08 October 2012 - 01:47 PM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS