Skip to content

Instantly share code, notes, and snippets.

@buybackoff
Last active September 21, 2018 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save buybackoff/f0b7c3ccb10327b4be8d4488e8714d50 to your computer and use it in GitHub Desktop.
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)
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");
}
}
}
}
@buybackoff
Copy link
Author

ReadLineAsync is blocking, need a separate task for delay to work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment