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

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

    博客分类:
  • .net
阅读更多
常见的内存泄露演示

作者使用了一个Forma,每一个按钮都会打开一个子form。按常理,当子form被关闭时,我们都希望它所占有的资源被自动释放。但在这里,作者将在每一个子form中演示一种泄露的情况。



Static references

最明显的,如果一个对象被静态字段(field)引用,它永远都不会被释放。这种情况常见于单键模式,因为它们往往都市静态的,要不就是长时间驻留的。
这些直接引用往往都是显而易见的,但真正危险的都是那些间接引用。因此你需要格外注意引用串。一个有效的方法就是查看这个串的根,如果这个根式静态的,那整个串上的引用都无法被释放。



如上图,如果Object1是静态的或者长驻留的,那么这条引用串上的对象都不被释放。危险的是,串很长时就很难意识到它的根是静态类型的。比如,如果仅仅注意了一级深度,那么当Object2消失的时候,Object3和Object4就应该被释放,当你很可能忽视了Object1的存在。

建议是慎用静态类型,尽可能的不用,否则请格外注意其或其他单键对象的内存驻留时间。

一种具体的风险就是静态事件,将在事件泄露演示中阐述。


事件或"lapsed listener"

子form订阅了main form的事件,以便在main form的通透度(opacity)变化时得到通知。
mainForm.OpacityChanged += mainForm_OpacityChanged;

问题由此产生:OpacityChanged事件创建了一个由main form到子form的引用:

作者在另外一篇文章中讨论了事件和引用的关系,这里只给出了演示图,和jetTrace截图:





分享到:
评论

相关推荐

    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. ...

    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 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 ...

    HOW TO DETECT OPEN PORTS, PROTOCOLS AND SERVICES.docx

    2. **提高补丁管理**:通过跟踪端口与服务的关系,可以更有效地应用安全补丁。 3. **增强审计能力**:准确记录端口状态有助于进行定期的安全审计。 4. **快速响应**:及时发现并解决新出现的开放端口问题,可迅速...

    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...

    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 ...

    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...

    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, ...

    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 ...

    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. ...

    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