My First Post      My Facebook Profile      My MeOnShow Profile      W3LC Facebook Page      Learners Consortium Group      Job Portal      Shopping @Yeyhi.com

Pages










Showing posts with label memory. Show all posts
Showing posts with label memory. Show all posts

Monday, February 20, 2012

Analysis of Valgrind “Still-reachable” memory leak

Valgrind, on Linux and FreeBSD etc, reports “Still reachable” in Memory Leaks section using the memcheck or mc tool. What is REAL Memory Leak and why some leaks are called False Positives?

Many programs use the C++ STL and string classes. Valgrind, at times (rather too often ;) ) reports “still reachable” memory leaks involving these classes at the exit of the program. Ideally, there should be no such occurrence, so what should be done.
To take this article further, one thing that has to be kept in mind that it may not be a bug. Every tool has its own False positives, and Valgrind is no exception. And in this case, this is not even a False positive. It just indicates a possible issue, which may be just intended by the programmer.

This article can be regarded as an extension to the memory leak tutorial on Valgrind. It will make the Software Quality Assurance Engineers and developers more confident on their releases. Here I discuss the Still Reachable part of the Valgrind memcheck report


Let us assume that you started a Valgrind with its memcheck tool, and Valgrind initial logs say as following:
==12078== Memcheck, a memory error detector
==12078== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==12078== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info
==12078== Command: ./mohd-anwar-faiz-test-tool --helloWorld /targetFolder/
==12078== Parent PID: 1287
--12078-- Valgrind options:
--12078-- --tool=memcheck
--12078-- --leak-check=yes
--12078-- --show-reachable=yes
--12078-- --num-callers=20
--12078-- --track-fds=yes
--12078-- -v
--12078-- --log-file=/perforce/work/mohd-anwar-faiz-test-tool.txt
--12078-- Contents of /proc/version:
--12078-- Arch and hwcaps: AMD64, amd64-sse3-cx16

…, and the processing goes on.

So, this logs simply tells you about the options you used. And, this gives you the confidence that memory leaks will be catched.

Now, let us see what actually a memory leak is in this context. During my stint with Software Product Development companies like Adobe and Symantec, I have really discovered more than one definition to the concept of a "memory leak".

The first says that a "memory leak" happens when, " Some memory that has been allocated is not subsequently freed before the termination of the program." However, I have seen programmers arguing (And that is why probably we hear terms like False Positives. Voila!), that certain memory leaks that fall into above category do not actually pose any threat, and therefore must not be regarded as real "memory leaks". They are NOT REAL “memory leaks”.

Another definition says that a "memory leak" happens when, "Some memory was allocated but that cannot be subsequently freed. This situation may arise if the program no longer holds any pointers to the allocated memory." That is, memory cannot be freed since you no longer have any pointers to that block. Such a condition is then a REAL "memory leak".

Now coming to Valgrind, it uses the latter definition for the term "memory leak". This is the type of leak which may potentially cause real heap depletion, particularly for long lived processes. These can be surfaced by persistence tests. And, tools like Valgrind, AppVerifier, Fortify etc come into roles.




Let us assume that your application gave result as follows:

==12078== LEAK SUMMARY:
==12078== definitely lost: 0 bytes in 0 blocks
==12078== indirectly lost: 0 bytes in 0 blocks
==12078== possibly lost: 0 bytes in 0 blocks
==12078== still reachable: 6,720 bytes in 26 blocks
==12078== suppressed: 0 bytes in 0 blocks

So now, the above sample clearly tells that there are no memory leaks. But, still-reachable section shows some counts. Now here, it must be taken into consideration that the "still reachable" category in the Valgrind's leak report refers to allocations that fall into the category of first definition. It is true that these reported blocks were not freed; But these could have been freed. And that is what is the literal meaning of the term ‘STILL REACHABLE’. So, may be it was intentionally left by the programmer, because in any case the program during all its execution was keeping track of these pointers.

