© 2003 Mark Bartosik, www.leakbrowser.com Dealing with Leaks.

Slides:



Advertisements
Similar presentations
Garbage Collection in the Next C++ Standard Hans-J. Boehm, Mike Spertus, Symantec.
Advertisements

Paging: Design Issues. Readings r Silbershatz et al: ,
Dynamic Memory Management
Introduction to Memory Management. 2 General Structure of Run-Time Memory.
Chapter 3: Editing and Debugging SAS Programs. Some useful tips of using Program Editor Add line number: In the Command Box, type num, enter. Save SAS.
CMSC 330: Organization of Programming Languages Memory and Garbage Collection.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
CMSC 330: Organization of Programming Languages Memory and Garbage Collection.
CSC 213 – Large Scale Programming. Today’s Goals  Consider what new does & how Java works  What are traditional means of managing memory?  Why did.
. Smart Pointers. Memory Management u One of the major issues in writing C/C++ code is managing dynamically allocated memory u Biggest question is how.
How to Detect a Memory Leak By Using System Performance Monitor in Windows 2000.
Traps and pitfalls for VC6, ATL, C++ May be obvious ? May be not ? Mark Bartosik.
Hastings Purify: Fast Detection of Memory Leaks and Access Errors.
© Neeraj Suri EU-NSF ICT March 2006 Budapesti Műszaki és Gazdaságtudományi Egyetem Méréstechnika és Információs Rendszerek Tanszék Zoltán Micskei
CSE506: Operating Systems Block Cache. CSE506: Operating Systems Address Space Abstraction Given a file, which physical pages store its data? Each file.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
CPSC 388 – Compiler Design and Construction
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
Memory Management. History Run-time management of dynamic memory is a necessary activity for modern programming languages Lisp of the 1960’s was one of.
C and Data Structures Baojian Hua
. Memory Management. Memory Organization u During run time, variables can be stored in one of three “pools”  Stack  Static heap  Dynamic heap.
Memory Layout C and Data Structures Baojian Hua
1 Thread Pools. 2 What’s A Thread Pool? A programming technique which we will use. A collection of threads that are created once (e.g. when server starts).
Chapter 9 Overview  Reasons to monitor SQL Server  Performance Monitoring and Tuning  Tools for Monitoring SQL Server  Common Monitoring and Tuning.
1 Using Compressed Files and Folders Applications and operating systems read and write to compressed files. NTFS uncompresses the file before making it.
Distributed Deadlocks and Transaction Recovery.
Windows 2000 Memory Management Computing Department, Lancaster University, UK.
Java Security. Topics Intro to the Java Sandbox Language Level Security Run Time Security Evolution of Security Sandbox Models The Security Manager.
Chapter 8 Windows Outline Programming Windows 2000 System structure Processes and threads in Windows 2000 Memory management The Windows 2000 file.
Dynamic Memory Allocation Questions answered in this lecture: When is a stack appropriate? When is a heap? What are best-fit, first-fit, worst-fit, and.
Lecture 11 vector and Free Store Bjarne Stroustrup
Suite zTPFGI Facilities. Suite Focus Three of zTPFGI’s facilities:  zAutomation  zTREX  Logger.
Chapter 3.5 Memory and I/O Systems. 2 Memory Management Memory problems are one of the leading causes of bugs in programs (60-80%) MUCH worse in languages.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Memory management (CTM 2.5) Carlos Varela RPI April 6, 2015.
®® Microsoft Windows 7 for Power Users Tutorial 9 Evaluating System Performance.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
In the name of Allah The Proxy Pattern Elham moazzen.
Storage Management. The stack and the heap Dynamic storage allocation refers to allocating space for variables at run time Most modern languages support.
Suite zTPFGI Facilities. Suite Focus Three of zTPFGI’s facilities:  zAutomation  zTREX  Logger.
1 File Systems: Consistency Issues. 2 File Systems: Consistency Issues File systems maintains many data structures  Free list/bit vector  Directories.
C++ Memory Overview 4 major memory segments Key differences from Java
 Managing the heap  Resource acquisition is initialization (RAII)  Overriding operator new and delete  Class-based memory pools.
