How to avoid Memory Leaks? [closed]

2019-03-15 10:52发布

What are some tips I can use to avoid memory leaks in my applications? Are there any gotchas or pitfalls that I can look out for?

18条回答
不美不萌又怎样
2楼-- · 2019-03-15 11:17

Watch that you remove any event handlers that you use. In .NET, they are the most common cause of leaked memory.

查看更多
你好瞎i
3楼-- · 2019-03-15 11:17
狗以群分
4楼-- · 2019-03-15 11:19
Luminary・发光体
5楼-- · 2019-03-15 11:20

Don't underestimate the helpfulness of tools in these situations. The .NET memory profilers are fairly mature and robust nowadays, and if you have a complicated application where an object that you think should be freed is still held as a reference by something else the ability to pinpoint that reference is invaluable.

I've just finished hunting down a memory leak where a WPF tab page hosted a Windows Form control (since these tabs held a lot of data and you could open and close them at will simply waiting until the GC cleared the memory on close was not an option). I used the YourKit profiler to take a snapshot of the memory before the tab was opened, opened a tab, closed it again and took another snapshot. Inside the profiler you could visually compare the two snapshots and see what objects had survived the process and follow their references back to the GC root. I have no experience with other profilers, but I know there are a few out there if YourKit doesn't fulfil your needs.


Edited to add:

Okay, this isn't avoiding memory leaks, it's fixing them. But I'll leave it here since I think it is useful information and I don't think that enough .NET developers know about these tools.

查看更多
6楼-- · 2019-03-15 11:21

Memory leaks are bugs, so in general - the question could be answered the same as "how to code without bugs"? In the long run - it is not possible to have no bugs, but you can limit the chance for having those in the released code.

Start with caring about developed code quality and following the guidelines mentioned by others.

  1. Simplicity is golden - the more simple the code - the less a chance for bugs or leaks
  2. Be careful when using unmanaged resources - Windows Handles, DB connections, GDI objects, etc.
  3. Use using for IDisposables
  4. Implement IDisposable for classes that carry unmanaged resources
  5. Make sure to remove any references to unused objects - including the tricky event handlers

On top of these - implement tests to see if the memory leaks - unit, concurrency, stress and load tests could help here most. See if memory leaks by checking metrics (perf counters). You could also log object creations and destructions and analyze the logs at the end of a test.

查看更多
别忘想泡老子
7楼-- · 2019-03-15 11:23

The most common case of memory not being destroyed by the GC is when you have event handlers that did not get unhooked properly. You can unhook in Dispose() if you want.

I describe the problem more in detail, and I have a way to write tests to determine if you leak the object here.

查看更多
登录 后发表回答