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

Monday, September 23, 2013

MEMWATCH: A Memory Debugging Tool


We can say that C language is one of the most standard programming language on Linux systems as this language provides a great deal of control over dynamic memory allocation. So C language provides a lot of freedom to control your program as you wish. But this can cause memory management problems and also this can degrade our program. We use malloc() function to allocate memory but forget to released that memory with corresponding free() call which can cause Memory leaks which is one of the memory management problem. Buffer overruns i.e. writing on the previous memory locations that has already been allocated for an array can also be the problem. These problems are difficult to detect.

So there is as memory debugging tool known as MEMWATCH which is an open source memory error detection tool for C. MEMWATCH is a user level memory debugging tool. This tool has been written by Johan Lindh. You can download MEMWATCH from here.

Now how to use it? When you write the C code just simply add the header file to your code and by defining it in the gcc statement , we can track the memory leaks in our program. It will provides a log of results and detects the unfreed memory, overflow, underflow etc.

Here is program sample memory.c :

#include <stdlib.h>
#include <stdio.h>
#include "memwatch.h"

int main()
{
  char *ptr1;
  char *ptr2;

  ptr1 = malloc(320);
  ptr2 = malloc(320);

  ptr2 = ptr1;
  free(ptr2);
  free(ptr1);
}
In above code, we can see that first 320 byte blocks of memory is allocated then the pointer to the first block is set to the second block, so the address of the second block is lost which cause memory leaks.

Now compile the code:
 
  gcc -DMEMWATCH -DMW_STDIO memory.c memwatch c -o memory

After that when we run the program, MEMWATCH reproduces a report of leaked memory.

     ./memory


 You can also see the memwatch.log file.




Saturday, September 7, 2013

top and htop shell command


Every one knows that Process means program in execution and in Linux everything is works by processes. So it's important to have good knowledge of Process Management/Process Handling. Here Process Management doesn't means that we learned during our academic years in subject called Operating System or Advance Operating System, it means, simply working on process or handling it on Terminal/GNOME-Terminal.
I am writing this blog for those Mind-Hunter's who are crazy to work on Linux Terminal. See, it's simply impossible to know everything in Linux, because Linux has 1000's of Distro for different purpose but still we are crazy to learn new things and that is good.
Now back to main topic, we know that if any one wants to know about process details during execution of Process or simply when Process is running, we always suggest or use top command in terminal. Thats shows following output on terminal.

Fig:- Output of top

In above fig, we can see all processes are running but you can also check a particular process information by using top with it's option -p . But again we have to use top command and have to check for the process ID, instead of doing this you can use pgrep command to know the process ID. Normaly pgrep is use to find the process ID of any process, for this you just have to follow following command.

pgrep 'process name'

Fig:- Output of pgrep

Now to check information of single process using top command follow this command,

top -p 'process_id'

Fig:- Output of top for single process


There are many other options are also available you can try them also. To check documentation of top command typr man top on Gnome-terminal.

Top is a great command to work with runtime processes but I will suggest to use htop command. htop is almost same as top command but htop have some advance facilities for us. htop show cpu, memory and swap usage in better way.

Fig:-Output of htop

You can also modify the look of htop by using it's setup option. To do so type shift+c and you will move to setup , as per your need you can make changes in htop and to exit setup press Esc button. htop have more powerful option you can also try them. To see documentation of htop type man htop. It also have some short cut options that shown below when you use htop(i.e. Function keys).
Fig:- Setup of htop

Friday, April 26, 2013

Use valgrind and find memory errors

The segmentation fault is the most frequently occurred runtime error in the C/C++ program compiled using gcc/g++. I have seen that students use TurboC++ compiler because it doesn't show the error like “segmentation fault”. Today's engineering students does not take any efforts to analyze why such kinds of problems occurs?
A program use memory space allocated to it which uses the stack and heap area. The memory is allocated here using runtime. For this we use the malloc function or new operator. Now, when the following situations occur, the program will show the “segmentation fault”.
- Memory is not released using delete/free.
- Using the array index which is not in the specified range.
- The uninitialized pointer is referenced in the program.
- Read only memory is attempted to be used for writing.
- Already freed pointer is dereferenced.

In order to find the reason of such different kinds of memory problem, we may use the valgrind which is a memory checker utility provided by the Linux system. It is used along with GDB. It is not possible for GDB  to find the memory errors in one stroke, so valgrind can be used. It is useful in many scenarios of C/C++ programming. Few of these are discussed in this blog post. Valgrind is used to notify the user with all the errors as given above. Generally, memory leakage problems can be easily detected by the valgrind. As the gcc is very robust compiler, the system tool is very much useful in the programming.

In order to download the valgrind latest version, use the following command on debian based Linux systems such as Ubuntu/Mint.

sudo apt-get install valgrind

or use following to install it on Redhat based Linux.

sudo yum install valgrind

As C/C++ don't have any automatic garbage collector, valgrind can be used to identify the garbages in the program. Lets write a simple program in C++.

#include<iostream>
using namespace std;
int main()
{
     int *x;
     x = new int(10);
     return 0;
}

This program is creating an array of 10 numbers using pointers. So, the memory of 10 locations is allocated to variable x using new operator. Now, compile the program by enabling the debugger to it using following command.

g++ -g prog.cpp -o prog

It will create the executable file named prog. Now use valgrind to check for the memory problems.

valgrind --tool=memcheck --leak-check=yes ./prog

This uses the tool “memcheck” by enabling the checking of the memory leakage ability.

