paip.提升性能---C#.NET程序内存占用过多以及OutOfMemory解决方法
来源:互联网 发布:直男癌 知乎 编辑:程序博客网 时间:2024/05/01 01:46
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
作者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
- paip.提升性能---C#.NET程序内存占用过多以及OutOfMemory解决方法
- paip.mysql 5.6 数据库 占用 内存 过多 的 解决方案
- phpQuery占用过多内存的解决方法
- phpQuery占用过多内存的解决方法
- phpQuery占用过多内存的解决方法
- paip.提升性能---mysql 性能 测试以及 参数调整.txt
- firefox4占用内存过多
- paip.cache 缓存架构以及性能提升总结
- paip.提升性能---string split
- .NET 程序内存占用问题
- NET 程序内存占用问题
- paip.提升性能---mysql 优化cpu多核以及lan性能的关系.
- paip.提升性能----数据库连接池以及线程池以及对象池
- paip.提升性能---首页性能加快解决方案
- paip.提升性能---.net listbox 使用BeginUpdate与EndUpdate
- paip.提升性能--多核cpu中的java/.net/php/c++编程
- paip.提升性能--多核编程中的java .net php c++最佳实践 v2.0 cah
- c语言内存占用区域,以及详解
- 黑马程序员_毕向东_JavaScript视频教程(1)
- 自己手绑个角色 结果边绑边写,写了一堆MEL。可以提下速度
- EXTJS panel 比较节省html的样式设置
- 谨慎程序员创业的思维障碍
- 微生物增值
- paip.提升性能---C#.NET程序内存占用过多以及OutOfMemory解决方法
- 【转载】int*p[ ]与int(*p)[ ]的不同
- Linux shell编程入门
- Linux 系统内核空间与用户空间通信的实现与分析
- hdu 4313
- Hibernate常见异常
- 软件设计需求分析规格说明---需求规格说明书(ISO标准版)
- C++ overload(重载) overrriding(重写 or 覆盖) redefine(重定义)浅析
- VS2010 使用TeeChart绘图控件 - 之一 - 控件和类的导入