Skip to content

Commit

Permalink
More aggressive GC, regular logging memory usage to diagnose memory l…
Browse files Browse the repository at this point in the history
…eaks
  • Loading branch information
jaredbroad committed Feb 25, 2015
1 parent 5be77cb commit fd218fd
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions Common/Isolator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,27 +84,41 @@ public static void ResetCancelToken()
public static bool ExecuteWithTimeLimit(TimeSpan timeSpan, Action codeBlock, long memoryCap = 1024)
{
var message = "";
var dtEnd = DateTime.Now + timeSpan;
var end = DateTime.Now + timeSpan;
var memoryLogger = DateTime.Now + TimeSpan.FromMinutes(1);

//Convert to bytes
memoryCap *= 1024 * 1024;

ResetCancelToken();

//Thread:
var task = Task.Factory.StartNew(codeBlock, cancelToken);
var task = Task.Factory.StartNew(codeBlock, cancelToken);

while (!task.IsCompleted && DateTime.Now < dtEnd)
while (!task.IsCompleted && DateTime.Now < end)
{
if (GC.GetTotalMemory(false) > memoryCap)
var memoryUsed = GC.GetTotalMemory(false);

if (memoryUsed > memoryCap)
{
if (GC.GetTotalMemory(true) > memoryCap)
{
message = "Execution Security Error: Memory Usage Maxed Out - " + Math.Round(Convert.ToDouble(memoryCap / (1024 * 1024))) + "MB max.";
break;
}
}
Thread.Sleep(1000);

if (DateTime.Now > memoryLogger)
{
if (memoryUsed > (memoryCap * 0.8))
{
memoryUsed = GC.GetTotalMemory(true);
Log.Error("Execution Security Error: Memory usage over 80% capacity.");
}
Console.WriteLine(DateTime.Now.ToString("u") + " Isolator.ExecuteWithTimeLimit(): Used: " + Math.Round(Convert.ToDouble(memoryUsed / (1024 * 1024))));
memoryLogger = DateTime.Now.AddMinutes(1);
}
Thread.Sleep(100);
}

if (task.IsCompleted == false && message == "")
Expand All @@ -121,6 +135,5 @@ public static bool ExecuteWithTimeLimit(TimeSpan timeSpan, Action codeBlock, lon
}
return task.IsCompleted;
}

}
}

0 comments on commit fd218fd

Please sign in to comment.