Skip to content
Open
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
3 changes: 0 additions & 3 deletions FirebasePerformance/Sources/FPRNanoPbUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}
}];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#import "FirebasePerformance/Sources/Configurations/FPRConfigurations.h"
#import "FirebasePerformance/Sources/FPRConsoleLogger.h"

#include <malloc/malloc.h>
#include <mach/mach.h>

@interface FPRMemoryGaugeCollector ()

Expand All @@ -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;
}

Expand Down
10 changes: 6 additions & 4 deletions FirebasePerformance/Sources/Gauges/Memory/FPRMemoryGaugeData.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down