Pascal Bolzhauser
2004-07-02 14:25:52 UTC
Hi all,
running the following small test program on different unices to test
malloc's out-of-memory error handling with this result:
FreeBSD 3.4-RELEASE - 512 MB malloc: Cannot allocate memory
SunOS 5.7 sparc - 1124 MB malloc: Resource temporarily unavailable
AIX 1 5 - 128 MB malloc: Not enough space
HP-UX B.11.00 9000/785- 1015 MB malloc: Not enough space
Linux 2.2.17 i686 - 588 MB malloc: Cannot allocate memory
Linux 2.4.19 x86_64 - 1958 MB Killed
Linux 2.4.17 i686 - 1802 MB Killed
Linux 2.4.17-mckinley-smp ia64 - 2015 MB Killed
-- snip ----
#include <stdlib.h>
#include <stdio.h>
#define MEG (1024*1024)
int main(int argc, const char* argv[]) {
int chunksize = 128*1024; /* 128 kB chunks */
unsigned long sum = 0; /* allocated memory */
printf("Allocating memory in chunks a %dkB\n", chunksize/1024);
for(;;) {
char* p;
int i;
sum += chunksize;
if (sum % MEG == 0) {
fprintf(stdout, "\r%9lu MB ", sum/MEG);
fflush(stdout);
}
p = malloc(chunksize);
if(!p) {
perror("malloc");
return 1;
}
/* touch the allocated memory space */
for (i=0; i<chunksize; i+= 1024) p[i] = ' ';
};
fprintf(stdout, "\nDone.\n");
return 0;
}
-- snap ----
On newer Linux systems a call to malloc() only returns NULL if there are
no more addresses available. Since older Linux systems and other unices
returns NULL if there is no more physical memory availabel.
Does anyone know why this behavior has changed in Linux?
If my test program tries to access the memory address returned from
malloc it receives a kill signal (which can't be handled).
Any (simple) ideas how to realize that there is no more physical memory
availabel, so my program can terminate with an out-of-memory error
instead of being killed?
Thanks,
Pascal
running the following small test program on different unices to test
malloc's out-of-memory error handling with this result:
FreeBSD 3.4-RELEASE - 512 MB malloc: Cannot allocate memory
SunOS 5.7 sparc - 1124 MB malloc: Resource temporarily unavailable
AIX 1 5 - 128 MB malloc: Not enough space
HP-UX B.11.00 9000/785- 1015 MB malloc: Not enough space
Linux 2.2.17 i686 - 588 MB malloc: Cannot allocate memory
Linux 2.4.19 x86_64 - 1958 MB Killed
Linux 2.4.17 i686 - 1802 MB Killed
Linux 2.4.17-mckinley-smp ia64 - 2015 MB Killed
-- snip ----
#include <stdlib.h>
#include <stdio.h>
#define MEG (1024*1024)
int main(int argc, const char* argv[]) {
int chunksize = 128*1024; /* 128 kB chunks */
unsigned long sum = 0; /* allocated memory */
printf("Allocating memory in chunks a %dkB\n", chunksize/1024);
for(;;) {
char* p;
int i;
sum += chunksize;
if (sum % MEG == 0) {
fprintf(stdout, "\r%9lu MB ", sum/MEG);
fflush(stdout);
}
p = malloc(chunksize);
if(!p) {
perror("malloc");
return 1;
}
/* touch the allocated memory space */
for (i=0; i<chunksize; i+= 1024) p[i] = ' ';
};
fprintf(stdout, "\nDone.\n");
return 0;
}
-- snap ----
On newer Linux systems a call to malloc() only returns NULL if there are
no more addresses available. Since older Linux systems and other unices
returns NULL if there is no more physical memory availabel.
Does anyone know why this behavior has changed in Linux?
If my test program tries to access the memory address returned from
malloc it receives a kill signal (which can't be handled).
Any (simple) ideas how to realize that there is no more physical memory
availabel, so my program can terminate with an out-of-memory error
instead of being killed?
Thanks,
Pascal