CSharp - Memory leaks problem detection and solution

/* 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.

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.

I hope you find this articles helpful!

时间: 2024-10-05 23:54:51

CSharp - Memory leaks problem detection and solution的相关文章

Identify Memory Leaks in Visual CPP Applications(VLD内存泄漏检测工具)

原文地址:http://www.codeproject.com/Articles/1045847/Identify-Memory-Leaks-in-Visual-CPP-Applications 基于CPOL License Identify Memory Leaks in Visual CPP Applications Visual Leak Detector (VLD) is an easy to use memory leak detection system. The installat

The Introduction of Java Memory Leaks

One of the most significant advantages of Java is its memory management. You simply create objects and Java Garbage Collector takes care of allocating and freeing memory. However, the situation is not as simple as that, because memory leaks frequentl

Avoid memory leaks on Android

Android applications are, at least on the T-Mobile G1, limited to 16 MB of heap. It’s both a lot of memory for a phone and yet very little for what some developers want to achieve. Even if you do not plan on using all of this memory, you should use a

[转]Activitys, Threads, & Memory Leaks

转自:http://www.androiddesignpatterns.com/2013/04/activitys-threads-memory-leaks.html http://www.cnblogs.com/kissazi2/p/4125356.html A common difficulty in Android programming is coordinating long-running tasks over the Activity lifecycle and avoiding

内存泄露 Memory Leaks 内存优化【总结】

什么是内存泄露 内存管理一直是Java 所鼓吹的强大优点.开发者只需要简单地创建对象,而Java的垃圾收集器将会自动管理内存空间的分配和释放. 但在很多情况下,事情并不那么简单,在 Java程序中总是会频繁地发生内存泄露(Memory Leaks). 内存泄漏就是:当某些对象不再被应用程序所使用,但是由于仍然被引用而导致垃圾收集器不能释放他们. 或者说是:我们对某一内存空间使用完成后没有释放. 用白话来说就是:该回收的内存没被回收. 要理解这个定义,我们需要理解内存中的对象状态.下图展示了什么是

解决:Detected memory leaks

最近在一个项目中,程序退出后都出现内存泄漏: Detected memory leaks!Dumping objects ->{171} normal block at 0x05785AD0, 12 bytes long.Data: << N       N x 7 > 3C AC 4E 10 00 00 00 00 BC A4 4E 10 78 B6 37 00Object dump complete. 解决方法: 1.在程序中的尽可能靠近启动代码的地方(足够前的地方,只要在泄漏

xcode里面使用Memory Leaks和Instruments检测内存泄漏

教程截图: Leaks和Instruments教程[检测内存泄露]" src="http://pic002.cnblogs.com/images/2011/283130/2011080816513182.jpg"> 作为一名无证程序员,无论你多么精通Objective-C的内存管理,随着时间的推移,你也不可避免的犯内存相关的错误.但通常因为代码量太大,以至于你不可能一行一行的去排除(等你解决完,你设计的动车早相撞了!) 幸运的是,苹果已经提供了一些好的方式来帮助你找到应

On Memory Leaks in Java and in Android.

from:http://chaosinmotion.com/blog/?p=696 Just because it's a garbage collected language doesn't mean you can't leak memory or run out of it. Especially on Android where you get so little to begin with. Now of course sometimes the answer is that you

[C++] How to prevent memory leaks

How to prevent memory leaks ? overload new/delete