IBM Rational PurifyPlus™ IBM Rational PurifyPlus™ Purify’s value in finding memory leaks This module will cover the basics of memory leaks in C / C++ code, and will show how IBM Rational Purify® can provide value in eliminating leaks. Module objectives Module objectives The following topics are covered in this module: What is a leak? Why are leaks problematic? / Effects of leaks? Leak example Demo: Code cleanup with Purify Comparison of native and managed code Upon completion of this module, you will be able to: Understand the importance of avoiding memory leaks Run Purify to detect memory leaks in your code to ensure your applications are reliable How IBM Rational Purify can help you avoid leaks and write better, more reliable code This module will cover what memory leaks are, and why they can be so troublesome. This module will also provide a leak example and a demonstration of code cleanup with Purify. Finally, this module will cover a comparison of native and managed code. Upon completion of this module, you will be able to: Understand the importance of avoiding memory leaks, and run Purify to detect leaks. You should know how you can use IBM Rational Purify to detect memory leaks in your code to ensure your applications are reliable and how IBM Rational Purify can help you avoid leaks and write better, more reliable code. You should be familiar with general programming concepts and C or C++ before continuing, because this module is geared toward individuals with some background in software development. Why are memory leaks so troublesome? 3 Memory leaks occur: Memory is requested but not returned back to the heap Memory is not able to be reused until the entire process is restarted Varying significance The amount of memory leaked Frequency of calling the “leaky” code Why are memory leaks so troublesome? In this section, we’ll take a deeper look at memory leaks, starting with why they are so troublesome. A memory leak occurs when memory is requested at runtime from the heap (typically through malloc or new) but is not returned back to the heap (typically through free or delete) while the associated pointer is still in scope. Once the pointer goes out of scope, this memory is not accessible by your code. Also, the memory is not able to be reused until the entire process is restarted since it is “leaked.” A memory leak can be of varying significance. There are two factors to consider: 1. The amount of memory leaked, and 2. The frequency of calling the “leaky” code If either of these factors is high, the result can be a significant amount of memory becoming unusable at runtime, resulting in sluggish application performance, or worse, unexpected results. All of these things can tarnish your corporate image in the eyes of the consumers of your software. Why are memory leaks so troublesome? 4 Memory leaks can cause your application to: Lock up Lose data Behave unexpectedly Cause severe slowdown Why are memory leaks so troublesome? If either of these varying factors is high, the result can be a significant amount of memory becoming unusable at runtime, resulting in sluggish application performance, lock ups, lost data, or worse, unexpected results. Leak code example 5 #include "stdafx.h" class MyWidget { int mystorage[500]; public: void Process() { std::cout<<"I'm a widget."; } }; int _tmain(int argc, _TCHAR* argv[]) { for (int i=0;i<5000;i++) { MyWidget* x = new MyWidget(); x->Process(); } // We have 5000 leaked objects! If our program weren’t through, we’d have to live with that memory loss until the process terminated! return 0; } Leak code example In this leak code example, you have a very simple C++ program. This program will run a loop 5,000 times, each time allocating an instance of the class MyWidget which includes 500 bytes of storage. Leak code example 6 int _tmain(int argc, _TCHAR* argv[]) { for (int i=0;i<5000;i++) { MyWidget* x = new MyWidget(); x->Process(); } // We have 5000 leaked objects! If our program weren’t through, we’d have to live with that memory loss until the process terminated! return 0; } Leak code example The important part of the code is highlighted here. You will notice that the code instantiates the MyWidget instance each time, but never deletes the object, so the memory is lost immediately after the x variable goes out of scope. This is a memory leak! Leak code Example (continued) 7 A leak is more severe the more frequently the code is called It is crucial to ensure that frequently called code is leak-free Examples: Event handlers, Web service handlers Leak code Example (continued) In some circumstances, code is called very frequently. Imagine that you have a routine that is called every time that a key is pressed on the keyboard to perform some validation. This code has the potential to be called many times. If there is a 1k memory leak that occurs in this code, the total bytes leaked can grow very quickly so it’s crucial to ensure that frequently called code is leak-free. For example, code that handles Web service or web page requests are generally called frequently, making the need to eradicate leaks very important in these areas. Compare to native code 8 .NET, JAVA include “garbage collection” which reclaims lost memory, thus eradicating the possibility of a leak C/C++ do NOT include this facility. It’s entirely the coder’s responsibility to ensure that your code does not leak memory Purify can significantly aid this process Coders coming from a .NET or JAVA background may be more likely to code in a similar “fire and forget” pattern, resulting in leaks. Purify is of great value to these people especially prone to writing leaky code! Compare to native code Programming languages such as Java or C# / VB.NET which are built on the .NET framework incorporate a mechanism called garbage collection. This mechanism essentially eradicates the possibility of a memory leak. As a result, people familiar with these languages who then write C/C++ code may be more prone to writing code with memory leaks as C/C++ does not include this facility. It is entirely the coder’s responsibility to ensure that your code does not leak memory. As a result, Purify’s ability to find memory leaks becomes even more valuable. Summary 9 What is a leak? Why are leaks problematic? / Effects of leaks? Leak example Comparison of native and managed code Demo: Code cleanup with Purify (see demonstration) Summary In summary, this module covered what memory leaks are, and why they can be so troublesome. Finally, this module covered a comparison of native and managed code. By now, you should understand the importance of avoiding memory leaks, and how to run Purify to detect leaks. You should know how you can use IBM Rational Purify to detect memory leaks in your code to ensure your applications are reliable and how IBM Rational Purify can help you avoid leaks and write better, more reliable code. You can learn more about code cleanup in Purify with the Code Cleanup demonstration associated with this module. Feedback Feedback Your feedback is valuable You can help improve the quality of IBM Education Assistant content to better meet your needs by providing feedback. Did you find this module useful? Did it help you solve a problem or answer a question? Do you have suggestions for improvements? Click to send e-mail feedback: mailto:iea@us.ibm.com?subject=Feedback_about_RPP_FindingMemoryLeaks.ppt This module is also available in PDF format at: ../RPP_FindingMemoryLeaks.pdf You can help improve the quality of IBM Education Assistant content by providing feedback. Trademarks