炮哥:"嘿,哥们,忙啥呢,电脑卡成这逼样。"

勇哥:"在用CLR Profile工具分析下FlexiPrint的内存占用情况。"

炮哥:“哎哟,不错啊,玩高级的了。”

勇哥:“也没有啊,就是发现点击查询按钮查询数据时,如果数据量一大的话,内存上上升了好几个M,所以第一感觉就不太正常。正好以前也了解过CLR Profile,但一直没怎么具休的用过,这次正好拿来研究研究。”

炮哥:“Nice job,要向你学习,能够主动发现问题并研究解决方法。对了,有什么发现么?”

勇哥:“通过工具发现dgMain_CellFormatting方法占用内存过多。”

炮哥:“这个方法干嘛用的,要这么多的内存?”

勇哥:“其实做的事情很简单,根据每行订单的不同状态,显示不同的图标,以便让客户能够很直白的了解订单的状态。”

炮哥:“噢,是这样,那确实没什么,让我看下代码。”

namespace IPP_PCL.HomeViewUserControl
{
public partial class PrintOrderInformationUserControl : UserControl, IHomePrintOrderInformationView
{
#region Field private readonly PrintCellLiteServiceClient _serviceClient = new PrintCellLiteServiceClient(); #endregion #region Event Handler /// <summary>
/// 此事件主要用于图片按钮的显示
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dgMain_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (dgMain.Columns[e.ColumnIndex].Name.Equals("dgMain_PrintIcon"))
{
int row = e.RowIndex;
//获取展示图片按钮的单元格
string status = dgMain.Rows[row].Cells["dgMain_statusKey"].Value.ToString(); switch (status)
{
case "Print in Progress":
string erpSoNumber = this.dgMain.Rows[row].Cells["dgMain_ErpSoNumber"].Value.ToString(); //从DB中查找erp_so_number的记录
IEnumerable<FileIndexModel> fileIndexes = _serviceClient.GetFileIndex(erpSoNumber); //如果在FileIndex表中存在pro的记录
if (fileIndexes == null || !fileIndexes.Any()) { break; } FileIndexModel fileIndex = fileIndexes.First(); if (FileStatus.PRINTED != fileIndex.PrintedStatus && FileStatus.PARTIAL_PRINTED != fileIndex.PrintedStatus)
{
e.Value = Properties.Resources.picYellow;
}
else
{
e.Value = Properties.Resources.picGreen;
} break; case "Printed": e.Value = Properties.Resources.picGreen;
break; case "Partial Printed": e.Value = Properties.Resources.picGreen;
break; default: e.Value = Properties.Resources.picYellow;
break;
}
}
} #endregion }
}

勇哥:“......”

炮哥:“很简单,写得很明白,这方法没什么问题吧,会不会搞错了。”

勇哥:“应该不会,通过CLR Profile分析发现,在这个方法中创建了非常多的对象。”

炮哥:“你是说BitMap对象?”

勇哥:“是的。”

炮哥:“噢,明白了,每次访问Properties.Resources.picGreen类似的属性时,都会创建一个新的对象。但其实图标就那么几类,完全可以先保存在内存中,要用时直接引用就行了。”

勇哥:“说得对,让我修改下代码 ,看看情况。”

炮哥:“让我看下你怎么修改的?”

勇哥:“......”

 namespace IPP_PCL.HomeViewUserControl
{
public partial class PrintOrderInformationUserControl : UserControl, IHomePrintOrderInformationView
{
#region Field private readonly PrintCellLiteServiceClient _serviceClient = new PrintCellLiteServiceClient(); Bitmap picGreenBitMap = Properties.Resources.picGreen; Bitmap picYellowBitMap = Properties.Resources.picYellow; #endregion #region Ctor public PrintOrderInformationUserControl()
{
InitializeComponent();
} #endregion #region Event Handler /// <summary>
/// 此事件主要用于图片按钮的显示
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dgMain_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (dgMain.Columns[e.ColumnIndex].Name.Equals("dgMain_PrintIcon"))
{
int row = e.RowIndex;
//获取展示图片按钮的单元格
string status = dgMain.Rows[row].Cells["dgMain_statusKey"].Value.ToString(); switch (status)
{
case "Print in Progress": var proDt = dgMain.DataSource as DataTable; if (proDt == null || proDt.Rows.Count == ) { break; } string erpSoNumber = this.dgMain.Rows[row].Cells["dgMain_ErpSoNumber"].Value.ToString(); //从DB中查找erp_so_number的记录
IEnumerable<FileIndexModel> fileIndexes = _serviceClient.GetFileIndex(erpSoNumber); //如果在FileIndex表中存在pro的记录
if (fileIndexes == null || !fileIndexes.Any()) { break; } FileIndexModel fileIndex = fileIndexes.First(); if (FileStatus.PRINTED != fileIndex.PrintedStatus && FileStatus.PARTIAL_PRINTED != fileIndex.PrintedStatus)
{
e.Value = picYellowBitMap;
}
else
{
e.Value = picGreenBitMap;
} break; case "Printed": e.Value = picGreenBitMap;
break; case "Partial Printed": e.Value = picGreenBitMap;
break; default: e.Value = picYellowBitMap;
break;
}
}
} #endregion }
}

炮哥:“再用CLR Profile分析下修改后的内存情况。”

勇哥:“你看,内存直接从9.0M变为425kB。”

炮哥:“哈哈,看来问题已经解决了。”

勇哥:“今天我发现C#也有闭包的概念。”

