SlideShare a Scribd company logo
How to Perform Memory Leak Test Leveraging Valgrind
Presented by: Muhammed Nisar PK, Senior Software Engineer-Testing
How to Perform Memory Leak Test Leveraging Valgrind
© RapidValue Solutions Confidential 2
How to Perform Memory Leak Test Leveraging Valgrind
1. Install Valgrind:
$sudo apt install valgrind # Ubuntu, Debian, etc.
2. Install gcc:
* gcc stands for GNU Compiler Collections. This is used to compile mainly C and C++ language.
$sudo apt install gcc
3. Let us consider a C program test.c with memory leak
The code represents that the memory
allocated for pointer ‘ptr’ (a size of ‘int’ which
is 4 bytes) which are not freed.
So probably this should be detected as
memory leak.
4. Give file permission for the sample program file:
$chmod 777 test.c
5. Compile the program:
$gcc -Wall -g test.c -o test
6. Run the program along with valgrind:
$valgrind --leak-check=yes ./test
or
$valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --verbose --log-file=valgrind-out.txt
./test (This will create one file with valgrind logs)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(void) {
int * ptr = (int * ) malloc(sizeof(int));
/* Do some work */
return 0; /* Return without freeing ptr*/
}
How to Perform Memory Leak Test Leveraging Valgrind
© RapidValue Solutions Confidential 3
nisar@RVSKCH33DT:~$ valgrind --leak-check=yes ./test
==2013== Memcheck, a memory error detector
==2013== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==2013== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==2013== Command: ./test
==2013==
==2013==
==2013== HEAP SUMMARY:
==2013== in use at exit: 4 bytes in 1 blocks
==2013== total heap usage: 1 allocs, 0 frees, 4 bytes allocated
==2013==
==2013== 4 bytes in 1 blocks are definitely lost in loss record 1 of 1
==2013== at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-
linux.so)
==2013== by 0x10865B: main (test.c:7)
==2013==
==2013== LEAK SUMMARY:
==2013== definitely lost: 4 bytes in 1 blocks
==2013== indirectly lost: 0 bytes in 0 blocks
==2013== possibly lost: 0 bytes in 0 blocks
==2013== still reachable: 0 bytes in 0 blocks
==2013== suppressed: 0 bytes in 0 blocks
==2013==
==2013== For counts of detected and suppressed errors, rerun with: -v
==2013== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Result
How to Perform Memory Leak Test Leveraging Valgrind
© RapidValue Solutions Confidential 4
There are 3 main parts to Valgrind output:
1. The Heap Summary tells you the number of bytes in use when the program exits, the number of
memory allocations (anytime the new operator is used), the number of frees (whenever the delete
operator is used), and the total number of bytes allocated.
2. The Leak Summary tells you what memory your program might have leaked. Anything lost means that
some heap-allocated memory can no longer be reached by your program. In general, all memory
should be tracked and none should be untracked.
3. The Error Summary tells you how many errors occurred during the execution of your program.
The above result shows “definitely lost: 4 bytes in 1 blocks” which represents the memory leak. Any
leaks listed as "definitely lost" should be properly fixed (as should ones listed "indirectly lost" or
"possibly lost" -- "indirectly lost" happens when you do something like freeing the root node of a tree but
not the rest of it, and "possibly lost" indicates that the memory is actually lost). If the program with
definitely lost runs for a long time, it will use a lot of memory that is not needed. The above example
says the program allocates a buffer and returns it, but the caller never frees the memory after it is
finished.
1. Similarly let us consider a C program test.c without memory leak.
2. Compile the program:
$gcc -Wall -g test.c -o test
3. Run the program along with valgrind:
$valgrind --leak-check=yes ./test
The result shows “All heap blocks were freed -- no leaks are possible” which represents that there is no
memory leak.
R
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(void) {
int * ptr = (int * ) malloc(sizeof(int));
/* Do some work */
free(ptr);
return 0; /* Return without freeing ptr*/
}
The code represents that
the memory allocated for
pointer ‘ptr’ (a size of ‘int’
which is 4 bytes) which
are freed. So probably
there should not be a
memory leak.
How to Perform Memory Leak Test Leveraging Valgrind
© RapidValue Solutions Confidential 5
nisar@RVSKCH33DT:~$ valgrind --leak-check=yes ./test
==2372== Memcheck, a memory error detector
==2372== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==2372== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==2372== Command: ./test
==2372==
==2372==
==2372== HEAP SUMMARY:
==2372== in use at exit: 0 bytes in 0 blocks
==2372== total heap usage: 1 allocs, 1 frees, 4 bytes allocated
==2372==
==2372== All heap blocks were freed -- no leaks are possible
==2372==
==2372== For counts of detected and suppressed errors, rerun with: -v
==2372== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Result
How to Perform Memory Leak Test Leveraging Valgrind in Android
1. Download Valgrind and cross compile the package for android platform using “. /configure” option under
valgrind folder.
2. Step 1 requires supported android “NDK” with test devices CPU compatible platforms (like ARM, X86 etc.).
3. Push the valgrind package to android device.
4. Set “VALGRIND_LIB”
$ export VALGRIND_LIB /data/local/tmp/Inst/lib/valgrind
$ /data/local/tmp/Inst/bin/valgrind --version
$ valgrind-3.13.0
5. Install Android APP(With JNI) with debug enabled mode in to android device.
6. Start valgrind using following comment.
How to Perform Memory Leak Test Leveraging Valgrind
© RapidValue Solutions Confidential 6
$ VGPARAMS='-v --error-limit=no --trace-children=yes --log-file=/sdcard/valgrind.log.%p --
tool=memcheck --leak-check=full --show-reachable=yes'
$ exec /data/local/tmp/Inst/bin/valgrind $ VGPARAMS
7. Start Android APP(With JNI) using
$ adb shell am start -a android.intent.action.MAIN -n $PACKAGE/.HelloJni
8. The log file should be generated and can be grep by using adb shell ls -lR "/sdcard/*grind*"
How to Perform Memory Leak Test Leveraging Valgrind
© RapidValue Solutions Confidential 7
RapidValue is a leading provider of end-to-end mobility, omni-channel, IoT and cloud solutions to
enterprises worldwide. Armed with a large team of experts in consulting, UX design, application
engineering and testing, along with experience delivering global projects, we offer a range of services
across various industry verticals. RapidValue delivers its services to the world’s top brands and Fortune
1000 companies, and has offices in the United States and India.
Disclaimer:
This document contains information that is confidential and proprietary to RapidValue Solutions Inc. No part of it may be used,
circulated, quoted, or reproduced for distribution outside RapidValue. If you are not the intended recipient of this report, you are
hereby notified that the use, circulation, quoting, or reproducing of this report is strictly prohibited and may be unlawful.
© RapidValue Solutions
www.rapidvaluesolutions.com/blogwww.rapidvaluesolutions.com
+1 877.643.1850 contactus@rapidvaluesolutions.com

