Skip to content

Instantly share code, notes, and snippets.

@jsoffer
Created March 7, 2012 15:27
Show Gist options
  • Save jsoffer/1993795 to your computer and use it in GitHub Desktop.
Save jsoffer/1993795 to your computer and use it in GitHub Desktop.
Distinguiendo memoria virtual reservada de memoria actualmente usada
/* reservando memoria, sin usarla */
#include <unistd.h>
#include <stdlib.h>
#define PAGESIZE 4096
#define SIZE 1212 * PAGESIZE
int main() {
int i;
void * chunk = malloc(SIZE);
sleep(60);
free(chunk);
return(0);
}
/* pmap; observar que en RSS (uso de memoria física) no aparece 'chunk' */
/*
Address Kbytes RSS Shared Priv Mode Mapped File
08048000 4 4 - 4 r-x /usr/home/jaime/C/testmem
08049000 3804 4 - 3804 rw- [ anon ]
28049000 200 92 200 - r-x /libexec/ld-elf.so.1
2807B000 8 8 - 8 rw- /libexec/ld-elf.so.1
2807D000 76 48 - 76 rw- [ anon ]
28090000 1032 384 1032 - r-x /lib/libc.so.7
28192000 24 24 - 24 rw- /lib/libc.so.7
28198000 160 48 - 160 rw- [ anon ]
28400000 8192 0 - 8192 rw- [ anon ]
BFBE0000 128 12 - 128 rwx [ anon ]
-------- ------- ------- ------- -------
Total Kb 13628 624 1232 12396
*/
/* reservando y usando esa memoria; el cast a (char *) garantiza
* que cada escritura sea de un byte
*/
int main() {
int i;
void * chunk = malloc(SIZE);
for(i = 0; i < SIZE; ++i){
*((char *)chunk + i) = 'x';
}
sleep(60);
free(chunk);
return(0);
}
/* pmap: observar los 4848 Kbytes (1212 * 4096) en RSS en [ anon ] */
/*
Address Kbytes RSS Shared Priv Mode Mapped File
08048000 4 4 - 4 r-x /usr/home/jaime/C/testmem
08049000 3804 4 - 3804 rw- [ anon ]
28049000 200 92 200 - r-x /libexec/ld-elf.so.1
2807B000 8 8 - 8 rw- /libexec/ld-elf.so.1
2807D000 76 48 - 76 rw- [ anon ]
28090000 1032 388 1032 - r-x /lib/libc.so.7
28192000 24 24 - 24 rw- /lib/libc.so.7
28198000 160 48 - 160 rw- [ anon ]
28400000 8192 4848 - 8192 rw- [ anon ]
BFBE0000 128 12 - 128 rwx [ anon ]
-------- ------- ------- ------- -------
Total Kb 13628 5476 1232 12396
*/
/* Notación:
* Kbytes es el total de memoria virtual asignada al segmento
* RSS es la memoria que está en realidad en memoria "física"
* Shared es memoria compartida con otros procesos
* Priv es la memoria que solamente se utiliza en este proceso
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment