Skip to content

Instantly share code, notes, and snippets.

@Bio2hazard
Created October 20, 2019 03:41
Show Gist options
  • Save Bio2hazard/4e1dcf897a15dcbc0d79533c2f6692f9 to your computer and use it in GitHub Desktop.
Save Bio2hazard/4e1dcf897a15dcbc0d79533c2f6692f9 to your computer and use it in GitHub Desktop.
Dotnet core 2.2.7 on Alpine 3.9 Memory Leak reproduction
FROM mcr.microsoft.com/dotnet/core/sdk:2.2.402-alpine3.9 AS build
ADD . /opt/testapp
WORKDIR /opt/testapp
RUN dotnet restore && \
dotnet publish -c Release -o dist
FROM mcr.microsoft.com/dotnet/core/runtime:2.2.7-alpine3.9 as runtime
COPY --from=build /opt/testapp/dist /opt/testapp/dist
ENV ASPNETCORE_ENVIRONMENT=Production
WORKDIR /opt/testapp/dist
ENTRYPOINT ["dotnet", "Alpine39ConsoleMemory.dll"]
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
namespace Alpine39ConsoleMemory
{
class Program
{
private static readonly Uri[] _uris = {
new Uri("https://www.microsoft.com"),
new Uri("https://www.google.com"),
new Uri("https://www.youtube.com"),
new Uri("https://www.amazon.com"),
new Uri("https://www.yahoo.com"),
new Uri("https://www.reddit.com"),
new Uri("https://www.ebay.com"),
new Uri("https://www.office.com"),
new Uri("https://www.bing.com"),
new Uri("https://www.netflix.com"),
new Uri("https://www.twitter.com"),
new Uri("https://www.twitch.tv"),
new Uri("https://www.stackoverflow.com"),
new Uri("https://www.live.com"),
new Uri("https://www.msn.com"),
new Uri("https://www.imgur.com")
};
private static StreamWriter _reportingFileWriter;
static async Task Main(string[] args)
{
var reportingFilePath = Path.Combine(Path.GetTempPath(), "report.csv");
var reportingFile = new FileInfo(reportingFilePath);
_reportingFileWriter = reportingFile.CreateText();
_reportingFileWriter.AutoFlush = true;
Write("Elapsed", "WorkingSet", "GCMemory", "Gen0Collections", "Gen1Collections", "Gen2Collections", "WorkingSetSegmentMax", "WorkingSetMax");
var watch = Stopwatch.StartNew();
long workingSetSegmentMax = 0;
var lastReport = watch.Elapsed;
var index = 0;
var client = new HttpClient();
while (true)
{
try
{
workingSetSegmentMax = Math.Max(workingSetSegmentMax, Process.GetCurrentProcess().WorkingSet64);
if (watch.Elapsed.Subtract(lastReport).Minutes > 0)
{
var elapsed = watch.Elapsed;
var workingSet = BytesToString(Process.GetCurrentProcess().WorkingSet64);
var workingSetMax = BytesToString(Process.GetCurrentProcess().PeakWorkingSet64);
var gcMemory = BytesToString(GC.GetTotalMemory(false));
var gen0 = GC.CollectionCount(0).ToString();
var gen1 = GC.CollectionCount(1).ToString();
var gen2 = GC.CollectionCount(2).ToString();
Write(elapsed.ToString(@"d\.hh\:mm"), workingSet, gcMemory, gen0, gen1, gen2, BytesToString(workingSetSegmentMax), workingSetMax);
lastReport = new TimeSpan(elapsed.Days, elapsed.Minutes, 0);
workingSetSegmentMax = 0;
}
if (index >= _uris.Length)
{
index = 0;
}
var uri = _uris[index];
index++;
using (var cts = new CancellationTokenSource(5_000))
using (var pageResponse = await client.GetAsync(uri, cts.Token))
{
pageResponse.EnsureSuccessStatusCode();
}
}
catch (Exception ex)
{
Console.WriteLine("Failure:" + ex.Message);
}
await Task.Delay(1_000);
}
}
private static void Write(params string[] data)
{
_reportingFileWriter.WriteLine(string.Join(',', data));
}
private static string BytesToString(long byteCount)
{
string[] suf = { "B", "KB", "MB", "GB", "TB", "PB", "EB" }; //Longs run out around EB
if (byteCount == 0)
return "0" + suf[0];
long bytes = Math.Abs(byteCount);
int place = Convert.ToInt32(Math.Floor(Math.Log(bytes, 1024)));
double num = Math.Round(bytes / Math.Pow(1024, place), 2);
return (Math.Sign(byteCount) * num).ToString(CultureInfo.InvariantCulture) + suf[place];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment