|
-
July 20th, 2011, 09:06 AM
#1
Increasing memory related to list allocation?
Using a few memory profilers I am seeing that the memory usage of my program is increasing due to an issue with code that looks like what is shown below. A 'for' loop creates a new instance of MyClass with each iteration and adds it to the ArrayList. Even though myList is cleared each time MyMethod is called, the memory is still increasing and not being cleaned up properly. I read somewhere that this type of code can lead to a managed code memory leak but it didn't say how to fix it. Any suggestions?
Code:
public class MainClass
{
private int max = 50;
private ArrayList myList = new ArrayList();
public MyMethod()
{
myList.Clear();
for (int i = 0; i < max; i++)
{
MyClass myvar = new MyClass()
myList.Add(myvar);
}
}
}
"My software doesn't have bugs, it just develops random features."
-
July 20th, 2011, 10:11 AM
#2
Re: Increasing memory related to list allocation?
My understanding is that with managed code such as yours, garbage collection is not constant, it is done periodically and cleans up any objects that are no longer referenced. So I do not believe your scenario would create a real leak, it may just be a while before GC gets around to cleaning up some released references.
-
July 20th, 2011, 10:21 AM
#3
Re: Increasing memory related to list allocation?
I thought that might be the case too. So even though it is bad coding practice, I put a GC.Collect() right after this code executes, but the memory still continued to increase. I thought perhaps since I'm creating a new object every time an item is added to the list, that the list was holding onto a reference of that object and it couldn't be garbage collected.
"My software doesn't have bugs, it just develops random features."
-
July 20th, 2011, 10:44 AM
#4
Re: Increasing memory related to list allocation?
Well, I'm afraid I can't be of any more in-depth help on the subject... but I'm thinking it's not really a leak, as much as the list just hasn't left the scope of Main{} yet.
Here is one article I came accross, and perhaps what they say about the ArrayList retaining the size when cleared has something to do with it? You could try setting your list to null then re-creating it to fill it... see what that does... ?
http://stackoverflow.com/questions/2...free-up-memory
Good luck!
-
July 20th, 2011, 11:13 AM
#5
Re: Increasing memory related to list allocation?
There is no leak in that code, and the term "managed memory leak" is arguably an oxymoron anyway. Regardless, the code you posted is fine. All that could happen is that the last batch of objects added to the list are never cleaned up because it is not cleared again, which is probably fine (and why are you using ArrayList anyway? Use List<T>, this isn't .NET 1.1 anymore...).
You have something else going on. I suggest you use a good profiler which tracks GC roots and see what is keeping your objects alive (event handlers, dead references, whatever). Don't just sprinkle calls to GC.Collect() around hoping that will help, because it almost certainly will not.
-
July 20th, 2011, 11:18 AM
#6
Re: Increasing memory related to list allocation?
If you're calling that, are you also setting it to nothing? Creating many classes will increase memory, as mentioned
-
July 20th, 2011, 12:15 PM
#7
Re: Increasing memory related to list allocation?
There is really no need to set the list to null each time. It is a single list that is cleared an repopulated N times; there will be no appreciable cost for that list, and setting it to null before creating a new instance of it would save nothing. As long as we are micro-optimizing, that would actually be more expensive due to the extra heap allocations.
-
July 22nd, 2011, 05:51 AM
#8
Re: Increasing memory related to list allocation?
But it is question how clear is internaly implemented. I don't know, but I can imagine (althought it doens't semm to be smart) that clear just new internal array[] as a storage without leaven the recent as a subject to GC.
Another explanation comming to my mind is that the array or the instances of MyClass are allocated in Large Object Space and GC doesn't take care about them.
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post. 
-
July 22nd, 2011, 07:45 AM
#9
Re: Increasing memory related to list allocation?
Firstly you should be using List<> rather than ArrayList. ArrayList is included for compatibility with .NET 1.*
Lists internally create an array which doubles in size whenever necessary.
Clear() doesn't clear the memory - it just sets the internal 'size' member variable to 0.
If you genuinely want the memory to be reset instead of Clear() you should create a new instance of the list.
A better approach if you know the size before going into the loop is not to use a list at all, but use an array which you create at the start i.e.
Code:
public class MainClass
{
private int max = 50;
private MyClass [] myList;
public MyMethod()
{
myList = new MyClass[max]; // if max will change, if not create in constructor
for (int i = 0; i < max; i++)
{
MyClass myvar = new MyClass()
myList[i] = myvar;
}
}
}
Last edited by darwen; July 22nd, 2011 at 07:49 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|