Objects can be created using new or IOC frames such as: Castle, MEF, etc., the life cycle of an IOC-created object, which may be administered by the IOC, and if the developer using the framework does not know it may cause a memory leak.
These memory leaks are not a bug in the IOC framework, but a memory leak caused by improper use of the developer or unnoticed.
Use MEF as an example to illustrate two of the memory leaks that I have encountered.
Memory Leak series Reading tips:
A touch of the same object graph, sometimes we can think of it as a memory leak, and sometimes think it is not a memory leak, all this is only because of the context, this series of articles ants Memoery profle have a specific context, alone see completely meaningless. How to determine a memory leak? You can refer to the previous article.
Objects exist in the form of graphs, and Ants Memory profile is useful for analysis to facilitate the processing of these graphs into trees, so that we can focus on the object of analysis. But we must understand the object relationship in memory diagram, that is, the ants tree is only a part of the distribution of objects in memory, the analysis of memory leaks must have a global concept, need to be related to a number of graphs to see together, even if a picture to look at the overall, so as to analyze the memory leak problem.
Because there is no point in looking at a picture, if you put more than one picture, this article is too difficult to write, even if more than a picture is posted, it may not be able to express clearly, analysis of memory leakage is the most important experience. The next few will reduce or even use the ants diagram.
Partcreationpolicy memory leaks due to improper use
Shared : Object to Singleton mode creation.
Non gkfx : Creates a new object each time the request is made.
any : be default, Compositioncontainer'll use Shared, unless the Composablepart or importer requests nonshared. Default value
Partcreationpolicy has different default values when using different combinations, so try not to use the default values.
Look at the following program:
Navigationcommand: Because of the need to navigate multiple view pages, the navigation is placed in the class Navigationcommand, it is precisely this approach leads to the memory leak problem.
1 |
<div
class
=
"cnblogs_code"
><div
class
=
"cnblogs_code_toolbar"
><span
class
=
"cnblogs_code_copy"
><a href=
"javascript:void(0);"
onclick=
"copyCnblogsCode(this)"
title=
"复制代码"
> "/img/fz.gif"
alt=
"复制代码"
></a></span></div><div><span style=
"color: #000000;"
>[Export(</span><span style=
"color: #0000FF;"
>
typeof
</span><span style=
"color: #000000;"
>(NavigationCommand))]<br><br></span><span style=
"color: #0000FF;"
>
public
</span><span style=
"color: #000000;"
> </span><span style=
"color: #0000FF;"
>
class
</span><span style=
"color: #000000;"
> NavigationCommand<br><br>{<br><br></span><span style=
"color: #0000FF;"
>
private
</span><span style=
"color: #000000;"
> RelayCommand</span><span style=
"color: #000000;"
><</span><span style=
"color: #0000FF;"
>
string
</span><span style=
"color: #000000;"
>></span><span style=
"color: #000000;"
> readBookComamand </span><span style=
"color: #000000;"
>=</span><span style=
"color: #000000;"
> </span><span style=
"color: #0000FF;"
>
null
</span><span style=
"color: #000000;"
>;<br><br></span><span style=
"color: #0000FF;"
>
public
</span><span style=
"color: #000000;"
> RelayCommand</span><span style=
"color: #000000;"
><</span><span style=
"color: #0000FF;"
>
string
</span><span style=
"color: #000000;"
>></span><span style=
"color: #000000;"
> ReadBookComamand<br><br>{<br><br></span><span style=
"color: #0000FF;"
>
get
</span><span style=
"color: #000000;"
><br><br>{<br><br></span><span style=
"color: #0000FF;"
>
if
</span><span style=
"color: #000000;"
> (navigatToContentCommand </span><span style=
"color: #000000;"
>==</span><span style=
"color: #000000;"
> </span><span style=
"color: #0000FF;"
>
null
</span><span style=
"color: #000000;"
>)<br><br>{<br><br>navigatToContentCommand </span><span style=
"color: #000000;"
>=</span><span style=
"color: #000000;"
> </span><span style=
"color: #0000FF;"
>
new
</span><span style=
"color: #000000;"
> RelayCommand</span><span style=
"color: #000000;"
><</span><span style=
"color: #0000FF;"
>
string
</span><span style=
"color: #000000;"
>></span><span style=
"color: #000000;"
><br><br>(<br><br>p </span><span style=
"color: #000000;"
>=></span><br><br>{<br><br><font
class
=
"Apple-style-span"
color=
"#008000"
>//<br></font>},<br><br>p <span style=
"color: #000000;"
>=></span><span style=
"color: #000000;"
> </span><span style=
"color: #000000;"
>!</span><span style=
"color: #0000FF;"
>
string
</span><span style=
"color: #000000;"
>.IsNullOrWhiteSpace(p)<br><br>);<br><br>}<br><br></span><span style=
"color: #0000FF;"
>
return
</span><span style=
"color: #000000;"
> readBookComamand;<br><br>}<br><br>}<br><br>}</span></div><div
class
=
"cnblogs_code_toolbar"
><span
class
=
"cnblogs_code_copy"
><a href=
"javascript:void(0);"
onclick=
"copyCnblogsCode(this)"
title=
"复制代码"
> "/img/fz.gif" alt=
"复制代码"
></a></span></div></div>
|
Viewmodle
[Viewmodelexport (typeof (Bookpagesearcheviewmodle), "Bookpagesearche")]
[Partcreationpolicy (creationpolicy.nonshared)]
public class Bookpagesearcheviewmodle:navviewmodel
{
Public relaycommand<string> Readbookcomamand
{
Get
{
Return Navigationcommand. Readbookcomamand;
}
}
}
Public abstract class Navviewmodel:viewmodelbase, Iviewmodel
{
<summary>
The navigation service allows you to navigate and the other ViewModel.
</summary>
[Import]
Public Inavigationservice NavigationService {get; set;}
[Import]
Public Navigationcommand Navigationcommand
{
Get
Set
}
}
View
[Viewexport (viewmodelcontract = typeof (Bookpagesearcheviewmodle))]
Public partial class Bookpagesearcheview:page, IView
{
Public Bookpagesearcheview ()
{
InitializeComponent ();
}
protected override void Onnavigatedfrom (NavigationEventArgs e)
{
if (this. DataContext is Icleanup)
{
((Icleanup) this. DataContext). Cleanup ();
}
}
}
}
In the example above, Navigationcommand is created by default in shared mode.
ANTS memory Profiel The object graph that generated the leak:
It can be seen that both Bookpagesearcheviewmodle and MEF refer to Navigationcommand, causing the Navigationcommand not to be released to create a memory leak, where only the MEF path is seen.
I was too focused on Bookpagesearcheviewmodle's references to Readbookcommand when dealing with this memory leak, rather than looking at the graphs in full, Disconnecting Bookpagesearcheviewmodle is the simplest way to handle readbookcommand references, but when dealing with other ViewModel, you will find that too many of the command needs to be handled, only to realize that the point of the broken reference is wrong.
Therefore, we must carefully look at the object graph, a path contains n points, break any point can solve the problem of this object leakage, but a program has n objects, this only to focus on the current object processing mode can not fundamentally solve the problem, must find the source of memory leaks .
The simplest thing to do to solve the MEF leak problem is to add: [Partcreationpolicy (creationpolicy.nonshared)]
You can also enable the Navviewmodel class to implement all the command, convert the combination to inheritance, and break the long reference.
Conclusion
A) do not blindly use third-party frameworks.
b) The design-time combination takes precedence over inheritance, but the combination is more prone to memory leaks, because if either party survives for a long time, the memory will not be freed. Inheritance is not the problem.
c) Note the object life cycle created by the IOC, and if the IOC creates an object that is managed by the container for the lifetime, it may be necessary to invoke the related method provided by IOC to perform the destruction of the object.
e) MVVM is more prone to memory problems than the code behind approach. V, VM bidirectional reference relationship, more likely to cause memory not to be released
Http://www.cnblogs.com/bluewater/archive/2011/04/14/2016288.html
The IOC framework such as MEF causes memory leaks-partcreationpolicy