• wpf内存泄漏问题


    http://www.cnblogs.com/Cindys/archive/2012/05/17/2505893.html

    http://blogs.msdn.com/b/jgoldb/archive/2008/02/04/finding-memory-leaks-in-wpf-based-applications.aspx

    NET Memory Profiler 托管环境内存检测工具,.net内存回收与Dispose﹐Close﹐Finalize方法

    http://blog.csdn.net/wlanye/article/details/7666121

    http://www.cnblogs.com/bluewater/archive/2011/04/12/2013748.html

     

    1、   如果用MVVM模式,View里面有图片,ViewModel里面有View引用,要把ViewModel里面的View设置为空,View里面的DataContext设置为空,不然有可能导致内存泄漏

    清除引用:

    this.Page.DataContext = null;

    this.Page = null;

    2、   类与类之间尽量不要互相引用,如果相互引用了要手动设置里面的引用为空,不然 会导致内存泄漏

    Class1 class1 =new Class1();

    Class2 class2 = new Class2();

    class1.Class2 = class2;

    class2.Class1 = class1;

    清除引用:

    class2.Class1 = null;

    class2 = null;

    class1.Class2 = null;

    class1 =null;

    3、   自定义控件里面有ImageBitMapSource属性值之类或者引用类属性时,要手动删除并设置为空

    CustomControl cc = new CustomControl();

    BitMapSource bms = new BitMapSource();

    bms.UriSource = ……;

    cc.Image = new Image(){Source= bms };

    清除引用:

    cc.Image=null;

    bms =null;

    4、   MVVM模式里面继承INotifyPropertyChangedViewModel里面的命令(CommandManager.RegisterClassCommandBinding)有可能导致内存泄漏

    protected ICommand CreateCommand(Action<ExecutedRoutedEventArgs> executed, Action<CanExecuteRoutedEventArgs> canExecute)

                  {

                var rCommand = new RoutedCommand();

                var cBinding = new CommandBinding(rCommand);

                CommandManager.RegisterClassCommandBinding(typeof(UIElement), cBinding);

                cBinding.CanExecute += (s, e) =>

                {

                    if (canExecute != null)

                        canExecute(e);

                    else

                        e.CanExecute = true;

                };

                cBinding.Executed += (s, e) =>

                {

                    executed(e);

                };

     

                         return rCommand;

                   }

    修改成:

    protected ICommand CreateCommand(Action<object> execute)

            {

                return new RelayCommand(execute);

            }

    public class RelayCommand : Icommand

    {

            ………….

    }

     

    5、   页面关闭时没结束的线程要结束线程

    6、   页面关闭时静态变量要设置为空

    7、   使用事件时,如果是一个类的事件在另一个类里面被注册(委托方法在这个类里面),要注销事件

    Window1.w2.TextBox1.TextChanged += new TextChangedEventHandler(this.TextBox1_TextChanged);

    Window1.w2.TextBox1.TextChanged -= new TextChangedEventHandler(this.TextBox1_TextChanged); 事件
    8、                   用静态事件时要注销事件
    9、                   在Image里面使用BitMapImage时要用

    BitmapImage bi = new BitmapImage();

    bi.BeginInit();

                    bi.CacheOption = BitmapCacheOption.OnLoad;

                    bi.UriSource = new Uri(path);

                    bi.EndInit();

                    bi.Freeze();

    10、        调用垃圾回收

    GC.Collect();
       GC.WaitForPendingFinalizers();
       GC.Collect();
    11、               如果绑定的数据源没有实现INotifyPropertyChanged,可能导致内存泄漏
     WPF 中,不标记为 OneTime 必须侦听属性的一个数据绑定操作从源对象 (对象 X 更改通知。WPF  INotifyPropertyChanged 界面使用 DependencyProperties 类的内置通知。 

    如果 DependencyProperties 类和 INotifyPropertyChanged 接口都不可用,WPF 使用 ValueChanged 事件。 此行为涉及到与属性 P 相对应的 PropertyDescriptor 对象上调用 PropertyDescriptor.AddValueChanged 方法。 遗憾的是,此操作会导致公共语言运行库 (CLR) 可以创建从此 PropertyDescriptor 对象 X 的强引用。 CLR 还保留全局表中的 PropertyDescriptor 对象的引用
    public class CustomCollectionClass : INotifyPropertyChanged
  • 相关阅读:
    Oracle
    Oracle
    Oracle
    PTA | 1012 数字分类 (20分)
    PTA | 1010 一元多项式求导 (25分)
    PTA | 1009说反话(20分)
    PTA | 1008 数组元素循环右移问题 (20分)
    PTA | 1005 继续(3n+1)猜想 (25分)
    LeetCode 题解 | 70. 爬楼梯
    LeetCode 题解 | 242. 有效的字母异位词
  • 原文地址:https://www.cnblogs.com/swarb/p/9924270.html
Copyright © 2020-2023  润新知