In a 'C' program,when I am trying to allocate memory with the help of malloc () function, it is allocating the memory up to a certain limit for e.g. in my case, it is 670 MB (approx). malloc() returns NULL if I allocate more than this amount of memory.When I tried
to allocate memory in chunks of 200MB, it failed when I exceed 670 MB.While I have sufficient amount of memory available (78 GB).

Actually I am getting CDRs from mediation for each subscriber and I have to allocate memory for each subscriber. There is a routine written inside the code where there is a structure containing Customer’s id, contract id, cell number, Activation Date and Deactivation Date in memory. So to avoid this issue, I hardcoded the value of one member of the structure.But this is not a permanent solution. So please help me to figure out how to increase the amount of memory allocated by malloc().Your valuable feedback appreciated.

os is 64 bit.
getconf KERNEL_BITS ==>> 64

If malloc() is setting errno to ENOMEM, try calling getrlimit() to get the specified limit of the RLIMIT_DATA resource.

#include <sys/resource.h>
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    struct rlimit rlim;

    if (getrlimit(RLIMIT_DATA, &rlim) == -1) {
        fputs("getrlimit() failed\n", stderr);
        return EXIT_FAILURE;
    }

    printf("Soft Limit: %lu\n", (unsigned long)rlim.rlim_cur);
    printf("Hard Limit: %lu\n", (unsigned long)rlim.rlim_max);

    return EXIT_SUCCESS;
}

If the limit is too low and you have permission to increase it, setrlimit() will do that for you.

Using a 64-bit compiler instead of a 32-bit compiler may also make malloc() allocate more memory. Just because you have a 64-bit computer doesn't mean the compiler is going to produce 64-bit code. If you are using gcc or g++ then I presume it has flags to enable 64-bit compiles.

This article has been dead for over six months. Start a new discussion instead.