常见的内存泄露演示
作者使用了一个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内存泄漏的简单检测方法,通过调用系统函数,在debug模式下运行,可以很快定位出内存泄漏之处;注:此方法并不能适用所有Detected memory leaks情况
HOW TO DETECT OPEN PORTS, PROTOCOLS AND SERVICES
By reading this book, you'll learn what problems can occur when web applications are deployed at scale and know how to avoid or mitigate these issues. You'll gain experience of how to write high-...
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. ...
App idle in .net, to detect no action within the app area in configurable time period
Chapter 2, Lab Setup, teaches how to build a safe environment to test bad code and introduces readers to the graphical interfaces of debuggers. Chapter 3, Assembly Language in Linux, explains how to ...
This research papers shows how to detect and track an object
Multilevel Framework to Detect and Handle Vehicle Occlusion
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 ...
国外研究成果,主要是描述RFID相关的攻击技术等。对于理论研究很有帮助。
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 ...
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...
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...
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 ...
Using Contours to Detect and Localize Junctions in Natural Images
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 ...
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...
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, ...
Using Modernizr to detect HTML5 and CSS3 browser support