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 

...

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 

...

Code Block
languagec#
themeDJango
--tool=memcheck: "To the check the memory"
​
--leak-check=full: "each individual leak will be shown in detail"
  
--show-reachable=yes: "When enabled, the leak detector also shows "reachable" and "indirectly lost" blocks"
​
--num-callers=20: "Specifies the maximum number of entries shown in stack traces that identify program locations"
​
--track-fds=yes: "When enabled, Valgrind will print out a list of open file descriptors on exit."
​
/usr/bin/<component>: "Component's binary path"

Journalctl

The journalctl command is a powerful tool used to query and view logs collected by systemd. To increase the size of log files, you can update the following settings in the /etc/systemd/journald.conf file.

Filter Logs by Service: journalctl -u service


vi /etc/systemd/journald.conf
[Journal]
Storage=volatile
#Compress=yes
#Seal=yes
#SplitMode=uid
#SyncIntervalSec=5m
RateLimitIntervalSec=0
RateLimitBurst=0
SystemMaxUse=500M
#SystemKeepFree=
SystemMaxFileSize=100M
#SystemMaxFiles=100
RuntimeMaxUse=1G
#RuntimeKeepFree=
RuntimeMaxFileSize=100M
#RuntimeMaxFiles=100
#MaxRetentionSec=
#MaxFileSec=1month
ForwardToSyslog=yes
#ForwardToKMsg=no
#ForwardToConsole=no
#ForwardToWall=yes
#TTYPath=/dev/console
#MaxLevelStore=debug
#MaxLevelSyslog=debug
#MaxLevelKMsg=notice
#MaxLevelConsole=info
#MaxLevelWall=emerg
#LineMax=48K
#ReadKMsg=yes
#Audit=yes

NOTE:To get more debug info, compile the component with debug symbols enabled. 

...