深入C# 内存管理以及优化的方法详解

本篇文章是对C#中内存管理以及优化的方法进行了详细的分析介绍,需要的朋友参考下

在C# winform应用程序中,用以下代码可以进行一些内存使用的优化
复制代码 代码如下:

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
///
/// 包含各种内存管理、优化的方法
///

    public class Memory
    {
        private static readonly Version myVersion = new Version(1, 0);
        ///
        /// 将当前进程的内存占用尺寸设置到最小
        ///

        /// 0为成功,-1为失败
        public static int SetProcessMemoryToMin()
        {
            return SetProcessMemoryToMin(Process.GetCurrentProcess().Handle);
        }
        ///
        /// 将内存占用尺寸设置到最小
        ///

        /// 需要设置内存使用范围的程序进程句柄,一般为当前进程, 如:System.Diagnostics.Process.GetCurrentProcess().Handle
        /// 0为成功,-1为失败
        public static int SetProcessMemoryToMin(IntPtr SetProcess)
        {
            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
            {
                return SetProcessWorkingSetSize(SetProcess, -1, -1);
            }
            return -1;
        }
        [DllImport("kernel32.dll")]
        private static extern int SetProcessWorkingSetSize(IntPtr hProcess, int dwMinimumWorkingSetSize, int dwMaximumWorkingSetSize);
    }

以上就是深入C# 内存管理以及优化的方法详解的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 其他教程