Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need C# code that will eat up system memory.

Tags:

I need the opposite of good, optimized code. For testing purposes I need a simple program to eat up RAM. Preferably not all memory so that the OS is non-functional, but more like something that would simulate a memory leak, using up a lot of memory and slowing down the OS, gradually, overtime.

Specifically, can anyone provide code spinets or links to tutorials I can use?

I saw this code as a suggestion on another post:

for (object[] o = null;; o = new[] { o }); 

But it is not quite what I am looking for as per the description above.

Please help.

like image 702
Zolt Avatar asked Dec 12 '13 18:12

Zolt


Video Answer


1 Answers

Use

Marshal.AllocHGlobal(numbytes) 

You can attach this to a timer. And just dont release the memory (dont call FreeHGlobal). Thats the most straighforward, controllable and predictable way to consume memory.

Marhsal.AllocHGlobal

like image 135
Rotem Avatar answered Sep 20 '22 10:09

Rotem