using System.ComponentModel;
using System.Runtime.InteropServices;
//内存的分配
int n = 100000;//长度
IntPtr buffer = Marshal.AllocHGlobal(sizeof(int) * n);
try
{
var t = buffer + (n * 10) * sizeof(int);
var p = Marshal.PtrToStructure<int>(t);
//内存的分配2
//拆箱:拆箱就是将一个引用型对象转换成任意值型!比如:
int i = 0;
System.Object obj = i;
int j = (int)obj;
//装箱:装箱就是隐式的将一个值型转换为引用型对象。比如:
//int i = 0;
//Syste.Object obj = i;
//内存的回收
//GC.Collect();
}
catch (Exception e)
{
Console.WriteLine(e);
}
//内存的释放
Marshal.FreeHGlobal(buffer);
Console.WriteLine("Hello, World!");
Console.Read();
//WPF 内存的保护
public class MyViewModel
{
public string _someText = "memory leak";
public string SomeText
{
get { return _someText; }
set
{
_someText = value;
}
}
}
//using 也可以避免内存泄露保护 Dispose方法 内存的保护
//using (MemoryStream stream = new MemoryStream())
//{
// // ...
//}