More Related Content

What's hot (20)

PDF
Top 10 bugs in C++ open source projects, checked in 2016
PVS-Studio
 
PPTX
PVS-Studio, a static analyzer detecting errors in the source code of C/C++/C+...
Andrey Karpov
 
PDF
PVS-Studio for Visual C++
Andrey Karpov
 
PDF
Checking Notepad++: five years later
PVS-Studio
 
PPTX
Python Programming Essentials - M27 - Logging module
P3 InfoTech Solutions Pvt. Ltd.
 
PDF
C&cpu
feathertw
 
PDF
PHPUnit your bug exterminator
rjsmelo
 
PDF
Picking Mushrooms after Cppcheck
Andrey Karpov
 
PDF
Linux Kernel, tested by the Linux-version of PVS-Studio
PVS-Studio
 
PPTX
Python Programming Essentials - M39 - Unit Testing
P3 InfoTech Solutions Pvt. Ltd.
 
PPTX
Python Programming Essentials - M35 - Iterators & Generators
P3 InfoTech Solutions Pvt. Ltd.
 
PPTX
PVS-Studio. Static code analyzer. Windows/Linux, C/C++/C#. 2017
Andrey Karpov
 
PDF
Checking the Qt 5 Framework
Andrey Karpov
 
PDF
Photon Server Deep Dive - View from Implmentation of PhotonWire, Multiplayer ...
Yoshifumi Kawai
 
