Skip to content

Populate PrivateMemorySize64 on macOS#131244

Open
nwoolls wants to merge 12 commits into
dotnet:mainfrom
nwoolls:105665-macos-privatememorysize
Open

Populate PrivateMemorySize64 on macOS#131244
nwoolls wants to merge 12 commits into
dotnet:mainfrom
nwoolls:105665-macos-privatememorysize

Conversation

@nwoolls

@nwoolls nwoolls commented Jul 22, 2026

Copy link
Copy Markdown

Process.PrivateMemorySize64 always returned 0 on macOS because proc_taskallinfo (via proc_pidinfo/PROC_PIDTASKALLINFO) only exposes virtual and resident memory size; it has no private memory field.

Populate it instead from proc_pid_rusage(RUSAGE_INFO_V3).ri_phys_footprint, the same value Activity Monitor's Memory column reports. Verified this call succeeds cross-process for processes owned by the current user (including sandboxed apps) without requiring task_for_pid()/root, and fails cleanly with EPERM for processes owned by other users, matching the existing permission behavior of proc_pidinfo already used in this file.

Fix #105665

Process.PrivateMemorySize64 always returned 0 on macOS because
proc_taskallinfo (via proc_pidinfo/PROC_PIDTASKALLINFO) only exposes
virtual and resident memory size; it has no private memory field.

Populate it instead from proc_pid_rusage(RUSAGE_INFO_V4).ri_phys_footprint,
the same value Activity Monitor's Memory column reports. Verified this
call succeeds cross-process for processes owned by the current user
(including sandboxed apps) without requiring task_for_pid()/root, and
fails cleanly with EPERM for processes owned by other users, matching
the existing permission behavior of proc_pidinfo already used in this
file.

Fix dotnet#105665
Copilot AI review requested due to automatic review settings July 22, 2026 23:54
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Jul 22, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-diagnostics-process
See info in area-owners.md if you want to be subscribed.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the macOS implementation of System.Diagnostics.Process to populate ProcessInfo.PrivateBytes (and thus Process.PrivateMemorySize64) using proc_pid_rusage’s physical footprint value, and updates tests to assert the value is now non-zero on macOS.

Changes:

  • Add a new macOS libproc interop helper to retrieve physical footprint via proc_pid_rusage.
  • Populate ProcessInfo.PrivateBytes in ProcessManager.OSX.CreateProcessInfo.
  • Update ProcessTests to assert PrivateMemorySize64 / PrivateMemorySize are now > 0 on macOS.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems Adds the new interop source to CoreLib build items on OSX/MacCatalyst.
src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs Updates assertions for private memory size to be non-zero (including on macOS).
src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.OSX.cs Sets ProcessInfo.PrivateBytes from the new physical-footprint interop call.
src/libraries/System.Diagnostics.Process/src/System.Diagnostics.Process.csproj Includes the new interop file for the Process library build on OSX/MacCatalyst.
src/libraries/Common/src/Interop/OSX/Interop.libproc.GetProcessPhysicalFootprint.cs Introduces new proc_pid_rusage/rusage_info_v4 interop and a helper to get ri_phys_footprint.

Comment thread src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems Outdated
nwoolls added 2 commits July 22, 2026 20:39
Address review feedback: Interop.libproc.cs already declared
rusage_info_v3 (with ri_phys_footprint) and a proc_pid_rusage
P/Invoke. The previous commit introduced a separate rusage_info_v4
struct and a second proc_pid_rusage binding for the same native
symbol, risking a struct layout/size mismatch between the two.

v4 only appends fields after ri_serviced_system_time; the field
this fix needs, ri_phys_footprint, is already at the same offset
in v3. Remove the v4 struct and duplicate binding entirely and
add TryGetProcessPhysicalFootprint, a small non-throwing wrapper
around the existing proc_pid_rusage(V3) call, matching the
existing GetProcessInfoById's null/false-on-failure convention
rather than the existing proc_pid_rusage(int) overload's throwing
one.
Copilot AI review requested due to automatic review settings July 23, 2026 00:53

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

Comment thread src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs Outdated
Copilot AI review requested due to automatic review settings July 23, 2026 01:10

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

Comment thread src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs Outdated
Copilot AI review requested due to automatic review settings July 23, 2026 01:31

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

Comment on lines +2364 to +2366
// PrivateMemorySize is unchecked((int)PrivateMemorySize64) and can be negative on
// high-memory processes - assert the cast relationship rather than a value range,
// matching TestVirtualMemorySize.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// PrivateMemorySize is unchecked((int)PrivateMemorySize64) and can be negative on
// high-memory processes - assert the cast relationship rather than a value range,
// matching TestVirtualMemorySize.

Unnecessary comment - match style of the rest of the file

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in eea7796

Copilot AI review requested due to automatic review settings July 23, 2026 10:01

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

@adamsitnik adamsitnik left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall it LGTM, but the Windows vs Linux vs macOS differences need to be documented in XML docs, preferably using <remarks> section.

Thank you for your contribution @nwoolls !

procInfo.SessionId = sessionId;
}

// Get the process's physical memory footprint (private/unique memory usage). This can fail

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add XML docs to the public API here based on this existing doc and extend it with the description of Windows vs Linux vs macOS differences.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 4750949

CreateDefaultProcess();

AssertNonZeroAllZeroDarwin(_process.PrivateMemorySize64);
Assert.InRange(_process.PrivateMemorySize64, 1, long.MaxValue);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The existing test just checks whether the returned value is not zero:

https://github.com/nwoolls/dotnet-runtime/blob/eea779642db7911fc6a093086b8bab68cd32fd04/src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs#L82

Ensuring that it's not negative value is an improvement 👍

Another improvement we could make would be merging the TestPrivateMemorySize64 and TestPrivateMemorySize test into one, so our test suite could spawn one less process (and run slightly faster). Not a blocker, just something to consider.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 7b4d697

@adamsitnik adamsitnik added this to the 11.0.0 milestone Jul 23, 2026
nwoolls added 2 commits July 23, 2026 09:27
…rySize and consolidate assertions for PrivateMemorySize64
Copilot AI review requested due to automatic review settings July 23, 2026 13:33

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

Comment thread src/libraries/Common/src/Interop/OSX/Interop.libproc.cs
Copilot AI review requested due to automatic review settings July 23, 2026 13:57

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

Comment thread src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.cs Outdated
Copilot AI review requested due to automatic review settings July 23, 2026 14:19

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Diagnostics.Process community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

On macOS Process.PrivateMemorySize64 returns 0

4 participants