MVVM: Dynamic Views and memory leak
-
Tuesday, May 08, 2012 10:52 AM
I have a long-lived ViewModel and a dynamic View that is added to the visual tree whenever the user wants to see the ViewModel. When the user closes the View, it is removed from the visual tree. I intentionally put 50mb of dummy data in the View(UserControl):
private byte[] data = new byte[50 * 1024 * 1024];I watched the Process Explorer memory shoots up when I add the View onto the visual tree. I closed the View(unloading it from the visual tree) and then explicitly called the Garbage collect method:
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();However, the memory didn't reduced and whenever i add the View onto the visual tree, another 50mb is added. I am sure my ViewModel is not referencing the View. I used the dotTrace memory profiler and found that there is no reference from the ViewModel. All references to the "data" variable are UI objects. Most of it are from template. How should I correctly solve this issue?
Any suggestions to solve this problem or explain to me why this is so?
Thanks!
All Replies
-
Tuesday, May 08, 2012 1:24 PMThe GC will not collect anything that still has event logic hooked up. Make sure you've disconnected all event wiring.
JP Cowboy Coders Unite!
-
Wednesday, May 09, 2012 2:27 AMThere are no events hooked up.
-
Wednesday, May 09, 2012 3:19 AM
You never have full control over the GC. It is a suggestion only. If memory does not need to be freed it won't do it unitl IT decides to do it.
http://msdn.microsoft.com/en-us/library/ee787088.aspx
JP Cowboy Coders Unite!
-
Wednesday, May 09, 2012 3:22 AM
Wait a second, that was true, but it may be fixed now.... I just read GC.Collect now responds to user method call... I do remember they did some major rework of GC in .NET 4.0
Are you using 4.0?
JP Cowboy Coders Unite!
-
Thursday, May 10, 2012 7:25 AMModerator
Hi icube,
it is hard to find the root cause of your issue without repro it, I suggest you read this article to find the cause, and then adpat it to your specific needs:
best regards,
Sheldon _Xiao[MSFT]
MSDN Community Support | Feedback to us
Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
-
Friday, May 11, 2012 5:48 AM
i have a sample app here i uploaded to skydrive. if anyone can look at it.. that would be good.
https://skydrive.live.com/redir.aspx?cid=ec790852be322dbd&resid=EC790852BE322DBD!249&parid=EC790852BE322DBD!203&authkey=!ACOyvd1jawJZX-0
the file is named MemLeak
- Edited by icube Friday, May 11, 2012 5:49 AM
-
Monday, May 14, 2012 7:55 AMModerator
Hi icube,
I am working on your issue, if I find the root cause, I will update this thread.
Best regards,
Sheldon _Xiao[MSFT]
MSDN Community Support | Feedback to us
Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
-
Monday, June 11, 2012 3:00 PMAny news Sheldon?

