zoukankan      html  css  js  c++  java
  • C# 内存释放 IDisposable 与 析构方法的 关系

    xx.csproj true 允许不安全代码和指针

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net5.0</TargetFramework>
        <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
      </PropertyGroup>
    
      <PropertyGroup>
        <InvariantGlobalization>true</InvariantGlobalization>
      </PropertyGroup>
    
    </Project>
    
    

    Program.cs

    #define RUNDISPOSE  // 注释预定义切换
    
    using System;
    using System.Runtime.InteropServices;
    
    namespace Cmd
    {
        class Program
        {
            static void Main(string[] args)
            {
                Test();
                GC.Collect();
                Console.WriteLine("Memory used after full collection:   {0:N0}", GC.GetTotalMemory(true));
                Console.WriteLine("Hello World!");
            }
    
            public static void Test()
            {
                
                unsafe
                {
                    #if RUNDISPOSE
                    using var _net = new NetworkChangeCleanup(int.MaxValue);
                    #else 
                    var _net = new NetworkChangeCleanup(int.MaxValue);
                    #endif
                    Console.WriteLine("size: {0}", Marshal.SizeOf<NetworkChangeCleanup>(_net));
                    Console.WriteLine("object is {0}.", _net._handler.ToString());
                }
    
                
    
            }
        }
    
        [StructLayout(LayoutKind.Sequential)]
        sealed class NetworkChangeCleanup : IDisposable
        {
            public int _handler;
    
            public NetworkChangeCleanup(int handler)
            {
                _handler = handler;
            }
    
            // If user never disposes the HttpClient, use finalizer to remove from NetworkChange.NetworkAddressChanged.
            // _handler will be rooted in NetworkChange, so should be safe to use here.
            ~NetworkChangeCleanup() => Console.WriteLine("Finalized.");
    
            public void Dispose()
            {
                _handler = 0;
                Console.WriteLine("disposed.");
    
                #if RUNDISPOSE
    
                GC.SuppressFinalize(this); // 试试注释该方法
    
                #endif
            }
        }
    }
    
    
  • 相关阅读:
    SQLSERVER 根据传入的参数拼接sql语句字符串,反馈结果集
    SQLSERVER 时间函数汇总
    在网页中加入百度地图
    关于收到谷歌邮件 Googlebot can't access your site 的解决方法
    phoneGap 3.5 eclipise 模拟器调试
    将MongoDB设为Windows服务
    apply 判定变量类型
    angularjs 手动启动
    Angular js ie 7,8 兼容性
    jQuery 之正则表达式篇
  • 原文地址:https://www.cnblogs.com/microestc/p/14979681.html
Copyright © 2011-2022 走看看