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
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,18 @@ @interface FPRMemoryGaugeCollector ()
FPRMemoryGaugeData *fprCollectMemoryMetric(void) {
NSDate *collectionTime = [NSDate date];

struct mstats ms = mstats();
FPRMemoryGaugeData *gaugeData = [[FPRMemoryGaugeData alloc] initWithCollectionTime:collectionTime
heapUsed:ms.bytes_used
heapAvailable:ms.bytes_free];
// Use malloc_zone_statistics to get heap memory usage.
// Passing nil aggregates statistics from all malloc zones.
malloc_statistics_t stats;
malloc_zone_statistics(nil, &stats);
uint64_t usedBytes = stats.size_in_use;
uint64_t totalHeapBytes = stats.size_allocated;
uint64_t freeInsideHeap = totalHeapBytes - usedBytes;

FPRMemoryGaugeData *gaugeData =
[[FPRMemoryGaugeData alloc] initWithCollectionTime:collectionTime
heapUsed:usedBytes
heapAvailable:freeInsideHeap];
return gaugeData;
}

Expand Down
12 changes: 8 additions & 4 deletions FirebasePerformance/Sources/Gauges/Memory/FPRMemoryGaugeData.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ 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 within allocated heap space (size_allocated - size_in_use).
*/
@property(nonatomic, readonly) u_long heapAvailable;

- (instancetype)init NS_UNAVAILABLE;
Expand All @@ -36,8 +39,9 @@ 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 Free memory within allocated heap space (size_allocated - size_in_use).
* @return Instance of memory gauge data.
*/
- (instancetype)initWithCollectionTime:(NSDate *)collectionTime
Expand Down