Allocate maximum memory available

1

I currently try to write a programm which is supposed to allocate the maximum memory available. I came to a solution which limits the area of potential available memory until both borders are equal (see listing)

void enforceMemoryLeakage(void** arrayOfAllocMemory)
{
    unsigned int maxMemory = 0x80000000;
    unsigned int minMemory = 0x50000000;
    unsigned int attempAllocatedMemory = minMemory + (maxMemory - minMemory) / 2;
    void* pAllocMemory;

    while((maxMemory - minMemory) > 1)
    {
        pAllocMemory = malloc(attempAllocatedMemory);

        if (pAllocMemory != NULL)
        {
            minMemory = attempAllocatedMemory;
            attempAllocatedMemory += (maxMemory - minMemory) / 2;
            free(pAllocMemory);
        }
        else
        {
            maxMemory = attempAllocatedMemory;
            attempAllocatedMemory = minMemory + (maxMemory - minMemory) / 2;
        }
    }
    arrayOfAllocMemory[0] = malloc(maxMemory);
    void* pAllocAdditionalMemory = malloc(100);

    if (pAllocAdditionalMemory == NULL)
        std::cout << "Maximum memory: " << minMemory << "\n";
}

The above displayed code works fine. However if the command is executed

void* pAllocAdditionalMemory = malloc(100);

if (pAllocAdditionalMemory == NULL)
    std::cout << "Maximum memory: " << minMemory << "\n";

I would have expected that there is no further memory available. However it does not work which brings me to my actual question why the above shown approach does not work.

Best regards

Ratbald

algorithm
memory
memory-leaks
malloc
asked on Stack Overflow Dec 5, 2019 by RatbaldMeyer • edited Dec 5, 2019 by Spektre

1 Answer

1

You did not specify OS nor platform so I am completely guessing here so read with extreme prejudice...

Assuming you do not have a bug in your binary search code... My bet is you face memory fragmentation problems as you are successfully allocate/free memory during execution other processes can do the same so you might fragment your memory. Example:

  1. OS has 2 MByte chunk of continuous free memory
  2. you allocate 1.5 MByte of mem (0.5 MByte free)
  3. some other process allocates 1 KByte (0.499 MByte free)
  4. you free the 1 MByte (1.0 + 0.499 MByte free two fragments)
  5. and attempt to allocate 1.25 MByte but OS does not have 1.25 memory in single continuous chunk so it failed hence you allocate the 1MByte again (0.499 MByte free)
  6. you succesffuly allocate 1 KByte (0.498 MByte free)

Depending on the OS memory management strategies you could sometimes even not need another process interfering to fragment memory ...

There are however another possibilities not related to Fragmentation. In case of emulations or WOW64 the OS will not allocate whole available RAM, also there are limits on single continuous chunk size. For example Win32 will not allow more than ~1.25 GByte but that does not mean there is only 1.25 GByte of free RAM ...

answered on Stack Overflow Dec 5, 2019 by Spektre • edited Dec 6, 2019 by Spektre

User contributions licensed under CC BY-SA 3.0