How to fill memory as fast as possible in c#

I'd go with a fork-bomb:

while (true) Process.Start(Assembly.GetExecutingAssembly().Location);

The concept is familiar, the program endlessly starts new instances of itself.


I haven't tried it, but I'd go with something like:

while(true) { Marshal.AllocHGlobal(1024); }

  1. Fork-Bomb, this will eventually make CPU very busy, but not necessarily fill the memory. If you have GBs of memory and a small program, Windows MMU might eventually swap not used (previous forks) to disk and still keep memory free for other program. The only problem is, this does not fill memory instead it simply makes system unresponsive.

  2. Virtual Memory, by allocating huge objects using Marshal.AllocHGlobal or similar functions, you may think you are filling memory, but once again, but OS is smarter, if you are just allocating memory and not using them to read again, OS will once again page them back to disk, still not occupying all memory. This is still virtual memory and OS will allow you to MAX memory given by .net guidelines, then it will start throwing no more memory without actually consuming all of memory.

  3. Physical Memory, Now this is tricky, first of all, you cannot access physical memory in Windows under normal circumstances in any application. If you really want to fill in the memory (Physical memory) then you have to write a kernel mode driver to do it.

  4. AllocateUserPhysicalPages function. This is the only Windows API which lets you allocate physical memory, (which in a way fills in memory faster) making it unavailable for other processes. https://msdn.microsoft.com/en-us/library/aa366528(VS.85).aspx SQL Server uses this and I believe even other databases would be using it to pre allocate physical memory, this memory is faster and mainly used for caching purpose.