Skip to content

[Perf] Fix memory gauge crash using task_info (#16121)#16220

Open
JesusRojass wants to merge 10 commits into
firebase:mainfrom
JesusRojass:JesusRojass/#16121
Open

[Perf] Fix memory gauge crash using task_info (#16121)#16220
JesusRojass wants to merge 10 commits into
firebase:mainfrom
JesusRojass:JesusRojass/#16121

Conversation

@JesusRojass

@JesusRojass JesusRojass commented Jun 1, 2026

Copy link
Copy Markdown
Member

FPRMemoryGaugeCollector samples app memory on a timer. On iOS 17 and later
devices running the XZone Malloc (XZM) allocator, the malloc zone statistics
call crashes inside xzm_statistics_self while taking a non-reentrant allocator
lock, killing the process with EXC_BAD_ACCESS / EXC_BREAKPOINT.

12.10.0 (#15595) replaced mstats() with malloc_zone_statistics() to address
#15501. However mstats() is a thin wrapper around
malloc_zone_statistics(NULL, &s), so the crashing call path was unchanged and
reports kept coming in (#16121).

This change collects memory with task_info(mach_task_self(), TASK_VM_INFO, ...)
and reads phys_footprint instead. task_info is a kernel RPC that does not
touch libmalloc and takes no userspace allocator lock, so it cannot reach
xzm_statistics_self. This removes the crash. It also matches the Mach API
approach already used by FPRCPUGaugeCollector.

Behavior note: phys_footprint is the per-process physical memory footprint
(the value Jetsam accounting and the Xcode memory gauge use). It is not a
heap-only statistic; it includes heap memory along with other dirty and
compressed pages. heapUsed now carries phys_footprint, and heapAvailable
is reported as 0 because phys_footprint reports only used memory. The backend
field free_app_heap_memory_kb will therefore be 0.

Fixes #16121

Testing

  • Existing FPRMemoryGaugeCollector unit tests pass.
  • Validated on a physical device running iOS 17+.

API Changes

  • No API Changes

@gemini-code-assist

Copy link
Copy Markdown
Contributor
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

@JesusRojass

Copy link
Copy Markdown
Member Author

Work in progress!!!

@JesusRojass JesusRojass changed the title [Perf] Fix memory gauge crash using task_info (#16121) WIP! [Perf] Fix memory gauge crash using task_info (#16121) Jun 1, 2026
@JesusRojass

Copy link
Copy Markdown
Member Author

/gemini review

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request replaces the use of malloc_zone_statistics with task_info(TASK_VM_INFO) in FPRMemoryGaugeCollector to prevent crashes on devices using the XZM allocator. It also updates the corresponding documentation, tests, and changelog. The review feedback suggests initializing the vmInfo struct to zero to avoid reading uninitialized stack memory and verifying that the returned count is at least TASK_VM_INFO_REV1_COUNT before accessing phys_footprint to ensure it was populated by the kernel.

Comment thread FirebasePerformance/Sources/Gauges/Memory/FPRMemoryGaugeCollector.m Outdated
…tor.m

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@JesusRojass

Copy link
Copy Markdown
Member Author

/gemini review

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request replaces the use of malloc_zone_statistics with task_info(TASK_VM_INFO) in FPRMemoryGaugeCollector to prevent crashes on devices using the XZM allocator. It also updates the associated documentation and adds unit tests to verify the new memory collection behavior. The review feedback suggests simplifying the memory collection logic by removing the unused freeBytes variable and improving the debug logging to distinguish between a failed task_info call and an insufficient returned data count.

Comment thread FirebasePerformance/Sources/Gauges/Memory/FPRMemoryGaugeCollector.m Outdated
@JesusRojass JesusRojass changed the title WIP! [Perf] Fix memory gauge crash using task_info (#16121) [Perf] Fix memory gauge crash using task_info (#16121) Jun 3, 2026
@JesusRojass JesusRojass self-assigned this Jun 3, 2026
@JesusRojass JesusRojass marked this pull request as ready for review June 3, 2026 23:21
@JesusRojass

Copy link
Copy Markdown
Member Author

Ready for review @tejasd

@JesusRojass JesusRojass requested a review from tejasd June 3, 2026 23:22
@JesusRojass JesusRojass requested a review from mghaznav June 15, 2026 15:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Crash in [FPRMemoryGaugeCollector collectMetric]

1 participant