|
-
Jan 11th, 2012, 11:57 AM
#1
Re: WinForms Memory Leak
You RandomDataHelper class has a Shared List of RandomData objects. A shared object like that list can't go out of scope while the program is running, because it belongs to the class not to an instance. So every time you instantiate a GridForm, the call to RandomDataHelper adds another 10,000 RandomData items to the list. I wonder if that is the cause of your memory leak?
Maybe you could fix that by using List.Clear before adding the new items, although a better design could be to get rid of the Shared keywords in RandomDataHelper. Then you could instantiate and dispose of it every time you need it, for example with a Using block in GridForm.Load:
Code:
Using RDH As New RandomDataHelper
_randomData = RDH.GetRandomData()
End Using
BB
-
Jan 11th, 2012, 01:04 PM
#2
Thread Starter
Member
Re: WinForms Memory Leak
 Originally Posted by boops boops
You RandomDataHelper class has a Shared List of RandomData objects. A shared object like that list can't go out of scope while the program is running, because it belongs to the class not to an instance. So every time you instantiate a GridForm, the call to RandomDataHelper adds another 10,000 RandomData items to the list. I wonder if that is the cause of your memory leak?
Maybe you could fix that by using List.Clear before adding the new items, although a better design could be to get rid of the Shared keywords in RandomDataHelper. Then you could instantiate and dispose of it every time you need it, for example with a Using block in GridForm.Load:
Code:
Using RDH As New RandomDataHelper
_randomData = RDH.GetRandomData()
End Using
BB
Thanks for the reply. I don't think this is the cause because of the massive amount of memory that is sometimes cleared. The static (shared) class was to keep the GUIDs in memory and generate lists from them because generating GUIDs every time took too long. I should really do it properly and read from a file or a database to eliminate this but I wanted to exclude unmanaged resources. I will make some changes and see what effect they have
-
Jan 11th, 2012, 11:04 PM
#3
Re: WinForms Memory Leak
 Originally Posted by boops boops
You RandomDataHelper class has a Shared List of RandomData objects. A shared object like that list can't go out of scope while the program is running, because it belongs to the class not to an instance. So every time you instantiate a GridForm, the call to RandomDataHelper adds another 10,000 RandomData items to the list. I wonder if that is the cause of your memory leak?
Not sure if anyone has responded to this yet (can't see anything but I may have missed it).
I don't think that is an issue. When the RandomDataHelper.GetRandomData method is called, it only allocates the list object and populates it with data the first time through. Subsequent calls to the method create a new List with new Data objects that get returned, but are not stored in the Shared member.
This will allocate a bunch of memory that is never released only on the first time the method is called. Subsequent to that, the memory allocated will be collectible before the end of the program.
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
|