Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache current process object to avoid performance hit #5597

Merged
merged 4 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
evgenyfedorov2 marked this conversation as resolved.
Show resolved Hide resolved
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;

Expand Down Expand Up @@ -41,7 +42,6 @@ public ulong GetMemoryUsage()

public ulong GetCurrentProcessMemoryUsage()
{
using Process process = Process.GetCurrentProcess();
return (ulong)process.WorkingSet64;
return (ulong)Environment.WorkingSet;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ internal static long GetCpuTicks()

internal static long GetMemoryUsageInBytes()
{
using var process = Process.GetCurrentProcess();
return process.WorkingSet64;
return Environment.WorkingSet;
}

internal static ulong GetTotalMemoryInBytes()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.Extensions.Diagnostics.ResourceMonitoring.Windows.Interop;
using Microsoft.TestUtilities;
using Xunit;

namespace Microsoft.Extensions.Diagnostics.ResourceMonitoring.Windows.Test;

/// <summary>
/// Process Info Interop Tests.
/// </summary>
/// <remarks>These tests are added for coverage reasons, but the code doesn't have
/// the necessary environment predictability to really test it.</remarks>
public sealed class ProcessInfoTests
{
[ConditionalFact]
public void GetCurrentProcessMemoryUsage()
{
var workingSet64 = new ProcessInfo().GetCurrentProcessMemoryUsage();
Assert.True(workingSet64 > 0);
}
}
Loading