Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Valgrind

Valgrind is a programming tool for memory debugging, memory leak detection, and profiling

...

. It shows memory leaks

...

and deallocation errors in the program during

...

runtime.

Valgrind Installation

Add valgrind to the package group will enable the valgrind support for your platform 

...

How to use valgrind for memory check? 

On a successful build, flash the binary to the board. Once the board is up, run the Valgrind command in the two ways shown below.

From console

Code Block
languagec#
themeDJango
valgrind --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=20 --track-fds=yes /usr/bin/<component> 

The memory leak summary will be seen on the console 

Sample: 
root@RaspberryPi-Gateway:~# valgrind --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=20 --track-fds=yes /usr/bin/sampleAppn 

==12907== Memcheck, a memory error detector 

==12907== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al. 

==12907== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info 

==12907== Command: /usr/bin/sampleAppn 

==12907== 

Hello Sample Program     1600439223.000000 

==12907== Invalid write of size 1 

==12907==    at 0x8458: ??? (in /usr/bin/sampleAppn) 

==12907==  Address 0x4909462 is 0 bytes after a block of size 10 alloc'd 

==12907==    at 0x48294CC: malloc (in /usr/lib/valgrind/vgpreload_memcheck-arm-linux.so) 

==12907== 

Hello Sample Program     1600439225.000000 

Hello Sample Program     1600439227.000000 

Hello Sample Program     1600439229.000000 

Hello Sample Program     1600439231.000000 

Hello Sample Program     1600439233.000000 

Hello Sample Program     1600439235.000000 

Hello Sample Program     1600439237.000000 

Hello Sample Program     1600439239.000000 

Hello Sample Program     1600439241.000000 

Hello Sample Program     1600439243.000000 

Hello Sample Program     1600439245.000000 

Hello Sample Program     1600439247.000000 

Hello Sample Program     1600439249.000000 

Hello Sample Program     1600439251.000000 

Hello Sample Program     1600439253.000000 

Hello Sample Program     1600439255.000000 

Hello Sample Program     1600439257.000000 

Hello Sample Program     1600439259.000000 

Hello Sample Program     1600439261.000000 

Hello Sample Program     1600439263.000000 

Hello Sample Program     1600439265.000000 

Hello Sample Program     1600439267.000000 

Hello Sample Program     1600439269.000000 

Hello Sample Program     1600439271.000000 

Hello Sample Program     1600439273.000000 

Hello Sample Program     1600439275.000000 

Hello Sample Program     1600439277.000000 

Hello Sample Program     1600439279.000000 

==12907== 

==12907== FILE DESCRIPTORS: 3 open at exit. 

==12907== Open file descriptor 2: /dev/pts/0 

==12907==    <inherited from parent> 

==12907== 

==12907== Open file descriptor 1: /dev/pts/0 

==12907==    <inherited from parent> 

==12907== 

==12907== Open file descriptor 0: /dev/pts/0 

==12907==    <inherited from parent> 

==12907== 

==12907== 

==12907== HEAP SUMMARY: 

==12907==     in use at exit: 290 bytes in 29 blocks 

==12907==   total heap usage: 30 allocs, 1 frees, 1,314 bytes allocated 

==12907== 

==12907== 290 bytes in 29 blocks are definitely lost in loss record 1 of 1 

==12907==    at 0x48294CC: malloc (in /usr/lib/valgrind/vgpreload_memcheck-arm-linux.so) 

==12907== 

==12907== LEAK SUMMARY: 

==12907==    definitely lost: 290 bytes in 29 blocks  >> This indicates the memory leak 

==12907==    indirectly lost: 0 bytes in 0 blocks 

==12907==      possibly lost: 0 bytes in 0 blocks 

==12907==    still reachable: 0 bytes in 0 blocks 

==12907==         suppressed: 0 bytes in 0 blocks 

==12907== 

==12907== For counts of detected and suppressed errors, rerun with: -v 

==12907== ERROR SUMMARY: 30 errors from 2 contexts (suppressed: 6 from 3) 

From service file 

Invoke the

...

Valgrind command from the service file

...

. Once the service is started, it checks for

...

memory

...

leaks

Code Block
languagec#
themeDJango
root@RaspberryPi-Gateway:~# cat /lib/systemd/system/sample.service 

[Unit] 

Description=Example sampleapp systemd service. 

After=CcspPandMSsp.service 

[Service] 

Type=simple 

ExecStart=/bin/sh -c 'valgrind --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=20 --track-fds=yes /usr/bin/sampleAppn' 

ExecStop=/bin/sh -c ' echo "Sample service Stopped" ' 

[Install] 

WantedBy=multi-user.target 

...