`
zealotds
  • 浏览: 122376 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

How to detect and avoid memory and resources leaks in .NET applications 摘译1

    博客分类:
  • .net
阅读更多
原文地址: How to detect and avoid memory and resources leaks in .NET applications

关于内存和资源泄露的说明

1. 提供自动内存管理机制的语言如Java C# VB.Net 并不对内存泄露免疫。

2. 三类主要的易泄露系统对象(system object):
  • 用户对象(User objects): 用于提供窗口管理功能,如快捷键列表,Alt快捷键(Carets),光标,钩子,图标,菜单和窗口。
  • GDI对象: 提供图形功能,如位图、画刷、设备上下文(DC)、字体、Memory DC、Metafiles、调色板、Region等
  • 内核对象:提供内存管理、进程执行、IPC等功能,如文件、进程、线程、信号量、timer、access token、socket等

系统对象本身没有问题,但他们都是有限的,就会出现问题。比如GDI对象的默认上限是10000,但这个值是可以查阅并修改的:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows: GDIProcessHandleQuota and USERProcessHandleQuota.

检测和发现泄漏

三个步骤:
  • 检测泄漏
  • 发现泄漏源
  • 确认在哪里和什么时候泄漏会发生

通常我们不会收到到OutOfMemory,因为当物理内存用尽后,系统会使用磁盘虚拟内存。所以一般会收到"out of handles"异常,具体的异常应该是System.ComponentModel.Win32Exception 或者 System.OutOfMemoryException。而且一般也只会在非常“密集”或长时间的使用程序时才会出现这种情况。

检查泄漏,可以用Memory Profiler工具从root object或者根容器着手,比如LayoutView。下一步就是搞清楚为什么那些没有被释放掉的对象上的引用没有被摘除。

原文作者使用了JetBrains dotTrace进行分析,但也可以使用他所谓的忍者方法:)

常见的泄露原因

    * 静态引用(static reference)
    * 没有取消订阅的事件(Event with missing unsubscription)
    * 没有取消订阅的静态事件(Static event with missing unsubscription)
    * 没有触发Dispose方法(Dispose method not invoked)
    * Dispose方法没有完成(Incomplete Dispose method)
    * 不合理使用了绑定数据源的WinForm(Windows Forms: BindingSource misused)
    * CAB: missing Remove call on WorkItem

举例说明,在原作者所做的项目中,使用了一个用于管理toolbar的第三方控件,但即便你的toolbar实现了IDisposable接口,控件管理类也不会去调用。幸运的是,可以自己去调用Dispose方法达到目的。
作者强调,虽然framwork也会有泄漏的情况,但那是非常少的情况,所以请大家不要随便推卸责任:)

0
0
分享到:
评论

相关推荐

    Detected memory leaks简单检测方法

    ### Detected Memory Leaks 简单检测方法 #### 背景介绍 在软件开发过程中,内存泄漏是一个常见的问题,特别是在使用C/C++等需要手动管理内存的语言进行开发时更为突出。内存泄漏不仅会导致程序占用的内存持续增长...

    Kivy - Interactive Applications and Games in Python(PACKT,2ed,2015).

    Kivy – Interactive Applications and Games in Python 2nd Edition, will equip you with all the necessary knowledge to create interactive, responsive, and cross-platform applications and games. ...

    How to Cheat at Securing a Wireless Network

    including Penetration Tester's Open Source Toolkit (ISBN: 1-5974490210), Stealing the Network: How to Own an Identity (ISBN: 1597490067), InfoSec Career Hacking (ISBN: 1597490113), and OS X for ...

    OpenCV 3.x with Python By Example, 2nd Edition-Packt Publishing(2018).pdf

    Chapter 4, Detecting and Tracking Different Body Parts, shows how to detect and track faces in a live video stream. We will discuss the face detection pipeline and see how we can use it to detect and...

    HOW TO DETECT OPEN PORTS, PROTOCOLS AND SERVICES.docx

    1. **减少攻击面**:关闭不必要的端口减少了黑客可能利用的入口点。 2. **提高补丁管理**:通过跟踪端口与服务的关系,可以更有效地应用安全补丁。 3. **增强审计能力**:准确记录端口状态有助于进行定期的安全审计...

    Improving your C# Skills

    You'll begin by learning how to identify the bottlenecks in writing programs, highlight common performance pitfalls, and apply strategies to detect and resolve these issues early. You'll also study ...

    C# Machine Learning Projects pdf

    Lastly, you will learn how to detect anomalies in network and credit card transaction data for cyber attack and credit card fraud detections. By the end of this book, you will be putting your skills...

    go系统编程(英文版)

    you can use the go tool to detect unreachable code and how to avoid some common Go mistakes. Chapter 4, Go Packages, Algorithms, and Data Structures, talks about algorithms and sorting in Go and about...

    opencvsharp-20个例子

    Sample01: How to create a simple 2D image and show it using OpenCV Window. Sample02: How to load and display images. Sample03: How to create a gray-scale image. Sample04: How to apply different ...

    OpenCVSharp 最新4.0 的19个例子

    Sample01: How to create a simple 2D image and show it using OpenCV Window. Sample02: How to load and display images. Sample03: How to create a gray-scale image. Sample04: How to apply different ...

    Programming Embedded Systems in C and C++

    the military uses embedded software to guide smart missiles and detect enemy aircraft; communications satellites, space probes, and modern medicine would be nearly impossible without it. Of course, ...

    Kivy – Interactive Applications and Games in Python Second Edition

    Kivy – Interactive Applications and Games in Python Second Edition, will equip you with all the necessary knowledge to create interactive, responsive, and cross-platform applications and games. ...

    OpenCVSharp-DEMO.zip

    Sample01: How to create a simple 2D image and show it using OpenCV Window. Sample02: How to load and display images. Sample03: How to create a gray-scale image. Sample04: How to apply different ...

    DDoS.Attacks.Evolution.Detection.Prevention.Reaction.and.Tolerance

    of-service (DDoS) attacks, how to detect a DDoS attack when one is mounted, how to prevent such attacks from taking place, and how to react when a DDoS attack is in progress, with the goal of ...

    [.Net 脱壳] .NET Generic Unpacker 1.0.0.1

    This .NET Generic Unpacker was written in a couple of hours and despite of the fact that it's very simple, it might turn useful having it: otherwise you have to unpack manually, which is quite easy ...

    Machine Learning in Java英文原版

    Moving on, you will discover how to detect anomalies and fraud, and ways to perform activity recognition, image recognition, and text analysis. By the end of the book, you will explore related web ...

Global site tag (gtag.js) - Google Analytics