PDF
JavaOne 2017 - The hitchhiker’s guide to Java class reloading
Anton Arhipov
 
KEY
groovy & grails - lecture 5
Alexandre Masselot
 
PDF
Analysis of bugs in Orchard CMS
PVS-Studio
 
PDF
Linux version of PVS-Studio couldn't help checking CodeLite
PVS-Studio
 
PPT
Python testing
John(Qiang) Zhang
 
PDF
Why Windows 8 drivers are buggy
Andrey Karpov
 
Top 10 bugs in C++ open source projects, checked in 2016
PVS-Studio
 
PVS-Studio, a static analyzer detecting errors in the source code of C/C++/C+...
Andrey Karpov
 
PVS-Studio for Visual C++
Andrey Karpov
 
Checking Notepad++: five years later
PVS-Studio
 
Python Programming Essentials - M27 - Logging module
P3 InfoTech Solutions Pvt. Ltd.
 
C&cpu
feathertw
 
PHPUnit your bug exterminator
rjsmelo
 
Picking Mushrooms after Cppcheck
Andrey Karpov
 
Linux Kernel, tested by the Linux-version of PVS-Studio
PVS-Studio
 
Python Programming Essentials - M39 - Unit Testing
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M35 - Iterators & Generators
P3 InfoTech Solutions Pvt. Ltd.
 
PVS-Studio. Static code analyzer. Windows/Linux, C/C++/C#. 2017
Andrey Karpov
 
Checking the Qt 5 Framework
Andrey Karpov
 
Photon Server Deep Dive - View from Implmentation of PhotonWire, Multiplayer ...
Yoshifumi Kawai
 
JavaOne 2017 - The hitchhiker’s guide to Java class reloading
Anton Arhipov
 
groovy & grails - lecture 5
Alexandre Masselot
 
Analysis of bugs in Orchard CMS
PVS-Studio
 
Linux version of PVS-Studio couldn't help checking CodeLite
PVS-Studio
 
Python testing
John(Qiang) Zhang
 
Why Windows 8 drivers are buggy
Andrey Karpov
 

Similar to How to Perform Memory Leak Test Using Valgrind (20)

PDF
Better Embedded 2013 - Detecting Memory Leaks with Valgrind
Rigels Gordani
 
DOCX
Valgrind debugger Tutorial
Anurag Tomar
 
PDF
Valgrind tutorial
Satabdi Das
 
PDF
150412 38 beamer methods of binary analysis
Raghu Palakodety
 
PDF
Debugging tools
Matteo Virgilio
 
PPTX
Valgrind
Md. Golam Hossain
 
PDF
Memory Leak Debuging in the Semi conductor Hardwares
Karthick Rajagopal
 
PPTX
C++ memory leak detection
Võ Hòa
 
PDF
Taint-based Dynamic Analysis (CoC Research Day 2009)
James Clause
 
PPTX
Valgrind tool
KrishnaPrasad630
 
PDF
Memory Management and Leaks in Postgres from pgext.day 2025
Phil Eaton
 
PDF
Leakpoint: Pinpointing the Causes of Memory Leaks (ICSE 2010)
James Clause
 
PDF
MSL2009. Valgrind
Juan A. Suárez Romero
 
PDF
TotalView - Memory debugging in parallel and distributed applications
IBM India Smarter Computing
 
PDF
150104 3 methods for-binary_analysis_and_valgrind
Raghu Palakodety
 
PDF
Detecting Memory Leaks in Android A/B Tests: A Production-Focused Approach by...
ScyllaDB
 
PDF
Software Design: Impact of Memory Usage (Copying, Cloning and Aliases)
Adair Dingle
 
PDF
Memory Debugging
Alain Leon
 
