Skip to content

Commit

Permalink
Try reducing memory pressure during object writing (#83181)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalStrehovsky authored Mar 9, 2023
1 parent 97de75b commit 201aae9
Showing 1 changed file with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1150,14 +1150,30 @@ public static void EmitObject(string objectFilePath, IReadOnlyCollection<Depende
}

if (logger.IsVerbose)
logger.LogMessage($"Finalizing output to '{objectFilePath}'...");
logger.LogMessage($"Emitting debug information");

// Native side of the object writer is going to do more native memory allocations.
// Free up as much memory as possible so that we don't get OOM killed.
// This is potentially a waste of time. We're about to end the process and let the
// OS "garbage collect" the entire address space.
var gcMemoryInfo = GC.GetGCMemoryInfo();
if (gcMemoryInfo.TotalCommittedBytes > gcMemoryInfo.TotalAvailableMemoryBytes / 2)
{
if (logger.IsVerbose)
logger.LogMessage($"Freeing up memory");

GC.Collect(GC.MaxGeneration, GCCollectionMode.Aggressive);
}

objectWriter.EmitDebugModuleInfo();

succeeded = true;
}
finally
{
if (logger.IsVerbose)
logger.LogMessage($"Finalizing output to '{objectFilePath}'...");

objectWriter.Dispose();

if (!succeeded)
Expand All @@ -1173,6 +1189,9 @@ public static void EmitObject(string objectFilePath, IReadOnlyCollection<Depende
}
}
}

if (logger.IsVerbose)
logger.LogMessage($"Done writing object file");
}

[DllImport(NativeObjectWriterFileName)]
Expand Down

0 comments on commit 201aae9

Please sign in to comment.