Last active
September 21, 2018 12:31
-
-
Save buybackoff/f0b7c3ccb10327b4be8d4488e8714d50 to your computer and use it in GitHub Desktop.
Create memory pressure on private working set. Exit after 30 seconds of inactivity (if on RDP and server hangs)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Threading.Tasks; | |
namespace MemoryPressure | |
{ | |
internal class Program | |
{ | |
private static List<byte[]> _buffers = new List<byte[]>(); | |
private static async Task Main(string[] args) | |
{ | |
var totalAllocated = 0; | |
var rng = new Random(); | |
while (true) | |
{ | |
Console.WriteLine("Press enter to allocate 100 MB"); | |
var to = Task.Delay(3000); | |
var tr = Task.Run(async () => await Console.In.ReadLineAsync()); | |
var t = Task.WhenAny(to, tr); | |
if (t.Result == to) | |
{ | |
Environment.Exit(0); | |
} | |
var buffer = new byte[100 * 1024 * 1024]; | |
rng.NextBytes(buffer); | |
_buffers.Add(buffer); | |
totalAllocated += 100; | |
Console.WriteLine($"Allocated {totalAllocated} MB"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ReadLineAsync is blocking, need a separate task for delay to work