diff --git a/FirebasePerformance/Sources/FPRNanoPbUtils.m b/FirebasePerformance/Sources/FPRNanoPbUtils.m index 2f02fd3d15b..57b8b0b248c 100644 --- a/FirebasePerformance/Sources/FPRNanoPbUtils.m +++ b/FirebasePerformance/Sources/FPRNanoPbUtils.m @@ -363,9 +363,6 @@ firebase_perf_v1_GaugeMetric FPRGetGaugeMetric(NSArray *gaugeData, NSString *ses memoryReadings[memoryReadingsCount].used_app_heap_memory_kb = (int32_t)BYTES_TO_KB(gaugeData.heapUsed); memoryReadings[memoryReadingsCount].has_used_app_heap_memory_kb = true; - memoryReadings[memoryReadingsCount].free_app_heap_memory_kb = - (int32_t)BYTES_TO_KB(gaugeData.heapAvailable); - memoryReadings[memoryReadingsCount].has_free_app_heap_memory_kb = true; memoryReadingsCount++; } }]; diff --git a/FirebasePerformance/Sources/Gauges/Memory/FPRMemoryGaugeCollector.m b/FirebasePerformance/Sources/Gauges/Memory/FPRMemoryGaugeCollector.m index 7c4104d79f1..f3016f0eeee 100644 --- a/FirebasePerformance/Sources/Gauges/Memory/FPRMemoryGaugeCollector.m +++ b/FirebasePerformance/Sources/Gauges/Memory/FPRMemoryGaugeCollector.m @@ -19,7 +19,7 @@ #import "FirebasePerformance/Sources/Configurations/FPRConfigurations.h" #import "FirebasePerformance/Sources/FPRConsoleLogger.h" -#include +#include @interface FPRMemoryGaugeCollector () @@ -37,10 +37,22 @@ @interface FPRMemoryGaugeCollector () FPRMemoryGaugeData *fprCollectMemoryMetric(void) { NSDate *collectionTime = [NSDate date]; - struct mstats ms = mstats(); + // Using task_info(TASK_VM_INFO) avoids allocator-introspection (mstats/malloc_zone_statistics), + // which could crash under heavy allocation contention. it's safe to call from any thread. + task_vm_info_data_t vmInfo; + mach_msg_type_number_t count = TASK_VM_INFO_COUNT; + uint64_t usedBytes = 0; + + kern_return_t kr = task_info(mach_task_self(), TASK_VM_INFO, (task_info_t)&vmInfo, &count); + if (kr == KERN_SUCCESS) { + // phys_footprint is the memory footprint used by Jetsam accounting. + usedBytes = (u_long)vmInfo.phys_footprint; + } else { + FPRLogDebug(kFPRMemoryCollection, @"task_info() failed with error: %d", kr); + } FPRMemoryGaugeData *gaugeData = [[FPRMemoryGaugeData alloc] initWithCollectionTime:collectionTime - heapUsed:ms.bytes_used - heapAvailable:ms.bytes_free]; + heapUsed:usedBytes + heapAvailable:0]; return gaugeData; } diff --git a/FirebasePerformance/Sources/Gauges/Memory/FPRMemoryGaugeData.h b/FirebasePerformance/Sources/Gauges/Memory/FPRMemoryGaugeData.h index 747a4aac254..9143aba9d29 100644 --- a/FirebasePerformance/Sources/Gauges/Memory/FPRMemoryGaugeData.h +++ b/FirebasePerformance/Sources/Gauges/Memory/FPRMemoryGaugeData.h @@ -24,10 +24,12 @@ NS_ASSUME_NONNULL_BEGIN /** @brief Time at which memory data was measured. */ @property(nonatomic, readonly) NSDate *collectionTime; -/** @brief Heap memory that is used. */ +/** @brief Physical memory footprint of the application, measured via task_info phys_footprint. + * Note: This represents the total memory used by the process (as reported by Jetsam accounting), + * not just heap allocations. It includes heap, stack, memory-mapped files, and other resources. */ @property(nonatomic, readonly) u_long heapUsed; -/** @brief Heap memory that is available. */ +/** @brief Heap memory that is available. Always 0 as this metric is not available via task_info. */ @property(nonatomic, readonly) u_long heapAvailable; - (instancetype)init NS_UNAVAILABLE; @@ -36,8 +38,8 @@ NS_ASSUME_NONNULL_BEGIN * Creates an instance of memory gauge data with the provided information. * * @param collectionTime Time at which the gauge data was collected. - * @param heapUsed Heap memory that is used. - * @param heapAvailable Heap memory that is available. + * @param heapUsed Physical memory footprint of the application (measured via task_info phys_footprint). + * @param heapAvailable Heap memory that is available. Always 0 as this metric is not available. * @return Instance of memory gauge data. */ - (instancetype)initWithCollectionTime:(NSDate *)collectionTime