ODP
An Introduction to PC-Lint
Ralf Holly
 
PPT
Security Applications For Emulation
Silvio Cesare
 
Better Embedded 2013 - Detecting Memory Leaks with Valgrind
Rigels Gordani
 
Valgrind debugger Tutorial
Anurag Tomar
 
Valgrind tutorial
Satabdi Das
 
150412 38 beamer methods of binary analysis
Raghu Palakodety
 
Debugging tools
Matteo Virgilio
 
Memory Leak Debuging in the Semi conductor Hardwares
Karthick Rajagopal
 
C++ memory leak detection
Võ Hòa
 
Taint-based Dynamic Analysis (CoC Research Day 2009)
James Clause
 
Valgrind tool
KrishnaPrasad630
 
Memory Management and Leaks in Postgres from pgext.day 2025
Phil Eaton
 
Leakpoint: Pinpointing the Causes of Memory Leaks (ICSE 2010)
James Clause
 
MSL2009. Valgrind
Juan A. Suárez Romero
 
TotalView - Memory debugging in parallel and distributed applications
IBM India Smarter Computing
 
150104 3 methods for-binary_analysis_and_valgrind
Raghu Palakodety
 
Detecting Memory Leaks in Android A/B Tests: A Production-Focused Approach by...
ScyllaDB
 
Software Design: Impact of Memory Usage (Copying, Cloning and Aliases)
Adair Dingle
 
Memory Debugging
Alain Leon
 
An Introduction to PC-Lint
Ralf Holly
 
Security Applications For Emulation
Silvio Cesare
 
Ad

More from RapidValue (20)

PDF
How to Build a Micro-Application using Single-Spa
RapidValue
 
PDF
Play with Jenkins Pipeline
RapidValue
 
PDF
Accessibility Testing using Axe
RapidValue
 
PDF
Guide to Generate Extent Report in Kotlin
RapidValue
 
PDF
Automation in Digital Cloud Labs
RapidValue
 
PDF
Microservices Architecture - Top Trends & Key Business Benefits
RapidValue
 
PDF
Uploading Data Using Oracle Web ADI
RapidValue
 
PDF
Appium Automation with Kotlin
RapidValue
 
PDF
Build UI of the Future with React 360
RapidValue
 
PDF
Python Google Cloud Function with CORS
RapidValue
 
PDF
Real-time Automation Result in Slack Channel
RapidValue
 
PDF
Automation Testing with KATALON Cucumber BDD
RapidValue
 
PDF
How to Implement Micro Frontend Architecture using Angular Framework
RapidValue
 
PDF
Video Recording of Selenium Automation Flows
RapidValue
 
PDF
JMeter JMX Script Creation via BlazeMeter
RapidValue
 
PDF
Migration to Extent Report 4
RapidValue
 
PDF
The Definitive Guide to Implementing Shift Left Testing in QA
RapidValue
 
PDF
Data Seeding via Parameterized API Requests
RapidValue
 
PDF
Test Case Creation in Katalon Studio
RapidValue
 
PDF
DevOps Continuous Integration & Delivery - A Whitepaper by RapidValue
RapidValue
 
How to Build a Micro-Application using Single-Spa
RapidValue
 
Play with Jenkins Pipeline
RapidValue
 
Accessibility Testing using Axe
RapidValue
 
Guide to Generate Extent Report in Kotlin
RapidValue
 
Automation in Digital Cloud Labs
RapidValue
 
Microservices Architecture - Top Trends & Key Business Benefits
RapidValue
 
Uploading Data Using Oracle Web ADI
RapidValue
 
Appium Automation with Kotlin
RapidValue
 
Build UI of the Future with React 360
RapidValue
 
Python Google Cloud Function with CORS
RapidValue
 
Real-time Automation Result in Slack Channel
RapidValue
 
Automation Testing with KATALON Cucumber BDD
RapidValue
 
How to Implement Micro Frontend Architecture using Angular Framework
RapidValue
 
Video Recording of Selenium Automation Flows
RapidValue
 
