Red Hat Enterprise Linux Diagnostics and Troubleshooting
Identify memory leaks.
Outcomes
You should be able to use the valgrind framework to identify a virtual memory leak and a resident memory leak.
As the student user on the workstation machine, use the lab command to prepare your system for this exercise.
[student@workstation ~]$ lab start application-memory
This command installs the bigmem application that causes a memory leak.
Instructions
The bigmem application in this exercise causes a memory leak. To verify the memory leak, run the bigmem application with the memcheck tool in the valgrind framework.
Log in to
serveraand switch to therootuser.[student@workstation ~]$
ssh student@servera...output omitted... [student@servera ~]$sudo -i[sudo] password for student:student[root@servera ~]#Install the
valgrindpackage on theserverasystem.[root@servera ~]#
yum install valgrind...output omitted... Complete!Use the
valgrindtool to run thebigmemcommand, allocating256MiB of physical memory.The
valgrindsummary indicates a memory leak.[root@servera ~]#
valgrind --tool=memcheck bigmem 256==26907== Memcheck, a memory error detector ==26907== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. ==26907== Using Valgrind-3.16.0 and LibVEX; rerun with -h for copyright info ==26907== Command: bigmem 256 ==26907== Attempting to allocate 256 Mebibytes of resident memory... Press <Enter> to exitEnter==26907== ==26907== HEAP SUMMARY: ==26907== in use at exit: 268,435,456 bytes in 256 blocks ==26907== total heap usage: 258 allocs, 2 frees, 268,437,504 bytes allocated ==26907== ==26907== LEAK SUMMARY: ==26907== definitely lost: 263,192,576 bytes in 251 blocks ==26907== indirectly lost: 0 bytes in 0 blocks ==26907== possibly lost: 5,242,880 bytes in 5 blocks ==26907== still reachable: 0 bytes in 0 blocks ==26907== suppressed: 0 bytes in 0 blocks ==26907== Rerun with --leak-check=full to see details of leaked memory ==26907== ==26907== For lists of detected and suppressed errors, rerun with: -s ==26907== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)Use
valgrindto run thebigmemapplication again, with256MiB of virtual memory.The summary output matches the output for physical memory.
[root@servera ~]#
valgrind --tool=memcheck bigmem -v 256==26910== Memcheck, a memory error detector ==26910== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. ==26910== Using Valgrind-3.16.0 and LibVEX; rerun with -h for copyright info ==26910== Command: bigmem -v 256 ==26910== Attempting to allocate 256 MebiBytes of virtual memory... Press <Enter> to exitEnter==26910== ==26910== HEAP SUMMARY: ==26910== in use at exit: 268,435,456 bytes in 256 blocks ==26910== total heap usage: 258 allocs, 2 frees, 268,437,504 bytes allocated ==26910== ==26910== LEAK SUMMARY: ==26910== definitely lost: 264,241,152 bytes in 252 blocks ==26910== indirectly lost: 0 bytes in 0 blocks ==26910== possibly lost: 4,194,304 bytes in 4 blocks ==26910== still reachable: 0 bytes in 0 blocks ==26910== suppressed: 0 bytes in 0 blocks ==26910== Rerun with --leak-check=full to see details of leaked memory ==26910== ==26910== For lists of detected and suppressed errors, rerun with: -s ==26910== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)The
valgrindcommand detected the memory leak but provides insufficient information to know the type of memory leak from thebigmemapplication.In a second terminal, log in to the
serverasystem and switch to therootuser.[student@workstation ~]$
ssh student@servera...output omitted... [student@servera ~]$sudo -i[sudo] password for student:studentContinuously display an overview of the system's allocated and committed memory.
[root@servera ~]#
watch -d -n1 'free -m; grep -i commit /proc/meminfo'In the first terminal window, run the
bigmemapplication twice to allocate physical memory and virtual memory. This time, do not use thevalgrindtool. Before you press Enter for both of the commands, navigate to the terminal running thewatchcommand and verify the output.[root@servera ~]#
bigmem 256Attempting to allocate 256 Mebibytes of resident memory... Press <Enter> to exit [root@servera ~]#bigmem -v 256Attempting to allocate 256 Mebibytes of virtual memory... Press <Enter> to exitView the system's allocated and committed memory in the second terminal.
The
watchcommand output shows thebigmemapplication using the requested memory when run without the-voption. In this case, both committed memory (Committed_Asfield) and resident memory (usedcolumn) rise by the requested amount of memory.When run with the
-voption, the committed memory (Committed_Asfield) increases with the requested amount, but resident memory (usedcolumn) does not increase significantly. The resident memory (usedcolumn) increase slightly due to memory for executing itself and its page tables.Use Ctrl+C to exit the
watchcommand in the second terminal.
Exit the second terminal.
[root@servera ~]#
exit[student@servera ~]$exit[student@workstation ~]$exitReturn to the
workstationmachine as thestudentuser on first terminal.[root@servera ~]#
exit[student@servera ~]$exit[student@workstation ~]$