How to exhaust all available dynamic memory in computer?

Hello
Last edited on
First of all most OSes I'm aware of don't distinguish between memory allocated to code space, stack space, or memory dynamically allocated by programs. So what you're
really asking is how to determine the amount of free memory on a machine. Computing free memory, at least in most OSes I'm aware of, is rather black magic. As an example, both linux and windows will report a (relatively) small amount of free memory when asked. However, it doesn't mean that that is all the memory available; if needed, both OSes will start swapping pages to disk, reducing the amount of kernel buffers preallocated (but not used), reduce the size of the page cache, etc. The result is that you'll get a lot more memory than what you were told was free.




Edit: Nevermind.
Last edited on
True. Well, I am trying to write C++ code to determine all the available dynamic memory on the computer. It does not matter if it is memory allocated to code space, stack space, etc. If Windows or Linux reports a small amount of memory, that is fine.
Write a program with a great memory leak (like, calling with new but no delete in a loop) and check when it fails to allocate some; just kidding :))

Though not a bad idea just to check where it fails, lol :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <cstdio>
#include <climits>
int main (void)
{
  for (unsigned int i = 1; i < UINT_MAX - 1; ++i)
    {
      char** x = new char[i];
      for (unsigned int j = 1; j < UINT_MAX - 1; ++j)
        {
          printf("%d\t%d\n", i, j);
          x[i] = new char[j];
        }
      printf("%d\t%d\n", i, UINT_MAX);
      x[i] = new char[UINT_MAX];
    }
  for (unsigned int j = 1; j < UINT_MAX - 1; ++j)
    {
      printf("%d\t%d\n", UINT_MAX, j);
      char* x = new char[j];
    }
  printf("%d\t%d\n", UINT_MAX, UINT_MAX);
  char* x = new char[UINT_MAX];
}


If your computer can get through all that and not be super slow, wow. That creates UINT_MAX*UINT_MAX byte arrays, if I am reading my own fabricated code correctly. :P

I don't recommend running this in actuality, but I figure the code is there if you /really/ wanted to do it... Also, if you redirect the output to a file (if you aren't using an IDE, in other words) or if you modify it to print to a file instead of stdout, you can view where it stops. That was the goal with this program. ^_^
Last edited on
It's not just enough to allocate the memory. You have to write to it also. For example, linux will gleefully allow you to allocate up to ~4GB of memory even if your machine doesn't have that much virtual memory (it stops there on a 32-bit machine because that will exhaust your process' virtual address space).

I'd say to allocate 1 page at a time and write to any byte of that page. That will force the kernel to backfill the allocation with real memory pages.

However, depending on the OS you're running on, that won't even work. For instance, when linux boxes run out of memory, the kernel will invoke the out of memory killer (OOM Killer) which will go and start killing running processes to free up additional memory).


Topic archived. No new replies allowed.