Object-Oriented Programming in C++
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 10: Virtual Memory Background Demand Paging Page Replacement Allocation of.
Operating Systems CSE 411 CPU Management Sept Lecture 10 Instructor: Bhuvan Urgaonkar.
CSI 3125, Preliminaries, page 1 SERVLET. CSI 3125, Preliminaries, page 2 SERVLET A servlet is a server-side software program, written in Java code, that.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
POINTERS. // Chapter 3 - Program 1 - POINTERS.CPP #include int main() { int *pt_int; float *pt_float; int pig = 7, dog = 27; float x = , y = 32.14;
ASP-2-1 SERVER AND CLIENT SIDE SCRITPING Colorado Technical University IT420 Tim Peterson.
Threads. Readings r Silberschatz et al : Chapter 4.
1 2 2 Call The Project Dynamic-Memory 4 4 # include "Utilities.hpp" int main(int argc, char * argv[]) { short int *PtrNo; (*PtrNo) = 5; printf ("(*PtrNo)
By Anand George SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
COMP091 – Operating Systems 1 Memory Management. Memory Management Terms Physical address –Actual address as seen by memory unit Logical address –Address.
Beyond Application Profiling to System Aware Analysis Elena Laskavaia, QNX Bill Graham, QNX.
.NET Memory Primer Martin Kulov. "Out of CPU, memory and disk, memory is typically the most important for overall system performance." Mark Russinovich.
Object Lifetime and Pointers
Module 9: Memory and Resource Management
Debugging Memory Issues
Concepts of programming languages
Chapter 11: File System Implementation
Storage.
Memory Allocation CS 217.
Chapter 9: Virtual Memory
Chapter 2: Operating-System Structures
Created By: Asst. Prof. Ashish Shah, J.M.Patel College, Goregoan West
CSE 451 Section 1/27/2000.
Chapter 2: Operating-System Structures
Implementation of page-replacement algorithms and Belady’s anomaly
Dynamic Binary Translators and Instrumenters
Presentation transcript:

© 2003 Mark Bartosik, Dealing with Leaks

© 2003 Mark Bartosik, What is a resource leak ? Repeated allocation of a resource without timely deallocation Probable resources: memory virtual memory, non-paged pool memory address space OS objects / handles files, threads, events, semaphores, sockets, windows, timers, etc. disk space application level / non OS recourses e.g. database records

© 2003 Mark Bartosik, Unreferenced resources int global = 20; void foo() { int b = 22; HANDLE h = CreateEvent(....); int * int_ptr = int(1000); int * array_ptr = new int[1024]; }

© 2003 Mark Bartosik, Unreferenced heap memory int global = 20; void foo() { int b = 22; HANDLE h = CreateEvent(...); int * int_ptr = int(1000); int * array_ptr = new int[1024];  execution is here } int main() { int a = 21; foo(); return EXIT_SUCCESS; }

© 2003 Mark Bartosik, Unreferenced heap memory int global = 20; void foo() { int b = 22; HANDLE h = CreateEvent(...); int * int_ptr = int(1000); int * array_ptr = new int[1024]; } int main() { int a = 21; foo();  execution is here return EXIT_SUCCESS; }

© 2003 Mark Bartosik, Unbounded resource consumption std::map transactions; void do_transaction(P1_t p1, P2_t p2) { transactions[id] = transaction_t(p1,p2); }

© 2003 Mark Bartosik, C++, avoiding the problem encapsulate ownership within objects or smart pointers bstr_t CComPtr shared_ptr auto_ptr scoped_ptr auto_handle std::string, avoid BSTR, char* only use raw pointers to implement tightly encapsulated objects, e.g. when implementing shared_ptr never use raw pointers to transfer ownership never use raw pointers with business logic exception: indication optional objects - use of NULL Use a garbage collector Problem: No language enforcement -- trusts the programmer

© 2003 Mark Bartosik, VB, avoiding the problem Stay within Visual Basic 5/6 environment (avoid C / OS APIs). Release objects (with events) in “unload” methods set some_object = nothing Forms reference objects (with events) and the objects can reference the form. This is a cyclic reference. VB7 is implemented on.NET only and garbage collected.

© 2003 Mark Bartosik, Java,.NET, avoiding the problem Stay within JVM or.NET VM (avoid C / OS APIs). You have no destructors, and finalizers are restrictive. Cyclic references -- no problem they are garbage collected. Don’t get lax, watch out for unbounded resource consumption.

© 2003 Mark Bartosik, Garbage collection - the solution pause the entire program find every object that can be reached from the static storage or thread stacks mark those objects with an “age” delete all older objects, in any order you like

© 2003 Mark Bartosik, Garbage collection - the solution pause the entire program find every object that can be reached from the static storage or thread stacks mark those objects with an “age” delete all older objects, in any order you like

