Search code examples
cmemorygmp

Cannot allocate memory when using GMP library


I need to compute three two-dimensional arrays. When I tried to initialize the array using GMP library. I got the following error:

GNU MP: Cannot allocate memory (size=16)
Aborted (core dumped)

My memory is 8GB. t_column is about 2500. row is a variable and it can be more than 10^7. Part of the code looks like this: (The last for loop causes the error)

    mpf_t **h;
    mpf_t **t;
    mpf_t **r;
    h = (mpf_t**)malloc(sizeof(mpf_t *)*row);
    t = (mpf_t**)malloc(sizeof(mpf_t *)*row);
    r = (mpf_t**)malloc(sizeof(mpf_t *)*256);
    int i, d, j;

    for(i = 0; i < row; i++) {
        h[i] = (mpf_t *)malloc(sizeof(mpf_t)*256);
    }
    for(i = 0; i < row; i++) {
        t[i] = (mpf_t *)malloc(sizeof(mpf_t)*t_column);
    }
    for(i = 0; i < 256; i++) {
        r[i] = (mpf_t *)malloc(sizeof(mpf_t)*t_column);
    }

    for(i = 0; i < row; i++) {
        for(j = 0; j < 256; j++)  {
            mpf_init2(h[i][j], 8);
            mpf_set_str(h[i][j], "0.0", 0);
        }
    }

How can I get around this? If other libraries have better memory allocation capabilities, I am open to that.


Solution

  • For t_column=2500 and row=10^7, your program will allocate about 2883 MB memory.

    The problem has no bussness with your physics memory size. If you are using 32-bit operating system, you have 2^32=4GB memory address space. usually the kernel uses 1GB address space, so it's not strange that you cannot find continuous space in the rest 3GB and fail to malloc().