A dynamic approach to detecting, eliminating and fixing memory leaks

  • Bin Yu
  • Cong TianEmail author
  • Nan ZhangEmail author
  • Zhenhua DuanEmail author
  • Hongwei Du


This paper presents a dynamic approach to detecting, eliminating and fixing memory leaks. With our approach, a program to be analyzed is instrumented before its execution. Dynamic symbolic execution is employed so as to expose memory leaks occurring in all execution paths. During the program execution, information about each allocated memory area is updated when corresponding statements are executed. Based on this information, a back-end leak checker records the changes of variables pointing to each memory area, detects memory leaks and deallocates leaked memory areas. After the program execution, a fixed program containing deallocation statements is generated. We have implemented our approach in a tool called DEF_LEAK based on Low Level Virtual Machine compiler infrastructure. Experiments show that DEF_LEAK can detect more leaks than the existing dynamic detection tools for programs with user inputs. Moreover, DEF_LEAK is more effective for helping programmers understand and fix memory leaks than other detection tools.


Memory leaks Leak detection Leak fixing Dynamic symbolic execution Low level virtual machine 



This research is supported by the National Natural Science Foundation of China Grant Nos. 61420106004, 61732013, 61751207 and 61572386.


  1. Andrzejak A, Eichler F, Ghanavati M (2017) Detection of memory leaks in C/C++ code via machine learning. In: IEEE international symposium on software reliability engineering workshops, IEEE, pp 252–258Google Scholar
  2. Bush WR, Pincus JD, Sielaff DJ (2000) A static analyzer for finding dynamic programming errors. Softw-Pract Exp 30(7):775–802CrossRefzbMATHGoogle Scholar
  3. Cadar C, Engler D (2005) Execution generated test cases: how to make systems code crash itself. In: International SPIN workshop on model checking of software, Springer, pp 2–23Google Scholar
  4. Cadar C, Dunbar D, Engler DR, et al (2008a) KLEE: unassisted and automatic generation of high-coverage tests for complex systems programs. In: USENIX symposium on operating systems design and implementation, pp 209–224Google Scholar
  5. Cadar C, Ganesh V, Pawlowski PM, Dill DL, Engler DR (2008b) EXE: automatically generating inputs of death. ACM Trans Inf Syst Secur 12(2):10:1–10:38CrossRefGoogle Scholar
  6. Cherem S, Princehouse L, Rugina R (2007) Practical memory leak detection using guarded value-flow analysis. SIGPLAN Not 42(6):480–491CrossRefGoogle Scholar
  7. Chou AC (2003) Static analysis for bug finding in systems software. Ph.D. thesis, Stanford UniversityGoogle Scholar
  8. Clause J, Orso A (2010) LEAKPOINT: pinpointing the causes of memory leaks. In: ACM/IEEE international conference on software engineering, ACM, pp 515–524Google Scholar
  9. Clause J, Li W, Orso A (2007) Dytan: a generic dynamic taint analysis framework. In: International symposium on software testing and analysis, ACM, pp 196–206Google Scholar
  10. Cui J, Tian C, Zhang N, Duan Z, Du H (2018) Verifying schedulability of tasks in ROS-based systems. J Comb Optim. Google Scholar
  11. Duan Z (2005) Temporal logic and temporal logic programming. Science Press, BeijingGoogle Scholar
  12. Evans D (1996) Static detection of dynamic memory errors. SIGPLAN Not 31(5):44–53CrossRefGoogle Scholar
  13. Fu C, Chau V, Li M, Xue CJ (2018a) Race to idle or not: balancing the memory sleep time with dvs for energy minimization. J Comb Optim 35(3):860–894MathSciNetCrossRefzbMATHGoogle Scholar
  14. Fu S, Cui B, Guo T, Song X (2018b) C++ memory detection tool based on dynamic instrumentation. In: International conference on emerging internetworking, data & web technologies. Springer, pp 663–674Google Scholar
  15. Gao Q, Xiong Y, Mi Y, Zhang L, Yang W, Zhou Z, Xie B, Mei H (2015) Safe memory-leak fixing for C programs. In: ACM/IEEE international conference on software engineering, IEEE, pp 459–470Google Scholar
  16. Hackett B, Rugina R (2005) Region-based shape analysis with tracked locations. SIGPLAN Not 40(1):310–323CrossRefzbMATHGoogle Scholar
  17. Hastings R, Joyce B (1992) Purify: fast detection of memory leaks and access errors. In: The winter 1992 USENIX conference, Citeseer, pp 125–138Google Scholar
  18. Hauswirth M, Chilimbi TM (2004) Low-overhead memory leak detection using adaptive statistical profiling. SIGPLAN Not 39(11):156–164CrossRefGoogle Scholar
  19. Heine DL, Lam MS (2003) A practical flow-sensitive and context-sensitive C and C++ memory leak detector. SIGPLAN Not 38(5):168–181CrossRefGoogle Scholar
  20. Insolvibile G (2003) Advanced memory allocation. Linux J 2003(109):7Google Scholar
  21. Joy MM, Rammig FJ (2017) A hybrid methodology to detect memory leaks in soft real-time embedded systems software. Int J Embed Syst 9(1):61–73CrossRefGoogle Scholar
  22. Jump M, McKinley KS (2007) Cork: dynamic memory leak detection for garbage-collected languages. SIGPLAN Not 42(1):31–38CrossRefGoogle Scholar
  23. Jung Y, Yi K (2008) Practical memory leak detector based on parameterized procedural summaries. In: International symposium on memory management, ACM, pp 131–140Google Scholar
  24. Kim D, Wang W, Li D, Lee JL, Wu W, Tokuta AO (2016) A joint optimization of data ferry trajectories and communication powers of ground sensors for long-term environmental monitoring. J Comb Optim 31(4):1550–1568MathSciNetCrossRefzbMATHGoogle Scholar
  25. Lattner C, Lenharth A, Adve V (2007) Making context-sensitive points-to analysis with heap cloning practical for the real world. SIGPLAN Not 42(6):278–289CrossRefGoogle Scholar
  26. Nethercote N, Seward J (2007) Valgrind: a framework for heavyweight dynamic binary instrumentation. SIGPLAN Not 42(6):89–100CrossRefGoogle Scholar
  27. Nguyen HH, Rinard M (2007) Detecting and eliminating memory leaks using cyclic memory allocation. In: International symposium on memory management, ACM, pp 15–30Google Scholar
  28. Orlovich M, Rugina R (2006) Memory leak analysis by contradiction. In: International static analysis symposium, Springer, pp 405–424Google Scholar
  29. Shingari D, Arunkumar A, Wu C (2015) Characterization and throttling-based mitigation of memory interference for heterogeneous smartphones. In: IEEE international symposium on workload characterization, IEEE, pp 22–33Google Scholar
  30. Sui Y, Ye D, Xue J (2012) Static memory leak detection using full-sparse value-flow analysis. In: International symposium on software testing and analysis, ACM, pp 254–264Google Scholar
  31. Wang M, Tian C, Zhang N, Duan Z, Du H (2018) Verifying a scheduling protocol of safety-critical systems. J Comb Optim. Google Scholar
  32. Xie Y, Aiken A (2005) Context-and path-sensitive memory leak detection. In: European software engineering conference held jointly with ACM SIGSOFT international symposium on foundations of software engineering, ACM, pp 115–125Google Scholar
  33. Yang K, Duan Z, Tian C, Zhang N (2018) A compiler for MSVL and its applications. Theor Comput Sci 749:2–16MathSciNetCrossRefzbMATHGoogle Scholar
  34. Zhang N, Duan Z, Tian C (2016a) Model checking concurrent systems with MSVL. Sci China-Inf Sci 59(11):118,101CrossRefGoogle Scholar
  35. Zhang N, Yang M, Gu B, Duan Z, Tian C (2016b) Verifying safety critical task scheduling systems in PPTL axiom system. J Comb Optim 31(2):577–603MathSciNetCrossRefzbMATHGoogle Scholar
  36. Zhao JL, He L, He B (2016) C++ memory check tool based on dynamic binary instrumentation platform. In: International conference on broadband and wireless computing, communication and applications. Springer, pp 3–11Google Scholar

Copyright information

© Springer Science+Business Media, LLC, part of Springer Nature 2019

Authors and Affiliations

  1. 1.Institute of Computing Theory and Technology and ISN LaboratoryXidian UniversityXi’anChina
  2. 2.Harbin Institute of Technology Shenzhen Graduate SchoolShenzhenChina

Personalised recommendations