© 2003 Mark Bartosik, Garbage collection - the penalties Pause the entire program! When? For how long? Find EVERY reachable object? Page in every object from disk! Must run in physical memory to be efficient. Working set will be same as used memory! Delete all the garbage, in what order? Any order! So how do I test deterministically? So I cannot use finalizers? These may or may not be acceptable compromises Remember it only solves half the problem!

© 2003 Mark Bartosik, Workarounds Watch dogs Who watches the watch dog? What can the watch dog do? Bark! COM+ Memory and lifetime limits can be configured for COM servers Heap limits Maximum heap size can be configured WRITE GOOD CODE! FIND THE LEAKS!

© 2003 Mark Bartosik, Available tools Bounds checker Purify Heap Agent Great Circle Lint Parasoft C++ debug heap

© 2003 Mark Bartosik, Typical restrictions Crashes itself on large scale programs Crashes large scale programs Only reports unreferenced memory not unbounded allocations Only reports malloc, free etc Does not report OS API leaks like SysAllocString Only supports C++ Requires recompilation Reports many false positives Requires many hours to run Grinds target program to a halt

© 2003 Mark Bartosik, perfmon

© 2003 Mark Bartosik, Identifying the problem Monitor resources perfmon“process private bytes” “process handle count” “process thread count” taskmgrVM Size, Handle count, Thread count, GDI Objects Exercise the program Coverage test -- does it leak? Use case tests -- when does it leak (most)?

© 2003 Mark Bartosik, Quantifying the problem Must stay out of the red Must consistently reach the green, without crashing The restart period might be... 1 hour 1 working day 1 working week 1 year 1000 transactions never! Max resources might be KB 2 GB

© 2003 Mark Bartosik, Needle in a haystack 3 processes 250 component DLLs 3500 source files 1,500,000 lines of source code 100 KB of leaks per transaction exercising 15% of the code great only 225,000 lines of source to check! 10,000 allocations per transaction caused by probably no more than half a dozen lines of code I have hidden a few leaks in the 1,500,000 lines of D3000 code. We are going to find them NOW!

© 2003 Mark Bartosik, Ignore one time initialization Ignore the first few transactions They might cause one-time initialization leaks, but who cares?

© 2003 Mark Bartosik, Cycle and observe everything Choose a small prime number Perform this number of transactions or cycles Track all allocations and deallocations

© 2003 Mark Bartosik, Cycle again observing deallocations strtype a = b; strtype & strtype::operator=(const strtype & rhs) { if (m_len < rhs.m_len) { char * temp = new char[rhs.m_len]; delete m_p; m_p = temp; } m_len = rhs.m_len; memmove(m_p, rhs.m_p, m_len); return *this; } Allocations made in the last cycle, may not be deleted until the next Perform additional cycles, only tracking deallocations

© 2003 Mark Bartosik, Leak Browser learns Now generate a report of the leak Leak Browser has just “learnt”, what you are interesting in Next time Leak Browser will examine these few areas of code in greater detail (requiring more CPU) Clear the leaks Repeat 7 monitoring cycles Repeat some additional cycles Regenerate the report

© 2003 Mark Bartosik, Not everything is a leak Background tasks may provide “background noise” for example, incoming news or price updates Caches may delay some deallocations Queues and logs may include records of the last 1000 transactions

© 2003 Mark Bartosik, Capabilities Resource profiling - finding the bloat Fast over 10,000 allocations per second can be tracked Tracks about 300 key APIs, not just malloc & free Finds both unreferenced leaks and unbounded leaks Does not require code recompilation Is not intrusive with large applications Remote installation and debugging It works!

© 2003 Mark Bartosik, How does it work? Traps key APIs API is redirected into Leak Browser stub Stack trace is recorded with allocation details In-memory database is maintained If no PDB further instrumentation can be done

© 2003 Mark Bartosik, Challenges Must be thread safe but... –Cannot hold a thread lock and call the OS –Cannot allocate memory –Restrictions on using thread local storage (it may not be allocated yet, or it may have been deallocated) Has only micro-seconds to trace the stack –Standard Microsoft algorithm takes seconds –Cannot afford to call the OS Must cope with optimized code –for stack tracing, and instrumentation –must modify hundreds or thousands of functions without side effects

© 2003 Mark Bartosik, Limitations Need to be able to cycle the application No true garbage detection (yet).NET interop only (so far) No JVM support - none planned Slow display -- too much HTML Occasional crashes, but more reliable than the rest

© 2003 Mark Bartosik, Summary Use garbage collection where appropriate Use best practice for the programming language of choice Monitor resource usage during development Are you within your customer acceptable restart period? If not –Leak Browser