Systematic Solution for Android Native Memory Leak

  • Libunwind is used throughout stack backtrace, which consumes numerous resources. When a large amount of native code exists, frequent calls of Libunwind may render applications unresponsive. The call stack that monitors all the memory operation functions needs to frequently call the related functions of Libunwind.
  • Limitations on read-only memory (ROM) make routine development and testing inconvenient.
  • An environment must be prepared for each troubleshooting operation that must be manually performed through CLI or DDMS. This process does not support comparative analysis and produces final results that are not intuitive.

Stack Backtrace Acceleration

Acceleration Principles

  1. During the compilation process, insert the related code by using the -finstrument-functions compilation option.
  2. Record call addresses in TLS in the form of arrays and cursors to insert, remove, and acquire code as fast as possible.
  • Define the data structures of arrays and cursors
  • Initialize the storage key of thread_stack_t in TLS
  • Initialize thread_stack_t and put it in TLS
  1. Implement __cyg_profile_func_enter and __cyg_profile_func_exit, and record the call address to TLS. The second parameter, call_site, of __cyg_profile_func_enter indicates the code segment address of a call point. The call address is recorded in the allocated array in TLS upon entry to the function, and the cursor ptr->current moves to the left. The cursor moves to the right upon exit from the function. The recording direction is inconsistent with the array growth direction so that the external interface for stack information acquisition can be more concise and efficient. You can directly copy the memory to acquire the call stack that starts with the address of the nearest call point and ends with the address of the farthest call point.
  1. Provide an interface for stack information acquisition.
  2. Compile the preceding logic as a dynamic library as other service modules depend on the dynamic library compilation. Insert -finstrument-functions into the compilation flag for instrumentation so that calls to all functions are recorded in TLS. You can acquire the call stack by calling get_tls_backtrace(void** backtrace, int max) in any place.

Performance Comparison

  • Single-threaded acquisition through Libunwind
  • Single-threaded acquisition through TLS
  • Libunwind with 10 threads
  • TLS method with 10 threads

Advantages and Disadvantages


  • Use the malloc_debug module of LIBC for memory monitoring. It is inconvenient to enable memory monitoring via the official method, as well as it is not conducive to automatic testing. Instead, you can compile code in your project to hook all memory functions and jump to the monitoring function leak_xxx of malloc_debug to execute the code. This approach ensures that malloc_debug monitors all memory applications and release operations and collects statistics accordingly.
  • Use get_tls_backtrace to execute LIBC_HIDDEN int32_t get_backtrace_external(uintptr_t* frames, size_t max_depth) that is used in the malloc_debug module, in combination with stack backtrace acceleration.
  • Establish socket communication to enable external programs for exchanging data through a socket and conveniently acquire memory data.
  • Build a web client to acquire and parse the uploaded memory data for display. The address must be reverse engineered by using addr2line.
  • Write test cases to be executed in combination with automatic testing. When testing begins, use the socket to collect memory information and subsequently store it. When testing ends, upload the memory information to the platform for parsing and send an evaluation email. When an alert is triggered, you can perform troubleshooting on the web client based on the memory curve and call stack information.

Original Source



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website: