Add managed memory allocations profiling with dotnet7

This commit is contained in:
Wojciech Figat
2023-01-06 09:18:11 +01:00
parent 1e046c0033
commit a6050e6f27
3 changed files with 21 additions and 1 deletions

View File

@@ -24,6 +24,8 @@ namespace FlaxEditor.Windows.Profiler
private int _frameIndex = -1;
private int _framesCount;
private bool _showOnlyLastUpdateEvents = true;
private long _lastManagedMemory = 0;
private long _lastManagedMemoryProfiler = 0;
/// <summary>
/// Gets or sets a value indicating whether live events recording is enabled.
@@ -143,6 +145,7 @@ namespace FlaxEditor.Windows.Profiler
{
_frameIndex = -1;
_framesCount = 0;
_lastManagedMemory = 0;
for (int i = 0; i < _tabs.ChildrenCount; i++)
{
if (_tabs.Children[i] is ProfilerMode mode)
@@ -218,8 +221,16 @@ namespace FlaxEditor.Windows.Profiler
{
FlaxEngine.Profiler.BeginEvent("ProfilerWindow.OnUpdate");
// Get memory allocations during last frame
long managedMemory = GC.GetAllocatedBytesForCurrentThread();
if (_lastManagedMemory == 0)
_lastManagedMemory = managedMemory;
var managedAllocs = managedMemory - _lastManagedMemory - _lastManagedMemoryProfiler;
_lastManagedMemory = managedMemory;
ProfilerMode.SharedUpdateData sharedData = new ProfilerMode.SharedUpdateData();
sharedData.Begin();
sharedData.ManagedMemoryAllocation = (int)managedAllocs;
for (int i = 0; i < _tabs.ChildrenCount; i++)
{
if (_tabs.Children[i] is ProfilerMode mode)
@@ -242,6 +253,10 @@ namespace FlaxEditor.Windows.Profiler
_framesCount = Mathf.Min(_framesCount + 1, ProfilerMode.MaxSamples);
UpdateButtons();
// Get memory allocations within profiler window update to exclude from stats
managedMemory = GC.GetAllocatedBytesForCurrentThread();
_lastManagedMemoryProfiler = managedMemory - _lastManagedMemory;
FlaxEngine.Profiler.EndEvent();
}
}