SlideShare uma empresa Scribd logo
1 de 7
Baixar para ler offline
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

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

Top 10 bugs in C++ open source projects, checked in 2016
Top 10 bugs in C++ open source projects, checked in 2016Top 10 bugs in C++ open source projects, checked in 2016
Top 10 bugs in C++ open source projects, checked in 2016
 
PVS-Studio, a static analyzer detecting errors in the source code of C/C++/C+...
PVS-Studio, a static analyzer detecting errors in the source code of C/C++/C+...PVS-Studio, a static analyzer detecting errors in the source code of C/C++/C+...
PVS-Studio, a static analyzer detecting errors in the source code of C/C++/C+...
 
PVS-Studio for Visual C++
PVS-Studio for Visual C++PVS-Studio for Visual C++
PVS-Studio for Visual C++
 
Checking Notepad++: five years later
Checking Notepad++: five years laterChecking Notepad++: five years later
Checking Notepad++: five years later
 
Python Programming Essentials - M27 - Logging module
Python Programming Essentials - M27 - Logging modulePython Programming Essentials - M27 - Logging module
Python Programming Essentials - M27 - Logging module
 
C&cpu
C&cpuC&cpu
C&cpu
 
PHPUnit your bug exterminator
PHPUnit your bug exterminatorPHPUnit your bug exterminator
PHPUnit your bug exterminator
 
Picking Mushrooms after Cppcheck
Picking Mushrooms after CppcheckPicking Mushrooms after Cppcheck
Picking Mushrooms after Cppcheck
 
Linux Kernel, tested by the Linux-version of PVS-Studio
Linux Kernel, tested by the Linux-version of PVS-StudioLinux Kernel, tested by the Linux-version of PVS-Studio
Linux Kernel, tested by the Linux-version of PVS-Studio
 
Python Programming Essentials - M39 - Unit Testing
Python Programming Essentials - M39 - Unit TestingPython Programming Essentials - M39 - Unit Testing
Python Programming Essentials - M39 - Unit Testing
 
Python Programming Essentials - M35 - Iterators & Generators
Python Programming Essentials - M35 - Iterators & GeneratorsPython Programming Essentials - M35 - Iterators & Generators
Python Programming Essentials - M35 - Iterators & Generators
 
PVS-Studio. Static code analyzer. Windows/Linux, C/C++/C#. 2017
PVS-Studio. Static code analyzer. Windows/Linux, C/C++/C#. 2017PVS-Studio. Static code analyzer. Windows/Linux, C/C++/C#. 2017
PVS-Studio. Static code analyzer. Windows/Linux, C/C++/C#. 2017
 
Checking the Qt 5 Framework
Checking the Qt 5 FrameworkChecking the Qt 5 Framework
Checking the Qt 5 Framework
 
Photon Server Deep Dive - View from Implmentation of PhotonWire, Multiplayer ...
Photon Server Deep Dive - View from Implmentation of PhotonWire, Multiplayer ...Photon Server Deep Dive - View from Implmentation of PhotonWire, Multiplayer ...
Photon Server Deep Dive - View from Implmentation of PhotonWire, Multiplayer ...
 
JavaOne 2017 - The hitchhiker’s guide to Java class reloading
JavaOne 2017 - The hitchhiker’s guide to Java class reloadingJavaOne 2017 - The hitchhiker’s guide to Java class reloading
JavaOne 2017 - The hitchhiker’s guide to Java class reloading
 
groovy & grails - lecture 5
groovy & grails - lecture 5groovy & grails - lecture 5
groovy & grails - lecture 5
 
Analysis of bugs in Orchard CMS
Analysis of bugs in Orchard CMSAnalysis of bugs in Orchard CMS
Analysis of bugs in Orchard CMS
 
Linux version of PVS-Studio couldn't help checking CodeLite
Linux version of PVS-Studio couldn't help checking CodeLiteLinux version of PVS-Studio couldn't help checking CodeLite
Linux version of PVS-Studio couldn't help checking CodeLite
 
