How to fill memory as fast as possible in C #

The interviewer just asked me a special question that I had not thought of before.

"How could you fill up your computer in C # as quickly as possible?

I replied that I would probably use some kind of recursive function, however, he pointed out that I was probably getting a stack overflow before filling the memory.

My question is simple: how can I fill up the computer memory with C # as quickly as possible?

+7
memory-management c #
source share
4 answers

I would go with a bomb bomb:

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

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

+8
source share
  • Fork-Bomb, this will ultimately make the processor very busy, but not necessarily fill the memory. If you have GB of memory and a small program, the Windows MMU may eventually replace the unused (previous plugs) with the disk and still keep the memory free for another program. The only problem is that it does not fill the memory, but simply makes the system immune.

  • Virtual memory, allocating huge objects using Marshal.AllocHGlobal or similar functions, you might think that you are filling the memory, but again, but the OS is smarter if you just allocate memory and do not use it for reading again, the OS will return them again to disk, still not taking up all the memory. This is still virtual memory, and the OS will allow you to get the MAX memory specified by the .net directives, then it will start throwing more memory without consuming all the memory.

  • Physical memory. Now it is difficult, first of all, you cannot access physical memory in Windows under normal circumstances in any application. If you really want to fill memory (physical memory), you need to write a kernel-mode driver to do this.

  • AllocateUserPhysicalPages . This is the only Windows API that allows you to allocate physical memory (which in turn speeds up memory), which makes it inaccessible to other processes. https://msdn.microsoft.com/en-us/library/aa366528(VS.85).aspx SQL Server uses this, and I believe that even other databases will use it to pre-allocate physical memory, this memory is faster and is mainly used for caching purpose.

+2
source share

I have not tried, but I would go with something like:

 while(true) { Marshal.AllocHGlobal(1024); } 
+1
source share

Create a bunch of objects and avoid garbage collection.

call kill () and enjoy.

 void kill() { while(true) ThreadPool.QueueUserWorkItem(fill)); } void fill(Object o) { List<Object> list = new List<Object>(); while(true) list.Add(new Object()); } 
+1
source share

All Articles