JMeter JMX Script Creation via BlazeMeter
RapidValue
 
Migration to Extent Report 4
RapidValue
 
The Definitive Guide to Implementing Shift Left Testing in QA
RapidValue
 
Data Seeding via Parameterized API Requests
RapidValue
 
Test Case Creation in Katalon Studio
RapidValue
 
DevOps Continuous Integration & Delivery - A Whitepaper by RapidValue
RapidValue
 
Ad

Recently uploaded (20)

PDF
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
PDF
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
PPTX
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
Tally software_Introduction_Presentation
AditiBansal54083
 
PDF
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
PDF
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
PPTX
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
PDF
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
PDF
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
PPTX
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
PDF
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
PDF
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
PDF
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
PPTX
Human Resources Information System (HRIS)
Amity University, Patna
 
PPTX
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
PDF
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
PDF
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
PPTX
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Tally software_Introduction_Presentation
AditiBansal54083
 
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
Human Resources Information System (HRIS)
Amity University, Patna
 
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 

How to Perform Memory Leak Test Using Valgrind

  • 1. How to Perform Memory Leak Test Leveraging Valgrind Presented by: Muhammed Nisar PK, Senior Software Engineer-Testing
  • 2. How to Perform Memory Leak Test Leveraging Valgrind © RapidValue Solutions Confidential 2 How to Perform Memory Leak Test Leveraging Valgrind 1. Install Valgrind: $sudo apt install valgrind # Ubuntu, Debian, etc. 2. Install gcc: * gcc stands for GNU Compiler Collections. This is used to compile mainly C and C++ language. $sudo apt install gcc 3. Let us consider a C program test.c with memory leak The code represents that the memory allocated for pointer ‘ptr’ (a size of ‘int’ which is 4 bytes) which are not freed. So probably this should be detected as memory leak. 4. Give file permission for the sample program file: $chmod 777 test.c 5. Compile the program: $gcc -Wall -g test.c -o test 6. Run the program along with valgrind: $valgrind --leak-check=yes ./test or $valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --verbose --log-file=valgrind-out.txt ./test (This will create one file with valgrind logs) #include<stdio.h> #include<string.h> #include<stdlib.h> int main(void) { int * ptr = (int * ) malloc(sizeof(int)); /* Do some work */ return 0; /* Return without freeing ptr*/ }
  • 3. How to Perform Memory Leak Test Leveraging Valgrind © RapidValue Solutions Confidential 3 nisar@RVSKCH33DT:~$ valgrind --leak-check=yes ./test ==2013== Memcheck, a memory error detector ==2013== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. ==2013== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info ==2013== Command: ./test ==2013== ==2013== ==2013== HEAP SUMMARY: ==2013== in use at exit: 4 bytes in 1 blocks ==2013== total heap usage: 1 allocs, 0 frees, 4 bytes allocated ==2013== ==2013== 4 bytes in 1 blocks are definitely lost in loss record 1 of 1 ==2013== at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64- linux.so) ==2013== by 0x10865B: main (test.c:7) ==2013== ==2013== LEAK SUMMARY: ==2013== definitely lost: 4 bytes in 1 blocks ==2013== indirectly lost: 0 bytes in 0 blocks ==2013== possibly lost: 0 bytes in 0 blocks ==2013== still reachable: 0 bytes in 0 blocks ==2013== suppressed: 0 bytes in 0 blocks ==2013== ==2013== For counts of detected and suppressed errors, rerun with: -v ==2013== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0) Result
  • 4. How to Perform Memory Leak Test Leveraging Valgrind © RapidValue Solutions Confidential 4 There are 3 main parts to Valgrind output: 1. The Heap Summary tells you the number of bytes in use when the program exits, the number of memory allocations (anytime the new operator is used), the number of frees (whenever the delete operator is used), and the total number of bytes allocated. 2. The Leak Summary tells you what memory your program might have leaked. Anything lost means that some heap-allocated memory can no longer be reached by your program. In general, all memory should be tracked and none should be untracked. 3. The Error Summary tells you how many errors occurred during the execution of your program. The above result shows “definitely lost: 4 bytes in 1 blocks” which represents the memory leak. Any leaks listed as "definitely lost" should be properly fixed (as should ones listed "indirectly lost" or "possibly lost" -- "indirectly lost" happens when you do something like freeing the root node of a tree but not the rest of it, and "possibly lost" indicates that the memory is actually lost). If the program with definitely lost runs for a long time, it will use a lot of memory that is not needed. The above example says the program allocates a buffer and returns it, but the caller never frees the memory after it is finished. 1. Similarly let us consider a C program test.c without memory leak. 2. Compile the program: $gcc -Wall -g test.c -o test 3. Run the program along with valgrind: $valgrind --leak-check=yes ./test The result shows “All heap blocks were freed -- no leaks are possible” which represents that there is no memory leak. R #include<stdio.h> #include<string.h> #include<stdlib.h> int main(void) { int * ptr = (int * ) malloc(sizeof(int)); /* Do some work */ free(ptr); return 0; /* Return without freeing ptr*/ } The code represents that the memory allocated for pointer ‘ptr’ (a size of ‘int’ which is 4 bytes) which are freed. So probably there should not be a memory leak.
  • 5. How to Perform Memory Leak Test Leveraging Valgrind © RapidValue Solutions Confidential 5 nisar@RVSKCH33DT:~$ valgrind --leak-check=yes ./test ==2372== Memcheck, a memory error detector ==2372== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. ==2372== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info ==2372== Command: ./test ==2372== ==2372== ==2372== HEAP SUMMARY: ==2372== in use at exit: 0 bytes in 0 blocks ==2372== total heap usage: 1 allocs, 1 frees, 4 bytes allocated ==2372== ==2372== All heap blocks were freed -- no leaks are possible ==2372== ==2372== For counts of detected and suppressed errors, rerun with: -v ==2372== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) Result How to Perform Memory Leak Test Leveraging Valgrind in Android 1. Download Valgrind and cross compile the package for android platform using “. /configure” option under valgrind folder. 2. Step 1 requires supported android “NDK” with test devices CPU compatible platforms (like ARM, X86 etc.). 3. Push the valgrind package to android device. 4. Set “VALGRIND_LIB” $ export VALGRIND_LIB /data/local/tmp/Inst/lib/valgrind $ /data/local/tmp/Inst/bin/valgrind --version $ valgrind-3.13.0 5. Install Android APP(With JNI) with debug enabled mode in to android device. 6. Start valgrind using following comment.
  • 6. How to Perform Memory Leak Test Leveraging Valgrind © RapidValue Solutions Confidential 6 $ VGPARAMS='-v --error-limit=no --trace-children=yes --log-file=/sdcard/valgrind.log.%p -- tool=memcheck --leak-check=full --show-reachable=yes' $ exec /data/local/tmp/Inst/bin/valgrind $ VGPARAMS 7. Start Android APP(With JNI) using $ adb shell am start -a android.intent.action.MAIN -n $PACKAGE/.HelloJni 8. The log file should be generated and can be grep by using adb shell ls -lR "/sdcard/*grind*"
  • 7. How to Perform Memory Leak Test Leveraging Valgrind © RapidValue Solutions Confidential 7 RapidValue is a leading provider of end-to-end mobility, omni-channel, IoT and cloud solutions to enterprises worldwide. Armed with a large team of experts in consulting, UX design, application engineering and testing, along with experience delivering global projects, we offer a range of services across various industry verticals. RapidValue delivers its services to the world’s top brands and Fortune 1000 companies, and has offices in the United States and India. Disclaimer: This document contains information that is confidential and proprietary to RapidValue Solutions Inc. No part of it may be used, circulated, quoted, or reproduced for distribution outside RapidValue. If you are not the intended recipient of this report, you are hereby notified that the use, circulation, quoting, or reproducing of this report is strictly prohibited and may be unlawful. © RapidValue Solutions www.rapidvaluesolutions.com/blogwww.rapidvaluesolutions.com +1 877.643.1850 [email protected]