Each process running on a machine uses some memory, allocating memory on a heap or in the stack. In linux system there is a way to know how to get information about process memory usage, so that you can control how much memory a process is using and also check if there is any memory leak.

If you already know about the different way that a process has to allocate memory, basically stack or heap, you can just go at the end of the page to check wow to get information about process memory usage, skipping the next section. Otherwise, if you still have some doubts about process memory usage and you want to find out more, the next paragraoh could be useful:

STACK AND HEAP ALLOCATTION
In most modern computer systems, each thread has a reserved region of memory referred to as its stack, where data are added and remove in the last-in first-out order. When a function executes, it may add some of its state data to the top of the stack (for example automatic variables); when the function exits it is responsible for removing that data from the stack. So the lifetime of the data stored by a function on the stack and of the function itself are the same.

The heap instead is an area of memory used for dynamic memory allocation, where allocation and deallocation of memory are done in an arbitrary order. In C++, for example, when you use the new operator to allocate memory, to create an object, this memory is assigned from the heap. The objects stored in the heap survive after the funtion that created them terminates. This could be very useful, but it could also lead to memory leak problem, since is the programmer that has to take care of the deallocation of the memory.

 

 

How to get information about process memory usage:

Now that we all know something about how a process use the memory, the Unix system offer you a very simple way to monitor the memory usage of a process. The only thing that you need is know the process id (for example using the “ps aux” comman),
One you have the PID (process id), you can just check /proc/”PID”/statm, that will provide you this information:

size            total program size  (same as VmSize in /proc/[pid]/status)
resident    resident set size  (same as VmRSS in /proc/[pid]/status)
share        shared pages (from shared mappings)
text            text (code)
lib              library (unused in Linux 2.6)
data          data + stack
dt              dirty pages (unused in Linux 2.6)

If you need other information (more that memory usage) of a process, check /proc/”PID”, you’ll find more other information)
Just check man proc to get an overview of all the information that you can find on a process.