Get maximum available heap memory

0

I'm currently trying to figure out the maximum memory that is able to be allocated through the malloc() command in C. Until now I´ve tried a simple algorithm that increments a counter that will subsequently be allocated. If the malloc command returns "NULL" I know, that there is not enough memory available.

        ULONG ulMaxSize = 0;

        for (ULONG ulSize = /*0x40036FF0*/ 0x40A00000; ulSize <= 0xffffffff; ulSize++)
        {
            void* pBuffer = malloc(ulSize);
            if (pBuffer == NULL)
            {
                ulMaxSize = ulSize - 1;
                break;
            }
            free(pBuffer);
        }

        void* pMaxBuffer = malloc(ulMaxSize);

However, this algorithm gets executed very long since the malloc() command has turned out to be a time consuming task.

My question is now, if there is a more efficient algorithm to find the maximum memory able to be allocated?

c
malloc
dynamic-memory-allocation
asked on Stack Overflow Dec 3, 2019 by RatbaldMeyer • edited Dec 3, 2019 by Lundin

2 Answers

1

The maximum memory that can be allocated depends mostly on few factors:

  • Address space limits on the process (max memory, virtual memory and friends).
  • Virtual space available
  • Physical space available
  • Fragmentation, which will limit the size of continuous memory blocks.
  • ... Other limits ...

From your description (extreme slowness) looks like the process start using swap, which is VERY slow vs. real memory.

Consider the following alternative

  • For address space limit, look at ulimit -a (or use getrlimit to access the same data from C program) - look for 'max memory size', and 'virtual memory'
  • For swap space, physical memory - top
ulimit -a (filtered)

data seg size           (kbytes, -d) unlimited
max memory size         (kbytes, -m) 2048
stack size              (kbytes, -s) 8192
virtual memory          (kbytes, -v) unlimited

From a practical point, given that a program does not have control over system resources, you should be focused on 'max memory size'.

answered on Stack Overflow Dec 3, 2019 by dash-o
1

Other than using OS specific API to get such number :

You can also do a binary search, not recommended, as the state of the system might be in flux and the result could vary over time:

ULONG getMax() {
    ULONG min = 0x0;
    ULONG max = 0xffffffff;
    void* t = malloc(max);
    if(t!=NULL) {
        free(t);
        return max;
    }
    while(max-min > 1) {
        ULONG mid = min + (max - min) / 2;
        t = malloc(mid);
        if(t == NULL) {
            max = mid;
            continue;
        }
        free(t);
        min = mid;
    }
    return min;
}
answered on Stack Overflow Dec 3, 2019 by dvhh

User contributions licensed under CC BY-SA 3.0