==3550== Memcheck, a memory error detector
==3550== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==3550== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==3550== Command: ./prog
==3550==
==3550==
==3550== HEAP SUMMARY:
==3550== in use at exit: 4 bytes in 1 blocks
==3550== total heap usage: 1 allocs, 0 frees, 4 bytes allocated
==3550==
==3550== 4 bytes in 1 blocks are definitely lost in loss record 1 of 1
==3550== at 0x402B733: operator new(unsigned int) (in /usr/lib/valgrind/vgpreload _memcheck -x86-linux.so)
==3550== by 0x80485B0: main (prog.cpp:6)
==3550==
==3550== LEAK SUMMARY:
==3550== definitely lost: 4 bytes in 1 blocks
==3550== indirectly lost: 0 bytes in 0 blocks
==3550== possibly lost: 0 bytes in 0 blocks
==3550== still reachable: 0 bytes in 0 blocks
==3550== suppressed: 0 bytes in 0 blocks
==3550==
==3550== For counts of detected and suppressed errors, rerun with: -v
==3550== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

Here, 3550 is the process ID of your program. It has shown that the program has definitely lost 4 bytes in 1 block. This is shown because that the memory allocated is not freed. After adding 'delete x' statement at the end of the program, we will get this message using valgrind.

All heap blocks were freed -- no leaks are possible

Lets use the variable x[15] from the given array. It is not possible to use this variable from the array! I will write the following statement in the program.

x[15] = 34;

Now, valgrind shows the following statements:

==3608== Invalid write of size 4
==3608== at 0x80485C2: main (prog.cpp:7)
==3608== Address 0x4330064 is not stack'd, malloc'd or (recently) free'd

It means the line number 7 of prog.cpp has “invalid write of size 4 bytes”! We will easily identify that the memory is used in some wrong way in the program. Remember it is not a syntactical mistake!

Now when we use the uninitialized data in the program such as,

if (x[1]==1)
    x[1] = 0;

As the array is not initialized, the x[1] won't contain any value in it. When you compile the program and analyze it by valgrind, you will get the following output.

==3661== Invalid read of size 4
==3661== at 0x80485C2: main (prog.cpp:7)
==3661== Address 0x433002c is 0 bytes after a block of size 4 alloc'd
==3661== at 0x402B733: operator new(unsigned int) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==3661== by 0x80485B0: main (prog.cpp:6)

This is the error for uninitialized data in the program. You will identify that the statement on line number 6 has some uninitialized data variable in it.
I think using valgrind improves the quality of software development. A software developer must use it in order to solve the memory leakage problems. It has many features, you may use 'man valgrind' to see all these features of it.
Enjoy good programming!

Sunday, February 3, 2013

Logical Volume Management In Ubuntu



Logical Volume Management (LVM) is a disk management option that every major Linux distribution includes. Whether you need to set up storage pools or just need to dynamically create partitions, LVM is probably what you are looking for.

                      
                                         Fig1: Logical Volume Management
 What is LVM?

Logical Volume Manager allows for a layer of abstraction between your operating system and the disks/partitions it uses. In traditional disk management your operating system looks for what disks are available (/dev/sda, /dev/sdb, etc.) and then looks at what partitions are available on those disks (/dev/sda1, /dev/sda2, etc.).



Fig2: LV  Manager

With LVM, disks and partitions can be abstracted to contain multiple disks and partitions into one device. Your operating systems will never know the difference because LVM will only show the OS the volume groups (disks) and logical volumes (partitions) that you have set up.
Because volume groups and logical volumes aren’t physically tied to a hard drive, it makes it easy to dynamically resize and create new disks and partitions. In addition, LVM can give you features that your file system is not capable of doing. For example, Ext3 does not have support for live snapshots, but if you’re using LVM you have the ability to take a snapshot of your logical volumes without unmounting the disk.

When To Use LVM?

The first thing your should consider before setting up LVM is what you want to accomplish with your disks and partitions. Some distributions, like Fedora, install with LVM by default.
If you are using Ubuntu on a laptop with only one internal hard drive and you don’t need extended features like live snapshots, then you may not need LVM. If you need easy expansion or want to combine multiple hard drives into a single pool of storage then LVM may be what you have been looking for.

Setting up LVM in Ubuntu

First thing to know about using LVM is there is no easy way to convert your existing traditional partitions to logical volumes. It is possible to move to a new partition that uses LVM, but that won’t be something that we will cover in this article; instead we are going to take the approach of setting up LVM on a fresh installation of Ubuntu 11.10.To install Ubuntu using LVM you need to use the alternate install CD. Download it from the link below and burn a CD or use unetbootin to create a USB drive.

 Fig3: Ubuntu Edition Screen

Boot your computer from the alternate install disk and select your options up until the partition disks screen and select guided – use entire disk and set up LVM.

 Fig4: Partition Disks 

Select the main disk you want to use, typically your largest drive, and then go to the next step.

 Fig5: Disk to Partition

You will immediately need to write the changes to disk so make sure you selected the right disk and then write the changes.

 Fig6: Changes Screen

Select the size you want the first logical volume to be and then continue.

 Fig7: Partitioning Disks

Confirm your disk partitions and continue with the installation.
 Fig8: Changes Screen

The final step will write the GRUB boot loader to the hard drive. It is important to note that GRUB cannot be on an LVM partition because computer BIOS’s cannot directly read from a logical volume. Ubuntu will automatically create a 255 MB ext2 partition for your boot loader.

  Fig9: GBL Installation Permission Screen

After the installation is complete, reboot the machine and boot into Ubuntu as normal. There should be no perceivable difference between using LVM or traditional disk management with this type of installation.


Fig10: Final Completion Screen

This is how the LVM is installed on UBUNTU Linux.