线程栈 stuck:存值类型,和引用类型的引用 先进后出,链表形式,连续摆放 CLR(公共语言运行库(Common Language Runtime))启动进程,main函数为一个线程入口数据库
进程堆heap:存引用类型 进程中的一块区域小程序
IL:中间语言app
对象的属性为值类型出如今堆里,方法里的值类型,由进程调用,出如今栈里函数
/// <summary> /// class 引用类型 /// </summary> public class ReferenceTypeClass { private int _valueTypeField;//堆:由于对象都在堆里,对象里面的属性也在堆里 public ReferenceTypeClass() { _valueTypeField = 0; } public void Method() { int valueTypeLocalVariable = 0;//栈:全新的局部变量,线程栈来调用方法,而后分配内存 new Process();//new对象,分配在堆里 } }
装箱拆箱(仅仅是说内存的拷贝动做):内存copy 也会浪费性能 一般都是由于object,性能
装箱拆箱只能发生在父子类里面, 由于这样你才能转换呀this
dynamic 是引用类型的语法糖spa
string student = "123"; string student2 = student; Console.WriteLine(student); //123 Console.WriteLine(student2);//123 student2 = "APP"; Console.WriteLine(student);//123 Console.WriteLine(student2);//APP
string student = "大山"; string student2 = "APP";//共享 student2 = "大山"; Console.WriteLine(object.ReferenceEquals(student, student2));//true 居然同样 //就是同一个 享元模式 CLR内存分配字符串的时候,会查找相同值,有就重用了
托管资源:.Net New的类,出了做用域就访问不到了,自动释放了,存放在进程堆中的资源; 值类型变量,存放在进程栈中,出了做用域就是放了,线程
非托管资源:访问数据库,操做Excel,Word什么的.code
析构函数 ~Class() 见下图orm
public class Class : IDisposable { public int ClassId { get; set; } public string ClassName { get; set; } ~Class() { MyLog.Log($"执行{this.GetType().Name}Dispose"); } public void Dispose() { MyLog.Log($"执行{this.GetType().Name}Dispose"); } }
主要是用来释放非托管资源,等着GC去把非托管资源释放掉 系统自动执行
GC.Collect();//主动GC(释放资源) GC回收的时候,CLR必定调用析构函数
Dispose() 也是释放非托管资源的,主动释放,方法自己是没有意义的,咱们须要在方法里面实现对资源的释放
GC不会调用,而是用对象时,使用者主动调用这个方法(using),去释放非托管资源
总结:
1.循环New100个对象,出了做用域,就访问不到了,没有引用,.Net GC就释放了,但少New对象,建立对象须要内存开辟空间
2.实现Idispose() 接口的 使用Using 或调用dispose() 方法
3.操做Word或Excel 的时候 时候须要注意释放资源
前一段时间作了一个小程序,Word批量转PDF,运行的时候内存持续增长,加上释放资源就行了,见下图
/// <summary> /// word 操做类 /// </summary> Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application(); /// <summary> ///word 转换成pdf /// </summary> /// <param name="sourcePath"></param> /// <param name="targetPath"></param> /// <returns></returns> public bool WordToPDF(string sourcePath, string targetPath) { bool result = false; Microsoft.Office.Interop.Word.Document _document = null; // application. try { application.Visible = false; _document = application.Documents.Open(sourcePath); _document.ExportAsFixedFormat(targetPath, Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF); result = true; } catch (Exception e) { Console.WriteLine(e.Message); result = false; } finally { var doc_close = (Microsoft.Office.Interop.Word._Document)_document; if (doc_close != null) { doc_close.Close(); } else { result = false; } // _document.Close(); } return result; }
4.访问数据库的时候还没看