Skip to content

Instantly share code, notes, and snippets.

@snaury
Created May 18, 2017 21:12
Show Gist options
  • Save snaury/8d0a5ad49512e9a5add22411742ce8be to your computer and use it in GitHub Desktop.
Save snaury/8d0a5ad49512e9a5add22411742ce8be to your computer and use it in GitHub Desktop.
Trying to measure coreclr gc pauses under extreme pressure
using System;
using System.Threading;
using System.Diagnostics;
using System.Collections.Generic;
namespace hello_memory
{
class MyObject
{
public int value;
public MyObject(int value)
{
this.value = value;
}
}
class Program
{
static void WatchPauses()
{
long[] buffer = new long[10000];
double msPerTick = 1000.0 / Stopwatch.Frequency;
long minTicks = Stopwatch.Frequency / 10000; // 0.1ms
for (;;)
{
long lastTimestamp = Stopwatch.GetTimestamp();
for (int i = 0; i < buffer.Length; ++i)
{
long ts;
do
{
ts = Stopwatch.GetTimestamp();
} while (ts - lastTimestamp < minTicks);
buffer[i] = ts - lastTimestamp;
lastTimestamp = ts;
}
Array.Sort(buffer);
Console.WriteLine("Pauses: min={0}ms p50={1}ms p99={2}ms p99.9={3}ms max={4}ms",
(buffer[0] - minTicks) * msPerTick,
(buffer[5000-1] - minTicks) * msPerTick,
(buffer[9900-1] - minTicks) * msPerTick,
(buffer[9990-1] - minTicks) * msPerTick,
(buffer[10000-1] - minTicks) * msPerTick);
}
}
public static bool DoAllocations = true;
public static bool DoInnerSleep = false;
public static IList<MyObject> L;
static void Main(string[] args)
{
var thread = new Thread(WatchPauses);
thread.IsBackground = true;
thread.Start();
if (DoAllocations)
{
L = new List<MyObject>();
for (int i = 0; i < 100_000_000; ++i)
{
if (DoInnerSleep && i % 10000 == 0)
{
Thread.Sleep(1);
}
L.Add(new MyObject(i));
}
int sum = 0;
for (int i = 0; i < 100_000_000; ++i)
{
sum += L[i].value;
}
Console.WriteLine("All done! {0} items total, sum = {1}", L.Count, sum);
Thread.Sleep(1000);
Console.WriteLine("Memory before gc = {0}", GC.GetTotalMemory(false));
GC.Collect();
Console.WriteLine("Memory after gc = {0}", GC.GetTotalMemory(false));
L.Clear();
Console.WriteLine("Memory after clear = {0}", GC.GetTotalMemory(true));
L = null;
Console.WriteLine("Memory after free = {0}", GC.GetTotalMemory(true));
}
else
{
Thread.Sleep(10000);
}
Console.WriteLine("Sleeping for 5 seconds...");
Thread.Sleep(5000);
}
}
}
$ uname -mrs
Darwin 16.6.0 x86_64
$ dotnet run -c Release
Pauses: min=0ms p50=1.9E-05ms p99=3.911576ms p99.9=12.861823ms max=102.592996ms
Pauses: min=0ms p50=1.9E-05ms p99=0.016168ms p99.9=11.640945ms max=23.27792ms
Pauses: min=0ms p50=1.8E-05ms p99=0.095015ms p99.9=11.369459ms max=479.434985ms
All done! 100000000 items total, sum = 887459712
Pauses: min=0ms p50=1.8E-05ms p99=0.025847ms p99.9=10.579593ms max=27.44908ms
Memory before gc = 4010769264
Memory after gc = 3473889984
Pauses: min=0ms p50=5E-06ms p99=0.000642ms p99.9=0.028353ms max=1030.0839ms
Memory after clear = 1073881840
Memory after free = 139952
Sleeping for 5 seconds...
Pauses: min=0ms p50=5E-06ms p99=5.7E-05ms p99.9=0.014859ms max=1132.418091ms
Pauses: min=0ms p50=5E-06ms p99=5.2E-05ms p99.9=0.01256ms max=1.875761ms
Pauses: min=0ms p50=5E-06ms p99=0.000836ms p99.9=0.015358ms max=1.979399ms
Pauses: min=0ms p50=5E-06ms p99=0.000135ms p99.9=0.015897ms max=1.891292ms
Pauses: min=0ms p50=5E-06ms p99=5.9E-05ms p99.9=0.013745ms max=1.947827ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment