简体   繁体   中英

How do I find memory leak of my application?

I have written a windows service using .NET technologies. I am using `JetBrains dotMemory' tool to understand the memory leak.

I am getting below report but as a new bee I am not sure how to read this.

在此处输入图片说明

System namespace is showing more survived bytes. But how do I know which code is the root cause of memory leak?

You should look at the survived bytes / retained bytes which will point you to the base instance or the root object of the creation. It depends on your application design and implementation to decide whether the specified object in the memory should be retained or not.

If you identify the root object of the creation, you should try to separate the linkage and make the .net garbage collector to automatically collect the unwanted objects.

There is no fixed flag points to identify memory leaks.

At the first your should decide which kind of memory issue you are going to find

  1. Constantly growing memory consumption - get base snaphsot, get another after memory consumption is increased, open snapshots comparison, open new objects created after first snapshot, look at them to understand which should be collected.

  2. Ensure that some key object doesn't leak - set your app in a state when some object should not be presented in memory (eg close some view), get snapshot, using filter on "Group by type" view to ensure that this object is not presented in memory.

  3. Memory traffic - get base snapshot if needed, run action/algorithm in your app which you want to check, get snapshot. Open "Memory Traffic" view, look if it looks as you implemented or more objects then you expected were allocated during the action.

Grab this free book for other possible memory issues.

PS Only you as an app author can answer the question, is it a problem or it is as designed.

One source of memory leaks are the event handlers that are not being de-referenced.

Example: myClass.DoSomething += Event_DoSomething

You need to make sure the resources are being clearead like below:

myClass.DoSomething -= Event_DoSomething

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM