. NET Common Memory leaks

Source: Internet
Author: User

Brief introduction

Previously thought,. NET program memory is managed, and there should be no memory leaks if the unmanaged resources are not called, but the last two days of the Archive memory usage analysis found that things are not as simple as they might seem.

. NET memory leaks, more accurately should be the object over the life cycle and not be recycled by GC. This article lists several scenarios that could lead to memory leaks, provides sample code, and solutions that we hope will help.

The examples given are extremely simplified, but to illustrate the point, the situation in the specific project is much more complex and requires specific analysis.

Sample code not de-registered after event registration

Memory usage when the program is running

Cause analysis

In the sample code, the surface looks like, after a loop, the last new object A will not continue to be referenced, it should be considered garbage, and will be recycled the next time it is garbage collected. However, the experiment proves that object A is not recycled.

The reason is because object B registers the click event without destroying it, causing object B to always refer to a,a and not be considered garbage collected by GC.

Solutions

After the end of the life cycle of object A, the registration of the event is dismissed, so that the memory occupied by a is reclaimed at the next garbage collection.

Memory usage after registering bindings for event cancellation:

Static referencing of sample code

If the CreateObject method is always called, the memory usage of the program is run

Cause analysis

The lifetime of a static variable is global, that is, the program does not exit, and the memory consumed is not released.

Solutions

Minimize the use of static members, and if you must use them, notice whether the objects referenced by static members will always grow.

The sample code is not destroyed after the control is not used

Memory usage when the program is running

Cause analysis

In the sample code, the control LBL has been removed, and the surface looks like the next time the loop is taken, the control should be considered garbage and reclaimed at the next garbage collection. But experiments have shown that memory is not recycled. The reason is that the control was not actually destroyed.

Solutions

After the end of the control's life cycle, call the Dispose method to ensure that the control is truly destroyed.

To use the memory after calling the Dispose method:

Calling an unmanaged resource without releasing the sample code

Cause analysis

Forget to release after using unmanaged objects.

Solutions

Classes that implement the IDisposable interface can be used using the using mode, or the Dispose method is called.

Tool log Output

Tools for the poor, output process memory consumption:)

Task Manager

See if there is a memory leak by observing the Task Manager memory usage.

. NET Memory Profiler

Professional. NET Performance tuning tool, cons, not free:)

Reference
    • Http://madgeek.com/articles/leaks/leaks.en.html
    • Http://stackoverflow.com/questions/620733/memory-leak-in-c-sharp

. NET Common Memory leaks

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.