多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > php教程 > CSharp - Memory leaks problem detection and solution

CSharp - Memory leaks problem detection and solution

來源:程序員人生   發布時間:2015-03-24 08:47:36 閱讀次數:3437次

/* By Dylan SUN*/

Out of memory exception happens when server doesn’t have enough memory to run the application.

This often happens when you are dealing with external resources, such as consuming Interop libraries, treating files, streams, etc.

Memory is a limited resource, it’s easy to be run out of. So, you should be careful when you are working with external resources in your application development.

There are some good memory profilers in the market.
I’ve listed two profilers I’ve used. They are both very easy to use. You can even try them for a limited period.

  1. DotTrace
  2. Ants Memory Profiler

Here are some screenshots of one snapshot in Ants memory profiler.

You could see the memory usage progress with time.
progress

You could easily see the memory usage with Heap generation 1, generation 2, Large object heap, Unused memory allocated to .NET, Unmanaged memories.

這里寫圖片描述

Then you could investigate which instance of class is used, how much times it’s been used, how many memory it has occupied, etc.

這里寫圖片描述

The best practice to consume an external data source is using “using” statement. Everything you get in external services will be disposed.

using (var service = _serviceAdapter.Configure()) { var data = _serviceAdapter.RetrieveData(parameters); }

If you need to use the retrieved data in several places in your application. You can convert it to a DTO object, so the code becomes managed.

DataDto dataDto = new DataDto(); using (var service = _serviceAdapter.Configure()) { var data = _serviceAdapter.RetrieveData(parameters); dtoData = ConvertToDto(data); }

In this way you can use dtoData wherever you like.
After the object’s usage, the data will be collected by GC later.

You can translate the previous code into :

Service service = new Service(); Data data = new Data(); try { service = _serviceAdapter.Configure(); data = _serviceAdapter.RetrieveData(parameters); dtoData = ConvertToDto(data); } finally { service.Dispose(); data.Dispose(); }

If you want to ensure that all exceptions will be caught. You can do something like this:

Service service = new Service(); Data data = new Data(); try { service = _serviceAdapter.Configure(); data = _serviceAdapter.RetrieveData(parameters); dtoData = ConvertToDto(data); } catch(Exception ex) { Logger.LogError(ex.Message + ex.StackTrace); } finally { service.Dispose(); data.Dispose(); }

I hope you find this articles helpful!

生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 草β好视频 | 久久精品九九亚洲精品天堂 | 色聚网久久综合 | 国产亚洲欧美在线观看的 | 中文字幕在线视频第一页 | 欧美亚洲综合另类成人 | 91精品免费在线观看 | 在线观看欧美精品 | 99热精品成人免费观看 | 国产精品亚洲一区二区三区 | 一区二区三区 日韩 | 欧美福利网| 久久国产区 | 91色网站 | 亚洲欧美高清视频 | 亚洲天天看 | 九九精品视频一区二区三区 | 黄污网 | 久久成人永久免费播放 | 成人午夜精品网站在线观看 | 最新亚洲人成网站在线影院 | 精品国产精品a | 香港一级a毛片在线播放 | 永久在线播放 | 美国一级大毛片 | 九九九久久久 | 99精品国产一区二区三区 | 亚洲香蕉一区二区三区在线观看 | 男女上下爽无遮挡午夜免费视频 | 国产一区二区不卡视频 | 古代级a毛片可以免费看 | 亚洲视频 欧美视频 | 欧美最猛黑人xxxx黑人猛交3p | 国产一区二区三区四区 | 久久午夜一区二区 | 亚洲永久精品免费www52zcm男男 | 欧美人成片免费看视频不卡 | 欧美hdxxxx| 日本护士xxxx视频 | 精品久久久久久久一区二区手机版 | 亚洲最大视频网站 |