Python testing
Python  testingPython  testing
Python testing
 
Why Windows 8 drivers are buggy
Why Windows 8 drivers are buggyWhy Windows 8 drivers are buggy
Why Windows 8 drivers are buggy
 

Semelhante a How to Perform Memory Leak Test Using Valgrind

Analytics tools and Instruments
Analytics tools and InstrumentsAnalytics tools and Instruments
Analytics tools and Instruments
Krunal Soni
 

Semelhante a How to Perform Memory Leak Test Using Valgrind (20)

Better Embedded 2013 - Detecting Memory Leaks with Valgrind
Better Embedded 2013 - Detecting Memory Leaks with ValgrindBetter Embedded 2013 - Detecting Memory Leaks with Valgrind
Better Embedded 2013 - Detecting Memory Leaks with Valgrind
 
[xp2013] Narrow Down What to Test
[xp2013] Narrow Down What to Test[xp2013] Narrow Down What to Test
[xp2013] Narrow Down What to Test
 
Valgrind debugger Tutorial
Valgrind debugger TutorialValgrind debugger Tutorial
Valgrind debugger Tutorial
 
Advanced debugging  techniques in different environments
Advanced debugging  techniques in different environmentsAdvanced debugging  techniques in different environments
Advanced debugging  techniques in different environments
 
Android tools for testers
Android tools for testersAndroid tools for testers
Android tools for testers
 
Valgrind tutorial
Valgrind tutorialValgrind tutorial
Valgrind tutorial
 
Valgrind
ValgrindValgrind
Valgrind
 
Os Selbak
Os SelbakOs Selbak
Os Selbak
 
Storage classes in C
Storage classes in CStorage classes in C
Storage classes in C
 
Using the android ndk - DroidCon Paris 2014
Using the android ndk - DroidCon Paris 2014Using the android ndk - DroidCon Paris 2014
Using the android ndk - DroidCon Paris 2014
 
Code quality par Simone Civetta
Code quality par Simone CivettaCode quality par Simone Civetta
Code quality par Simone Civetta
 
Android crash debugging
Android crash debuggingAndroid crash debugging
Android crash debugging
 
Containerize your Blackbox tests
Containerize your Blackbox testsContainerize your Blackbox tests
Containerize your Blackbox tests
 
The Green Lab - [04 B] [PWA] Experiment setup
The Green Lab - [04 B] [PWA] Experiment setupThe Green Lab - [04 B] [PWA] Experiment setup
The Green Lab - [04 B] [PWA] Experiment setup
 
JavaScript - Chapter 15 - Debugging Techniques
 JavaScript - Chapter 15 - Debugging Techniques JavaScript - Chapter 15 - Debugging Techniques
JavaScript - Chapter 15 - Debugging Techniques
 
Analytics tools and Instruments
Analytics tools and InstrumentsAnalytics tools and Instruments
Analytics tools and Instruments
 
It Works On Dev
It Works On DevIt Works On Dev
It Works On Dev
 
Virus lab
Virus labVirus lab
Virus lab
 
Discussing Errors in Unity3D's Open-Source Components
Discussing Errors in Unity3D's Open-Source ComponentsDiscussing Errors in Unity3D's Open-Source Components
Discussing Errors in Unity3D's Open-Source Components
 
How Secure Are Docker Containers?
How Secure Are Docker Containers?How Secure Are Docker Containers?
How Secure Are Docker Containers?
 

Mais de RapidValue

The Definitive Guide to Implementing Shift Left Testing in QA
The Definitive Guide to Implementing Shift Left Testing in QAThe Definitive Guide to Implementing Shift Left Testing in QA
The Definitive Guide to Implementing Shift Left Testing in QA
RapidValue
 

Mais de RapidValue (20)

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

Último

Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
VictoriaMetrics
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 

Último (20)

Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 

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 contactus@rapidvaluesolutions.com