• C#内存泄漏的事例


    C#内存泄漏的事例

      一,使用非托管资源忘记及时Dispose

        (1)  使用完非托管资源一定要Dispose或者使用using

                using (FileStream fsWrite = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    string str = "我好像不能全部覆盖源文件中的数据";
                    byte[] buffer = Encoding.Default.GetBytes(str);
                    fsWrite.Write(buffer,0,buffer.Length);//无返回值,以字节数组的形式写入数据
                }
           string path =@"C:Usersfighting  manDesktopFileStream的使用vs快捷键.txt" ;
                FileStream fsRead = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read);
                //三个参数(参数1读取文件的路径,参数2对文件的做什么样的操作,参数3对文件中的数据做什么样的操作)
                //FileStream 用来操作字节(不是一次性读取对内存压力小适合读大文件)
                try
                {
                    //创建FileStream类的对象(路径,对文件的操作,对文本文件数据的操作)
                    byte[] buffer = new byte[1024 * 1024 * 1];
                    int r = fsRead.Read(buffer, 0, buffer.Length);//把数据读到字节数组中,返回实际读到的有效字节数
                    string str = Encoding.Default.GetString(buffer, 0, r);//解码到实际读到的字节数
                }
                finally
                {
                    fsRead.Close();//关闭流
                    fsRead.Dispose();//释放流
                }

                  非托管资源还包括OracleConnection,套接字,com对象,操作excel对象等,使用完毕一定要手动Dispose。

              (2)定义的自定义类里使用了非托管资源,需要继承接口IDisposable,实现Dispose方法,手动清理掉内部非托管资源。  

    public class DealData : IDisposable
        {
            private bool disposed = false;
    
            private System.Timers.Timer t = new System.Timers.Timer();
    
            private List<object> listAll;
    
            public void Dispose()
            {
                Dispose(true);
                GC.SuppressFinalize(this);
            }
    
            protected virtual void Dispose(bool disposing)
            {
                if (!disposed)
                {
                    if (disposing)
                    {
                        listAll.Clear();
                        //释放托管资源
                    }
                    t.Dispose();
                    //释放非托管资源
                    disposed = true;
                }
            }
    
            ~DealData() {
                MessageBox.Show("析构函数");
            }
        }

              二,静态引起的内存泄漏现象

                    (1)静态对象导致的内存堆积,程序不执行完毕,内存就不能释放

    public class MySingletonClass
    {
        private static MySingletonClass myInstance;
        private static List<IAmBig> bigObjects = new List<IAmBig>();
        private MySingletonClass(){}
        public static MySingletonClass MyInstance
        {
            get
            {
                if(myInstance == null)
                {
                    myInstance = new MySingletonClass();
                }
                return myInstance;
            }
        }
        public static IAmBig CreateBigObject()
        {
            var bigObject = new IAmBig();
            bigobject.AllocateMemory(4096);
            bigObjects.add(bigObject);
            return bigObject;
        }
    }
    public class IAmBig
    {
        
    }

     

  • 相关阅读:
    为什么说2013是PHP年
    wordpress 投稿插件 支持图片上传
    php简易页面内调试技巧
    WordPress中文文档
    百度网盘文件直链
    HOWTO:如何解决安装包在系统“添加/删除”中无法修复或卸载的问题
    InstallShield 2008 终止声明 (EOL)对最终客户意味着什么
    InstallShield 2011新功能试用(10) Express版本
    AdminStudio 9.5 Service Pack 3
    INFO:InstallShield中安装路径变量的区别
  • 原文地址:https://www.cnblogs.com/LGDD/p/9522952.html
Copyright © 2020-2023  润新知