炮哥:“闭包?什么玩意?”

勇哥:“我是在学习JS的时候发现有闭包这个概念,但是没想到C#也有这个。”

炮哥:“所以说当你视野放开时,你会发现更多的美好。”

勇哥:“不扯了,该下班了。噢,对了,昨晚看了一部电影<美丽人生>,有兴趣的话可以看下,很不错。”

第一次通过CLR Profile解决内存占用过高的问题的更多相关文章

  1. CLR Profile解决内存占用过高

    CLR Profile解决内存占用过高的问题 炮哥:"嘿,哥们,忙啥呢,电脑卡成这逼样." 勇哥:"在用CLR Profile工具分析下FlexiPrint的内存占用情况 ...

  2. PHPExcel解决内存占用过大问题-dw 查找memoryCacheSize把1M改为2048M

    http://blog.sina.com.cn/s/blog_4ec7952d0101fcrd.html PHPExcel解决内存占用过大问题-设置单元格对象缓存 PHPExcel是一个很强大的处理E ...

  3. Spring cloud开发内存占用过高解决方法

    https://blog.csdn.net/wanhuiguizong/article/details/79289986 版权声明:本文为博主原创文章,转载请声明文章来源和原文链接. https:// ...

  4. PHPExcel解决内存占用过大问题-设置单元格对象缓存

    PHPExcel解决内存占用过大问题-设置单元格对象缓存 PHPExcel是一个很强大的处理Excel的PHP开源类,但是很大的一个问题就是它占用内存太大,从1.7.3开始,它支持设置cell的缓存方 ...

  5. [转帖]Linux中buff/cache内存占用过高解决办法

    Linux中buff/cache内存占用过高解决办法 https://www.cnblogs.com/rocky-AGE-24/p/7629500.html /proc/sys/vm/drop_cac ...

  6. 通过修改my.ini配置文件来解决MySQL 5.6 内存占用过高的问题

    打开后台进程发现mysql占用的内存达到400+M. 修改一下my.ini这个配置文件的配置选项是可以限制MySQL5.6内存占用过高这一问题的,具体修改选项如下: performance_schem ...

  7. [2017-08-09]一则使用WinDbg工具调试iis进程调查内存占用过高的案例

    最近遇到一个奇葩内存问题,跟了三四天,把Windbg玩熟了,所以打算分享下. 症状简介 我们团队的DEV开发环境只有一台4核16G的win2012r2. 这台服务器上装了SqlServer.TFS(项 ...

  8. 【转】一则使用WinDbg工具调试iis进程调查内存占用过高的案例

    最近遇到一个奇葩内存问题,跟了三四天,把Windbg玩熟了,所以打算分享下. 症状简介 我们团队的DEV开发环境只有一台4核16G的win2012r2.这台服务器上装了SqlServer.TFS(项目 ...

  9. 一个神奇的bug:OOM?优雅终止线程?系统内存占用较高?

    摘要:该项目是DAYU平台的数据开发(DLF),数据开发中一个重要的功能就是ETL(数据清洗).ETL由源端到目的端,中间的业务逻辑一般由用户自己编写的SQL模板实现,velocity是其中涉及的一种 ...

随机推荐

  1. 中国大学MOOC-C程序设计(浙大翁恺)—— 素数和

    题目内容: 我们认为2是第一个素数,3是第二个素数,5是第三个素数,依次类推. 现在,给定两个整数n和m,0<n<=m<=200,你的程序要计算第n个素数到第m个素数之间所有的素数的 ...

  2. PAT-A Java实现

    1001 A+B Format (20) 输入:两个数a,b,-1000000 <= a, b <= 1000000 输出:a+b,并以每3个用逗号隔开的形式展示. 思路一: 1)计算出a ...

  3. 破解Wifi

    牛刀小试:Wifi破解的原理. 准备工具:   1:Kali Linux系统 2:一块好用的无线网卡 (推荐免驱版,网上也有推荐,可以去百度上google一下) 3:WPA字典(用来爆破抓获的握手包) ...

  4. mybatis中的resultMap实际作用

    resultMap和resultType在实际的使用上完全可以进行替换,但是resultMap有比resultType更多的一个功能.我们先定义一个简单的resultMap例子 <resultM ...

  5. Solr与Lucene的区别

    Lucene是一个优秀的开源搜索库,Solr是在Lucene上封装的完善的搜索引擎.通俗地说,如果Solr是汽车,那么Lucene就是发动机,没有发动机,汽车就没法运转,但对于用户来说只可开车,不能开 ...

  6. React Router 4.0 实现路由守卫

    在使用 Vue 或者 Angular 的时候,框架提供了路由守卫功能,用来在进入某个路有前进行一些校验工作,如果校验失败,就跳转到 404 或者登陆页面,比如 Vue 中的 beforeEnter 函 ...

  7. LeetCode: 53. Maximum Subarray(Easy)

    1. 原题链接 https://leetcode.com/problems/maximum-subarray/discuss/ 2. 题目要求 给定一个整型数组,返回其子串之和的最大值 例如,[-2, ...

  8. CakePHP 查询总结

    返回 $this->Post->buildQuery(); 返回: Array ( [conditions] => [fields] => [joins] => Arra ...

  9. Use GitHub Desktop to get GitHub projects

    Find the project's https git file in the home page of the project. e.g. https://github.com/PrismLibr ...

  10. java 创建具有参数化类型的数组

    1. List<String>[] ls; Object[] objects = ls; objects[1] = new ArrayList<Integer>(); 先把数组 ...