#define _GNU_SOURCE #include #include #include int startsWith (const char* s1, const char* s2) { while ((*s1 != 0) && (*s2 != 0) && (*s1 == *s2)) { ++s1; ++s2; } return (*s2 == 0); } void printMemInfo (void) { char* filename; FILE* status; char buffer[128]; asprintf (&filename, "/proc/%d/status", getpid()); status = fopen (filename, "r"); free (filename); while (fgets (buffer, sizeof (buffer), status) != NULL) if (startsWith (buffer, "VmSize")) { printf ("%s", buffer); break; } fclose (status); } void** allocate (int n) { void** result = NULL; int i; for (i = 0; i < n; ++i) { void** next = malloc (sizeof (void**)); *next = result; result = next; } return result; } void release (void** list) { while (list != NULL) { void** next = *list; free (list); list = next; } } int main (int argc, char** argv) { void** list; printf ("At the beginning:\n"); printMemInfo(); list = allocate (1024*1024); printf ("After allocating the memory:\n"); printMemInfo(); release (list); printf ("After releasing the memory:\n"); printMemInfo(); return 0; }