代码改变世界

控制台进程优化分析

2009-11-03 17:02  爱车龟速兔  阅读(1893)  评论(0编辑  收藏  举报

      自己做的模块, 内存占用太高了, 一个并非进程占用了20多M, boss和客户都受不了了,勒令优化内存.

      没有想到什么好的优化方法前,先来招笨招, 优化dll引用.

优化思路: 1 能不调用的dll一定要排除

              2 所有进程都需要调用的功能尽可能合并.

 

沿着这个思路开动,  一下午的dug, 跟踪....

先在程序里面植入debug语句,将时间点打印出来, 看看什么代码都干了什么坏事.

未优化前, 单步输入如下:

 “NeConnection.exe”(托管): 已加载“C:\Windows\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll”,已跳过符号加载。已对模块进行了优化并启用了调试器选项“仅我的代码”。
“NeConnection.exe”(托管): 已加载“C:\Windows\assembly\GAC_MSIL\mscorlib.resources\2.0.0.0_zh-CHS_b77a5c561934e089\mscorlib.resources.dll”,已跳过符号加载。已对模块进行了优化并启用了调试器选项“仅我的代码”。
“NeConnection.exe”(托管): 已加载“F:\当前工作\联机指令平台\2.程序代码\VirtualPortService\VirtualPortServiceConsole\bin\Debug\NeConnection.exe”,符号已加载。
“NeConnection.exe”(托管): 已加载“F:\当前工作\联机指令平台\2.程序代码\VirtualPortService\VirtualPortServiceConsole\bin\Debug\Ultra.Nmos.DataEntity.dll”,符号已加载。
“NeConnection.exe”(托管): 已加载“F:\当前工作\联机指令平台\2.程序代码\VirtualPortService\VirtualPortServiceConsole\bin\Debug\Ultra.Nmos.RemotingObjectLib.dll”,符号已加载。
“NeConnection.exe”(托管): 已加载“F:\当前工作\联机指令平台\2.程序代码\VirtualPortService\VirtualPortServiceConsole\bin\Debug\Ultra.Nmos.UserIdentityInterface.dll”,符号已加载。
“NeConnection.exe”(托管): 已加载“F:\当前工作\联机指令平台\2.程序代码\VirtualPortService\VirtualPortServiceConsole\bin\Debug\Ultra.Nmos.NeAdapterBase.dll”,符号已加载。
“NeConnection.exe”(托管): 已加载“C:\Windows\assembly\GAC_MSIL\System\2.0.0.0__b77a5c561934e089\System.dll”,已跳过符号加载。已对模块进行了优化并启用了调试器选项“仅我的代码”。
“NeConnection.exe”(托管): 已加载“F:\当前工作\联机指令平台\2.程序代码\VirtualPortService\VirtualPortServiceConsole\bin\Debug\Ultra.Nmos.Equipment.dll”,符号已加载。
“NeConnection.exe”(托管): 已加载“C:\Windows\assembly\GAC_MSIL\System.Configuration\2.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll”,已跳过符号加载。已对模块进行了优化并启用了调试器选项“仅我的代码”。
“NeConnection.exe”(托管): 已加载“C:\Windows\assembly\GAC_MSIL\System.Xml\2.0.0.0__b77a5c561934e089\System.Xml.dll”,已跳过符号加载。已对模块进行了优化并启用了调试器选项“仅我的代码”。
启动进程:2009/11/3 14:26:51
开始远程调用,获取用户信息:2009/11/3 14:26:51
“NeConnection.exe”(托管): 已加载“C:\Windows\assembly\GAC_MSIL\System.Runtime.Remoting\2.0.0.0__b77a5c561934e089\System.Runtime.Remoting.dll”,已跳过符号加载。已对模块进行了优化并启用了调试器选项“仅我的代码”。
结束远程调用,获取用户信息:2009/11/3 14:26:52
开始远程调用,查看端口连接数限制:2009/11/3 14:26:52
结束远程调用,查看端口连接数限制:2009/11/3 14:26:52
启动超时不活动监控对象:2009/11/3 14:26:52
启动多播消息对象:2009/11/3 14:26:52
“NeConnection.exe”(托管): 已加载“F:\当前工作\联机指令平台\2.程序代码\VirtualPortService\VirtualPortServiceConsole\bin\Debug\Ultra.Nmos.DataAccess.dll”,符号已加载。
“NeConnection.exe”(托管): 已加载“C:\Windows\assembly\GAC_MSIL\System.Core\3.5.0.0__b77a5c561934e089\System.Core.dll”,已跳过符号加载。已对模块进行了优化并启用了调试器选项“仅我的代码”。
“NeConnection.exe”(托管): 已加载“C:\Windows\assembly\GAC_MSIL\System.Data.Linq\3.5.0.0__b77a5c561934e089\System.Data.Linq.dll”,已跳过符号加载。已对模块进行了优化并启用了调试器选项“仅我的代码”。
“NeConnection.exe”(托管): 已加载“C:\Windows\assembly\GAC_32\System.Data\2.0.0.0__b77a5c561934e089\System.Data.dll”,已跳过符号加载。已对模块进行了优化并启用了调试器选项“仅我的代码”。
“NeConnection.exe”(托管): 已加载“C:\Windows\assembly\GAC_32\System.Transactions\2.0.0.0__b77a5c561934e089\System.Transactions.dll”,已跳过符号加载。已对模块进行了优化并启用了调试器选项“仅我的代码”。
“NeConnection.exe”(托管): 已加载“C:\Windows\assembly\GAC_32\System.EnterpriseServices\2.0.0.0__b03f5f7f11d50a3a\System.EnterpriseServices.dll”,已跳过符号加载。已对模块进行了优化并启用了调试器选项“仅我的代码”。
“NeConnection.exe”(托管): 已加载“Anonymously Hosted DynamicMethods Assembly”
“NeConnection.exe”(托管): 已加载“C:\Windows\assembly\GAC_MSIL\System.Xml.Linq\3.5.0.0__b77a5c561934e089\System.Xml.Linq.dll”,已跳过符号加载。已对模块进行了优化并启用了调试器选项“仅我的代码”。
启动心跳:2009/11/3 14:26:52
获取网元所属的网元组的:2009/11/3 14:26:52
“NeConnection.exe”(托管): 已加载“F:\当前工作\联机指令平台\2.程序代码\VirtualPortService\VirtualPortServiceConsole\bin\Debug\Ultra.Nmos.PgmSocket.dll”,符号已加载。
启动网元连接对象:2009/11/3 14:26:52
“NeConnection.exe”(托管): 已加载“F:\当前工作\联机指令平台\2.程序代码\VirtualPortService\VirtualPortServiceConsole\bin\Debug\Dart.PowerTCP.Telnet.dll”
“NeConnection.exe”(托管): 已加载“C:\Windows\assembly\GAC_MSIL\System.Windows.Forms\2.0.0.0__b77a5c561934e089\System.Windows.Forms.dll”,已跳过符号加载。已对模块进行了优化并启用了调试器选项“仅我的代码”。
“NeConnection.exe”(托管): 已加载“C:\Windows\assembly\GAC_MSIL\System.Drawing\2.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll”,已跳过符号加载。已对模块进行了优化并启用了调试器选项“仅我的代码”。
启动结束:2009/11/3 14:26:52
“NeConnection.exe”(托管): 已加载“F:\当前工作\联机指令平台\2.程序代码\VirtualPortService\VirtualPortServiceConsole\bin\Debug\Emcaster.dll”,符号已加载。

 

 

发现不对劲的地方了, 启动多播消息对象 ,这个一个纯Socket操作的模块,怎么引用了一大堆不认识的dll, review代码, 一通改. ok成了下面的样子.

“VirtualPortServiceConsole.vshost.exe”(托管): 已加载“F:\当前工作\联机指令平台\2.程序代码\VirtualPortService\VirtualPortServiceConsole\bin\Debug\Ultra.Nmos.UserIdentityInterface.dll”,符号已加载。
“NeConnection.exe”(托管): 已加载“C:\Windows\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll”,已跳过符号加载。已对模块进行了优化并启用了调试器选项“仅我的代码”。
“NeConnection.exe”(托管): 已加载“C:\Windows\assembly\GAC_MSIL\mscorlib.resources\2.0.0.0_zh-CHS_b77a5c561934e089\mscorlib.resources.dll”,已跳过符号加载。已对模块进行了优化并启用了调试器选项“仅我的代码”。
“NeConnection.exe”(托管): 已加载“F:\当前工作\联机指令平台\2.程序代码\VirtualPortService\VirtualPortServiceConsole\bin\Debug\NeConnection.exe”,符号已加载。
“NeConnection.exe”(托管): 已加载“F:\当前工作\联机指令平台\2.程序代码\VirtualPortService\VirtualPortServiceConsole\bin\Debug\Ultra.Nmos.DataEntity.dll”,符号已加载。
“NeConnection.exe”(托管): 已加载“F:\当前工作\联机指令平台\2.程序代码\VirtualPortService\VirtualPortServiceConsole\bin\Debug\Ultra.Nmos.RemotingObjectLib.dll”,符号已加载。
“NeConnection.exe”(托管): 已加载“F:\当前工作\联机指令平台\2.程序代码\VirtualPortService\VirtualPortServiceConsole\bin\Debug\Ultra.Nmos.UserIdentityInterface.dll”,符号已加载。
“NeConnection.exe”(托管): 已加载“F:\当前工作\联机指令平台\2.程序代码\VirtualPortService\VirtualPortServiceConsole\bin\Debug\Ultra.Nmos.NeAdapterBase.dll”,符号已加载。
“NeConnection.exe”(托管): 已加载“C:\Windows\assembly\GAC_MSIL\System\2.0.0.0__b77a5c561934e089\System.dll”,已跳过符号加载。已对模块进行了优化并启用了调试器选项“仅我的代码”。
“NeConnection.exe”(托管): 已加载“F:\当前工作\联机指令平台\2.程序代码\VirtualPortService\VirtualPortServiceConsole\bin\Debug\Ultra.Nmos.Equipment.dll”,符号已加载。
“NeConnection.exe”(托管): 已加载“C:\Windows\assembly\GAC_MSIL\System.Configuration\2.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll”,已跳过符号加载。已对模块进行了优化并启用了调试器选项“仅我的代码”。
“NeConnection.exe”(托管): 已加载“C:\Windows\assembly\GAC_MSIL\System.Xml\2.0.0.0__b77a5c561934e089\System.Xml.dll”,已跳过符号加载。已对模块进行了优化并启用了调试器选项“仅我的代码”。
启动进程:2009/11/3 15:08:38
开始远程调用,获取用户信息:2009/11/3 15:08:38
“NeConnection.exe”(托管): 已加载“C:\Windows\assembly\GAC_MSIL\System.Runtime.Remoting\2.0.0.0__b77a5c561934e089\System.Runtime.Remoting.dll”,已跳过符号加载。已对模块进行了优化并启用了调试器选项“仅我的代码”。
“VirtualPortServiceConsole.vshost.exe”(托管): 已加载“Ultra.Nmos.NeIdentityImplementForCommon”
“VirtualPortServiceConsole.vshost.exe”(托管): 已加载“F:\当前工作\联机指令平台\2.程序代码\VirtualPortService\VirtualPortServiceConsole\bin\Debug\Ultra.Nmos.NeIdentityInterface.dll”,符号已加载。
“VirtualPortServiceConsole.vshost.exe”(托管): 已加载“Ultra.Nmos.UserIdentityImplementForNmos”
“VirtualPortServiceConsole.vshost.exe”(托管): 已加载“C:\Windows\assembly\GAC_MSIL\System.DirectoryServices\2.0.0.0__b03f5f7f11d50a3a\System.DirectoryServices.dll”,已跳过符号加载。已对模块进行了优化并启用了调试器选项“仅我的代码”。
“VirtualPortServiceConsole.vshost.exe”(托管): 已加载“C:\Windows\assembly\GAC_32\CustomMarshalers\2.0.0.0__b03f5f7f11d50a3a\CustomMarshalers.dll”,已跳过符号加载。已对模块进行了优化并启用了调试器选项“仅我的代码”。
“VirtualPortServiceConsole.vshost.exe”(托管): 已加载“C:\Windows\assembly\GAC_32\System.Web\2.0.0.0__b03f5f7f11d50a3a\System.Web.dll”,已跳过符号加载。已对模块进行了优化并启用了调试器选项“仅我的代码”。
结束远程调用,获取用户信息:2009/11/3 15:08:40
开始远程调用,查看端口连接数限制:2009/11/3 15:08:40
结束远程调用,查看端口连接数限制:2009/11/3 15:08:40
启动超时不活动监控对象:2009/11/3 15:08:40
启动多播消息对象:2009/11/3 15:08:40
启动心跳:2009/11/3 15:08:40
获取网元所属的网元组的:2009/11/3 15:08:40
“NeConnection.exe”(托管): 已加载“F:\当前工作\联机指令平台\2.程序代码\VirtualPortService\VirtualPortServiceConsole\bin\Debug\Ultra.Nmos.DataAccess.dll”,符号已加载。
“NeConnection.exe”(托管): 已加载“F:\当前工作\联机指令平台\2.程序代码\VirtualPortService\VirtualPortServiceConsole\bin\Debug\Ultra.Nmos.PgmSocket.dll”,符号已加载。
“NeConnection.exe”(托管): 已加载“C:\Windows\assembly\GAC_MSIL\System.Core\3.5.0.0__b77a5c561934e089\System.Core.dll”,已跳过符号加载。已对模块进行了优化并启用了调试器选项“仅我的代码”。
“NeConnection.exe”(托管): 已加载“C:\Windows\assembly\GAC_MSIL\System.Data.Linq\3.5.0.0__b77a5c561934e089\System.Data.Linq.dll”,已跳过符号加载。已对模块进行了优化并启用了调试器选项“仅我的代码”。
“NeConnection.exe”(托管): 已加载“C:\Windows\assembly\GAC_32\System.Data\2.0.0.0__b77a5c561934e089\System.Data.dll”,已跳过符号加载。已对模块进行了优化并启用了调试器选项“仅我的代码”。
“NeConnection.exe”(托管): 已加载“C:\Windows\assembly\GAC_32\System.Transactions\2.0.0.0__b77a5c561934e089\System.Transactions.dll”,已跳过符号加载。已对模块进行了优化并启用了调试器选项“仅我的代码”。
“NeConnection.exe”(托管): 已加载“C:\Windows\assembly\GAC_32\System.EnterpriseServices\2.0.0.0__b03f5f7f11d50a3a\System.EnterpriseServices.dll”,已跳过符号加载。已对模块进行了优化并启用了调试器选项“仅我的代码”。
“NeConnection.exe”(托管): 已加载“Anonymously Hosted DynamicMethods Assembly”
启动网元连接对象:2009/11/3 15:08:40
“NeConnection.exe”(托管): 已加载“C:\Windows\assembly\GAC_MSIL\System.Xml.Linq\3.5.0.0__b77a5c561934e089\System.Xml.Linq.dll”,已跳过符号加载。已对模块进行了优化并启用了调试器选项“仅我的代码”。
“NeConnection.exe”(托管): 已加载“F:\当前工作\联机指令平台\2.程序代码\VirtualPortService\VirtualPortServiceConsole\bin\Debug\Dart.PowerTCP.Telnet.dll”
“NeConnection.exe”(托管): 已加载“C:\Windows\assembly\GAC_MSIL\System.Windows.Forms\2.0.0.0__b77a5c561934e089\System.Windows.Forms.dll”,已跳过符号加载。已对模块进行了优化并启用了调试器选项“仅我的代码”。
“NeConnection.exe”(托管): 已加载“C:\Windows\assembly\GAC_MSIL\System.Drawing\2.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll”,已跳过符号加载。已对模块进行了优化并启用了调试器选项“仅我的代码”。
启动结束:2009/11/3 15:08:40
“NeConnection.exe”(托管): 已加载“F:\当前工作\联机指令平台\2.程序代码\VirtualPortService\VirtualPortServiceConsole\bin\Debug\Emcaster.dll”,符号已加载。

 

还算不多, 不过看见那一大堆的linq, 好像也没有用到,必须干掉:

“VirtualPortServiceConsole.vshost.exe”(托管): 已加载“F:\当前工作\联机指令平台\2.程序代码\VirtualPortService\VirtualPortServiceConsole\bin\Debug\Ultra.Nmos.UserIdentityInterface.dll”,符号已加载。
“NeConnection.exe”(托管): 已加载“C:\Windows\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll”,已跳过符号加载。已对模块进行了优化并启用了调试器选项“仅我的代码”。
“NeConnection.exe”(托管): 已加载“C:\Windows\assembly\GAC_MSIL\mscorlib.resources\2.0.0.0_zh-CHS_b77a5c561934e089\mscorlib.resources.dll”,已跳过符号加载。已对模块进行了优化并启用了调试器选项“仅我的代码”。
“NeConnection.exe”(托管): 已加载“F:\当前工作\联机指令平台\2.程序代码\VirtualPortService\VirtualPortServiceConsole\bin\Debug\NeConnection.exe”,符号已加载。
“NeConnection.exe”(托管): 已加载“F:\当前工作\联机指令平台\2.程序代码\VirtualPortService\VirtualPortServiceConsole\bin\Debug\Ultra.Nmos.DataEntity.dll”,符号已加载。
“NeConnection.exe”(托管): 已加载“F:\当前工作\联机指令平台\2.程序代码\VirtualPortService\VirtualPortServiceConsole\bin\Debug\Ultra.Nmos.RemotingObjectLib.dll”,符号已加载。
“NeConnection.exe”(托管): 已加载“F:\当前工作\联机指令平台\2.程序代码\VirtualPortService\VirtualPortServiceConsole\bin\Debug\Ultra.Nmos.UserIdentityInterface.dll”,符号已加载。
“NeConnection.exe”(托管): 已加载“F:\当前工作\联机指令平台\2.程序代码\VirtualPortService\VirtualPortServiceConsole\bin\Debug\Ultra.Nmos.NeAdapterBase.dll”,符号已加载。
“NeConnection.exe”(托管): 已加载“C:\Windows\assembly\GAC_MSIL\System\2.0.0.0__b77a5c561934e089\System.dll”,已跳过符号加载。已对模块进行了优化并启用了调试器选项“仅我的代码”。
“NeConnection.exe”(托管): 已加载“F:\当前工作\联机指令平台\2.程序代码\VirtualPortService\VirtualPortServiceConsole\bin\Debug\Ultra.Nmos.Equipment.dll”,符号已加载。
“NeConnection.exe”(托管): 已加载“F:\当前工作\联机指令平台\2.程序代码\VirtualPortService\VirtualPortServiceConsole\bin\Debug\Ultra.Nmos.PgmSocket.dll”,符号已加载。
“NeConnection.exe”(托管): 已加载“C:\Windows\assembly\GAC_MSIL\System.Configuration\2.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll”,已跳过符号加载。已对模块进行了优化并启用了调试器选项“仅我的代码”。
“NeConnection.exe”(托管): 已加载“C:\Windows\assembly\GAC_MSIL\System.Xml\2.0.0.0__b77a5c561934e089\System.Xml.dll”,已跳过符号加载。已对模块进行了优化并启用了调试器选项“仅我的代码”。
启动进程:2009/11/3 15:42:54
开始远程调用,获取用户信息:2009/11/3 15:42:54
“NeConnection.exe”(托管): 已加载“C:\Windows\assembly\GAC_MSIL\System.Runtime.Remoting\2.0.0.0__b77a5c561934e089\System.Runtime.Remoting.dll”,已跳过符号加载。已对模块进行了优化并启用了调试器选项“仅我的代码”。
“VirtualPortServiceConsole.vshost.exe”(托管): 已加载“Ultra.Nmos.NeIdentityImplementForCommon”
“VirtualPortServiceConsole.vshost.exe”(托管): 已加载“F:\当前工作\联机指令平台\2.程序代码\VirtualPortService\VirtualPortServiceConsole\bin\Debug\Ultra.Nmos.NeIdentityInterface.dll”,符号已加载。
“VirtualPortServiceConsole.vshost.exe”(托管): 已加载“Ultra.Nmos.UserIdentityImplementForNmos”
“VirtualPortServiceConsole.vshost.exe”(托管): 已加载“C:\Windows\assembly\GAC_MSIL\System.DirectoryServices\2.0.0.0__b03f5f7f11d50a3a\System.DirectoryServices.dll”,已跳过符号加载。已对模块进行了优化并启用了调试器选项“仅我的代码”。
“VirtualPortServiceConsole.vshost.exe”(托管): 已加载“C:\Windows\assembly\GAC_32\CustomMarshalers\2.0.0.0__b03f5f7f11d50a3a\CustomMarshalers.dll”,已跳过符号加载。已对模块进行了优化并启用了调试器选项“仅我的代码”。
“VirtualPortServiceConsole.vshost.exe”(托管): 已加载“C:\Windows\assembly\GAC_32\System.Web\2.0.0.0__b03f5f7f11d50a3a\System.Web.dll”,已跳过符号加载。已对模块进行了优化并启用了调试器选项“仅我的代码”。
结束远程调用,获取用户信息:2009/11/3 15:42:56
开始远程调用,查看端口连接数限制:2009/11/3 15:42:56
结束远程调用,查看端口连接数限制:2009/11/3 15:42:56
启动超时不活动监控对象:2009/11/3 15:42:56
启动多播消息对象:2009/11/3 15:42:56
启动心跳:2009/11/3 15:42:56
获取网元所属的网元组的:2009/11/3 15:42:56
启动网元连接对象:2009/11/3 15:42:56
“NeConnection.exe”(托管): 已加载“F:\当前工作\联机指令平台\2.程序代码\VirtualPortService\VirtualPortServiceConsole\bin\Debug\Dart.PowerTCP.Telnet.dll”
“NeConnection.exe”(托管): 已加载“C:\Windows\assembly\GAC_MSIL\System.Windows.Forms\2.0.0.0__b77a5c561934e089\System.Windows.Forms.dll”,已跳过符号加载。已对模块进行了优化并启用了调试器选项“仅我的代码”。
“NeConnection.exe”(托管): 已加载“C:\Windows\assembly\GAC_MSIL\System.Drawing\2.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll”,已跳过符号加载。已对模块进行了优化并启用了调试器选项“仅我的代码”。
启动结束:2009/11/3 15:42:56
“NeConnection.exe”(托管): 已加载“F:\当前工作\联机指令平台\2.程序代码\VirtualPortService\VirtualPortServiceConsole\bin\Debug\Emcaster.dll”,符号已加载。

 

 

到此, 一阶段优化已经达标了.

优化完了, 总结了一下,代码的问题:

1: 开发的时候, 对于内存控制没有概念, 对于dll引入没有给予足够的关注,导致dll乱引用.

2: 在设计模块的时候, 有些地方的没有考虑系统开销, 导致工作效率很低.

    但是这个问题在开发的时候, 有时候 是不可避免的, 第一优先级的是简单实现(毕竟我们是做应用开发的), 但是需要养成好的习惯, 不然写三行代码就1M内存,这个代价是任何一个boss都不会接受的.

3: 质量意识, 这个是关键. 开发时一般的问题不是没有质量意识, 而是质量意识不严格. 尽量提高对代码的要求. 这样在面对客户和boss时我们会更有底气了.