Memory usage in C ++ program reported by Gnome resource monitor: confusion

I am looking at the memory consumed by my application to make sure I am not allocating too much and am confused by what the Gnome Resource Monitor shows me. I used the following code snippets to allocate memory in two separate applications that are otherwise identical; they contain nothing but this code, and a scanf () call to pause execution while I grab onto memory usage:

malloc(1024 * 1024 * 100);

      

and

char* p = new char[1204*1024*100];

      

The following figure shows the memory usage of my application before and after each of the following lines:

Memory use

Now I've read a lot (but obviously not enough) about memory usage (including this SO question) and I'm having trouble distinguishing between writeable memory and virtual memory. According to a related question,

"Writable memory is the amount of address space your process has allocated with write access"

and

"Virtual memory is the address space that your application has allocated"

1) If I allocated memory myself, does it have write permissions?

2) The linked question also points out (regarding malloc)

"... which won't actually allocate any memory (see the disclosure at the end of the malloc (3) page for details.)"

I don't see any "rant" and my images show that the virtual memory has increased! Can someone explain this please?

3) If I only have the following code:

char* p = new char[100];

      

... the resource monitor shows memory and writeable memory increased by 8KB - the same as allocating a full megabyte! - with an increase in virtual memory by 0.1. What's going on here?

4) Which column should I look at in the resource monitor to see how much memory my application is using?

Thank you very much for your participation, and sorry if you were unclear or missed anything that could make me find the answers myself.

+3


source to share


2 answers


The memory classes reported by the Gnome Resource Monitor (and, in fact, the vast majority of resource reporting tools) are not just separate memory classes - there is overlap between them because they report different memory characteristics. Some of these various characteristics include:

  • virtual virtual - all memory in the address space of processes in modern operating systems is virtual; that the virtual address space is mapped to actual physical memory using the hardware capabilities of the CPU; how this mapping is performed is a complex topic in itself, with many differences between different architectures.
  • Memory access permissions - memory can be readable, writeable, or executable, or any combination of the three (in theory - some combinations don't really make sense and therefore can't really be resolved by hardware and / or software, but the point is that these permissions are processed separately)
  • resident and non-resident - with a virtual memory system, most of a process's address space may not actually be mapped currently in real physical memory for various reasons - it may not have been allocated yet; it can be part of a binary or one of the libraries, or even a data segment that has not yet been loaded because the program has not yet called it; it may have been swapped out to free up physical memory for another program it needs.
  • shared vs private - parts of the virtual address space of processes that are read-only (for example, the actual program code and most libraries) can be shared by other processes using the same libraries or programs - this is a great advantage for shared memory use, since having 37 different running instances xterm

    does not mean that the code for xterm

    has to be loaded 37 times into memory - all processes can share one copy of the code


Due to this and several other factors (IPC shared memory, memory mapped files, physical devices with memory mapped to hardware, etc.), determining the actual memory used by any one process, or even the whole system can be tricky.

+1


source


A more accurate way to understand Linux process memory usage is to use the proc (5) filesystem.

So if your process pid is 1234 try

cat /proc/1234/maps

      



Note that processes have address space in virtual memory . This address space can be changed by mmap (2) and other syscalls (2) . For multiple reasons malloc (3) is efficient and free

avoid too many of these system calls and prefer to reuse earlier free

-d. So when your program is free

-ing (or, in C ++, delete

-ing) a piece of memory, that piece is often marked as reusable, but is not returned back to the kernel (for example munmap

). Similarly, if you are malloc

only 100 bytes, yours is libc

resolved eg. request a whole megabyte with mmap

(on next callmalloc

e.g. 200 bytes, it will use a fraction of that magabyte)

See also http://linuxatemyram.com/ and Linux Advanced Programming (and this over-memory question )

+2


source







All Articles