Silverlight Memory leakage (4) solving Memory leakage

Source: Internet
Author: User
Document directory
  • It is easy to locate the objects that leak memory, but it is not easy to find out the cause of the leakage, because there are more than one path from GC to the target object (Memory leakage object, it is very annoying to find out which path causes memory leakage. In most cases, you can only rely on your own experience.
It is easy to locate the objects that leak memory, but it is not easy to find out the cause of the leakage, because there are more than one path from GC to the target object (Memory leakage object, it is very annoying to find out which path causes memory leakage. In most cases, you can only rely on your own experience.

Generally, we do not pay attention to the memory issue. When the memory becomes a problem, the program may be developed completely. At this time, the object graph will introduce a third-party library ,.. net Framework, other libraries of the company, need to sort out all the paths from GC to the target object, and test each path to confirm which causes memory leakage. When testing a path, you need to exclude the impact of other paths on Memory leakage. At this time, it is very difficult to exclude the impact of other paths, and many code changes will be involved.

An example is provided to illustrate how to solve Memory leakage. After forced GC, two snapshots are compared and no recycled objects are found. For example, BookPageSearche is not the current page, the memory problem is really a crazy problem ):

This figure contains memory leaks caused by Command, Event, Style, and MEF, including almost all memory leaks I have found. This article discusses general solutions for memory leaks, we will not discuss each issue.

See the marked points:

For the target, if you cut off 1, the target object will be released, but the direct parent object of the target object belongs to the. net control, and the path cannot be cut off at this point.

For Mark 2, cut it will cut off 3, 4, 5, Mark 2 NavigationCommand is your own code can be released here, but should not be released here.

The circled Part 3 and 5 is caused by other View memory problems and has little to do with this target object. After the memory of other views (About and SIteNav) is released, this path is automatically disconnected.

Mark 9 is a memory leak caused by a custom navigation framework.

Mark 8 is a memory leak caused by MEF and is the only path directly related to the target object (BookPageSearche). In this target, release the memory from this point.

The following figure compares with the previous one. It shows that BookPageSearche is the object graph of the current page, and GC is not executed.

Misunderstandings to be avoided

The first intuition must be the direct parent object of the target object. As long as the target object is released, there is no need to worry about many references on the upper layer. If so, it may be a headache, if you do not find a point that is the most suitable to cut off the path, it will cause a disaster to the program modification, and there will be countless objects to be released in the future. Therefore, to release the memory, you must avoid releasing the memory directly without analyzing it clearly.

Overall Solution

I don't know how many people can understand it. The purpose of this article is to provide a common analysis method to find out which paths should be selected from GC to the target object to release the memory, after selecting a proper path, the object in the path will be disconnected. Most of the analysis is based on experience.

Tips

Let's look at several similar object graphs, such as memory leakage of the View, and several more views, such as About and Home, to find the commonalities.

For an object graph, only the memory leakage directly related to this object is solved, and the memory problem caused by other objects is ignored.

Starting from the simplest leakage problem, we can solve a leakage problem. We will find that the object graph will be much smaller, and it will be easier to analyze other leakage problems.

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.