ABothe wrote:
> So there isn't any other method than calling C's free(); or malloc
> (); ?
Note that there is no guarantee that C's malloc/free will return
the memory to the OS either (*). The only way to be sure that the
memory will be returned is to call mmap/munmap (or their equivalent
on whatever OS you're using).
(*) Most malloc implementations allocate large chunks of memory
using mmap, which can return the memory to the OS, but they allocate
small chunks with sbrk, which can't. As proof, see the attached C
code which prints:
At the beginning:
VmSize: 3872 kB
After allocating the memory:
VmSize: 36608 kB
After releasing the memory:
VmSize: 36608 kB
Jerome
--
mailto:[email protected]
http://jeberger.free.fr
Jabber: [email protected]
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
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;
}
signature.asc
Description: OpenPGP digital signature
