zoukankan      html  css  js  c++  java
  • paip.提升性能C#.NET程序内存占用过多以及OutOfMemory解决方法

    paip.提升性能---C#.NET程序内存占用过多以及OutOfMemory解决方法


    作者Attilax ,  EMAIL:1466519819@qq.com 




    需要的效果是,监测内存占用,当物理内存占用超过99M时,调用SetProcessWorkingSetSize方法回收内存。。
    在WIN7任务栏,工作集(工作设置内存)指的是程序使用的整个内存(缓存+物理内存),而内存 - 专用工作集,它是工作集的子集,它专门描述了某进程所占用的物理内存大小。






    引用 MOLEI.DLL
    代码:
       private void Form1_Load(object sender, EventArgs e)
            {
       new memory().start();
    }




    --------------------框架内全部源码:------------------------
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading;
    using System.Diagnostics;
    using System.Runtime.InteropServices;


    namespace m
    {
        class memory
        {
            internal void start()
            {
              
                   //c452308 add txt2list
              Thread t = new Thread(new ParameterizedThreadStart(
              delegate(object obj)
              {
                 
                  while (true)
                  {


                      Thread.Sleep(3000);
                      float mem=getMem();
                      if(mem>99)
                      {
                          ClearMemory();
                          Console.WriteLine("--timex clr mm clear finish " + filex.getFilename());
                      }
                      Console.WriteLine("--timex clr mm "+filex.getFilename());
                      


                  }




              }));
              t.Name = " --start timex thread 4 clear memory";
              t.IsBackground = true;
              t.Priority = ThreadPriority.Lowest;
              t.Start(null);
            }
     


            private float getMem()
            {
                string procName = Process.GetCurrentProcess().ProcessName;
                using (PerformanceCounter pc = new PerformanceCounter("Process", "Working Set - Private", procName))
                      {
                            float r= (pc.NextValue() / 1024/1024);
                            return r;
                }
            }


            #region 内存回收
            [DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize")]
            public static extern int SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize);
            /// <summary>
            /// 释放内存
            /// </summary>
            public static void ClearMemory()
            {
                     GC.Collect();
                //     GC.WaitForPendingFinalizers();
                if (Environment.OSVersion.Platform == PlatformID.Win32NT)
                {
                    SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
                }
            }
            #endregion
        }
    }






    检测程序使用最大内存的情况
    -------------------------------




    运行CLRProfiler,点击Start Application选择CLRProfilerTestDemo.exe,将会运行此程序,运行一段时间后,点击Kill Application,CLRProfiler将会保存日志显示分析结果







    需要查看的参数:FINALLY HEAP  BYTES。指明程序实际最终使用的内存大小。。很可能总共使用过700,但是GC了680M,实际只有20M。。打开此直言图。。选中了大的块》》右键


    》show who alocate.。。可以打开一个方法调用过程,其中的对象占用大小。。




    垃圾收集统计信息:GC统计信息。总共进行了4501次0代垃圾回收! 






    其它性能调试工具:
    ---------------------
    使用性能测试工具dotTrace 3.0,它能够计算出你程序中那些代码占用内存较多
    ** Profiler API 概述
    当各种事件发生时,CLR 将调用这个回调接口的方法(参见图 2)。 


      下面是需要注意的主要分析器回调方法:RuntimeSuspendStarted、RuntimeSuspendFinished、RuntimeResumeStarted、ObjectAllocated、ObjectsAllocatedByClass、


    MovedReferences、RootReferences 和 ObjectReferences。
      
      如果不熟悉 Profiler API,可以阅读 Profiler.doc(位于 Visual Studio .NET 安装目录下面的 \FrameworkSDK\Tool Developers Guide\docs 文件夹中),来了解某些更


    深入的信息。






    总结:
    -------------------
    调用  GC.Collect();实际上不起任何作用,pass
    ..资料上说SetProcessWorkingSetSize只是把物理内存移至了硬盘缓存上。实际上,调用此方法,不管理缓存还有,物理内存都大大减少了..




    参考:
    ------------
    C#?Winform应用程序占用内存较大解决方法整理_原建顺_新浪博客.htm
    .NET程序内存分析工具CLRProfiler的使用(性能测试)%20-%20一路看.htm
    使用.NET Profiler API检查并优化程序的内存使用_雨枫技术教程网.htm
    【NET程序性能分析 - 下篇】使用CLR探查分析。NET程序 - DebugLZQ - 博客园.htm
    ANTS Memory Profiler 7.4 C#程序内存分析工具 非破解 免费14天 - 下载频道 - CSDN.NET
    使用CLR Profiler分析.NET程序内存不断上升的图文教程
    使用 SetProcessWorkingSetSize 降低程序内存_无牙子的流水账簿_百度空间.htm
    .NET(C#):获取进程的内存私有工作集 - _Mgen - 博客园.htm
    windows 7 任务管理器 内存相关项目的解释_2012-7-16_NetNote_网记.htm
    Performance Counter的使用 - jiangxinyu的专栏 - 博客频道 - CSDN.NET.htm
  • 相关阅读:
    继承
    redis面试题收集
    spring知识点
    jvm类加载
    jvm回收机制
    HashMap和ConcurrentHashMap
    java基础
    spring-quartz整合
    alibaba-sentinel-1.8变化
    springcloud执行流程理解图
  • 原文地址:https://www.cnblogs.com/attilax/p/15199664.html
Copyright © 2011-2022 走看看