PROBLEM: malloc can alloc more memory that Linux really has

From: Igor Oblakov (oblakov@x4u.lebedev.ru)
Date: Wed Jul 05 2000 - 10:05:17 EST


Hi,

1. malloc can alloc more memory that Linux really has

2. Try to start program from subsection 6.
You can test that this program can alloc more memory that really kernel
has (memory+swap).
Result of this program is unpredictable.
We saw diagnostics "Killed", "Segmentation fault", "no pages for
init/syslogd/kerneld" and so on.
We tried different releases 2.0.35, 2.5.2-22 and some others.

3. Keywords: memory allocation, paging, swapping

4. For examples:
     Linux version 2.0.35 (root@...) (gcc version 2.7.2.3) #6 Wed Sep 16
16:48:13 MSD 1998
     Linux version 2.2.14 (root@...) (gcc version egcs-2.91.66
19990314/Linux (egcs-1.1.2 release)) #15 Mon Jul 3 20:25:24 MSD 2000

5. no

6. Test program:
/*
    Malloc test
    Author: I.V. Oblakov
*/
#include <stdio.h>
#include <malloc.h>
int main(int argc, char **argv)
{
    char *p, *p1;
    int m = 0, m1;
    int size = 4096; /* we begin from request 4í */
    /* find maximum possible size for malloc */
    while(size){
        if(p=malloc((m+size)*1024)){
             m+=size; free(p);
        }else{
             size >>= 1;
        }
     }
     fprintf(stderr," Size is %dKb\n", m);
     /* divide our request, but use request many times */
     m1 = m/2+m/4;
     fprintf(stderr," New size is %dKb\n", m1*2); /* we try to use 150%
of free memory */
     /* you can do it more than 2 times !! */
     p = malloc(m1*1024); p1 = malloc(m1*1024);
     if(p!=NULL && p1 !=NULL){
         fprintf(stderr,"Begin memset\n"); /* Yes we see that kernel
give us 150% of "free" memory */
         memset(p, 0, m1*1024); /* try to use our memory */
         memset(p1, 0, m1*1024); /* you can repeat it with p2... */
     }
     return 0; /* no chance */
}

7. Different environment. Total free space must be less than malloc can
potentially return (2Gb).
8. Linux has not normal system for paging reservation.

Best regards,
Igor Oblakov

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/



This archive was generated by hypermail 2b29 : Fri Jul 07 2000 - 21:00:16 EST