[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: malloc problem
> if you malloc a certain amount of memory, sometimes, the
> malloc succeeds,
> but the process is killed:
> Out of Memory: Killed process 163 (testm).
> for example, a program such as this:
> main(int argc, char *argv)
> int *dataptr;
> unsigned long i, n;
> n = atoi(argv);
> n = n*n;
> printf ("allocating %ld ints\n", n);
> dataptr = (int *)malloc(sizeof(int) * n);
> if (!dataptr)
> printf ("malloc failed\n");
> printf ("malloc succeeded\n");
> for (i = 0; i < n; i++)
> dataptr[i] = i;
> does anyknow know why this happens and how to prevent this?
You only have so much memory to allocate and when you haven't
got any more the kernel will kill some process to free some
(I've heard of some kernel setting that doesn't kill on malloc
but waits until the memory is actually used).
I'm not sure about how the 2.4 kernel handles fragmentation,
but I think that if you fragment the memory you will increase
the possibility that a large malloc will fail.