I have been in touch with my former colleague at Tata Consultancy Services over this issue (Thanks to *SO*), and was issued the same advice. Later I googled and found similar conclusion from the Valgrind FAQ document itself. As seen from documents on Valgrind official website itself Valgrind Official Documentation Page, these situations might arise and there is not much to worry about them. Read below, an snippet from their web pages itself:
"
Using GCC, you can force the STL to use malloc and to free memory as soon as possible by globally disabling memory caching. Beware! Doing so will probably slow down your program, sometimes drastically.
• With GCC 2.91, 2.95, 3.0 and 3.1, compile all source using the STL with -D__USE_MALLOC. Beware! This was removed from GCC starting with version 3.3.
• With GCC 3.2.2 and later, you should export the environment variable GLIBCPP_FORCE_NEW before running your program.
• With GCC 3.4 and later, that variable has changed name to GLIBCXX_FORCE_NEW.
There are other ways to disable memory pooling: using the malloc_alloc template with your objects (not portable, but should work for GCC) or even writing your own memory allocators. But all this goes beyond the scope of this FAQ. Start by reading http://gcc.gnu.org/onlinedocs/libstdc++/faq/index.html#4_4_leak if you absolutely want to do that. But beware: allocators belong to the more messy parts of the STL and people went to great lengths to make the STL portable across platforms. Chances are good that your solution will work on your platform, but not on others.
"

So, what I conclude and do practice is that, there is no need to worry about "still reachable" memory leaks in Valgrind. They do not pose threats as the REAL memory leaks do. Practically these NON-REAL memory leaks have no potential for heap exhaustion. You can revisit the code for gaining more confidence, if your project cycle permits that much time (which I know is rare!! :) ). There is not much need to worry for these still reachable pointers. These allocations were done and the references were kept throughout the process execution cycle. And in the end, the operating system will reclaim these allocated memory after the process termination.

That is it for the day.
-Mohd Anwar Jamal Faiz
Poet and Creative Writer. Software Engineering is both my job and passion. I love what I do!!

Valgrind Version Used: Valgrind-3.6.1 and LibVEX;
Platform Used: FreeBSD 64 bit, Linux, Solaris

To know more about me cheack: Anwar Faiz on MeOnShow

Monday, July 11, 2011

ulimit to set linux resource for testing

Recently I started using ulimit. I can seriously recommend ulimit utility as an awesome tool. Its location is /usr/bin/ulimit. Before begining let me accept that the better title for this post would have been 'how to use ulimit on linux' or 'setting resource limit and unset using ulimit', or better still, 'what is ulimit?'. [Huh..but what is wrong with my title ;) ]

Coming back, You can use 'ulimit' to set or display a resource limit. The available options are listed below. The limit for a specified resource is set when the limit is specified. The value of limit can be a number in the unit specified with each resource, or the value unlimited. I dedicate this post to my fellows in my team. My team including my manager is really great.

Back to the ulimit, the various ulimit Command options are as follows:

-a
Lists all of the current resource limits.
-c
The number of 512-byte blocks on the size of core dumps.
-d
The number of K-bytes on the size of the data area.
-f
The number of 512-byte blocks on files that can be written by the current process and its child processes (files of any size may be read). On file systems (such as vxfs file systems) that support large files, setting this limit to unlimited represents the largest file size supported on the file system. See ``Notices'', below.
-m
The number of K-bytes on the size of physical memory.
-n
The number of file descriptors plus 1.
-s
The number of K-bytes on the size of the stack area.
-t
The number of CPU seconds to be used by each process.
-v
The number of K-bytes for virtual memory.
If no option is given, -f is assumed; that is, you can specify a limit with or without the preceding -f option, and both cases are interpreted as an attempt to raise the file size limit.

There is a provision of hard and soft limit. i have never tried that, but in case yo want to explore more you can search ulimit man pages. or can even refer Click

I am attaching screenshot of ulimit -a option. this would clearly show all the limits currently set:



There are two important operands:

1) limit
Gives the resource-specific limit to use as the new resource limit.

2) unlimited
Sets the largest allowable resource limit. It is necessary to set limit to the value unlimited for access to large files.

It is important to note that ulimit works across all flavors of linux. Even on WindRiverLinux. This comes as a very handy tool for QA people testing on unix environments. Once set, you can reset the limits again using the same command. You can also make it unlimited. Following two scenarios are of great use while testing:
1) ulimit -c unlimited: this will make maximum possible space available for making core dumps. This will help fruitful core dump creations to be consumed both by the QA and dev people.
2) ulimit -v unlimited. this will make maximum possible space available for virtual memory. So this can also regulate or simulate low memory conditions.

I guess this much is enuff. Rest you can explore. Please do not forget clicking on a few Advertisements. Google will give ranking and points to my blog, you will get good contents always. ;) rest you know!!!