Footprint

1.0.2

Footprint is a Swift library that facilitates dynamic memory management in iOS apps, categorizing memory states and allowing adaptive behavior based on real-time constraints.
naftaly/Footprint

What's New

v1.0.2

2024-04-03T20:10:56Z

Full Changelog: 1.0.1...v1.0.2

Footprint

License Swift Platform

Overview

Footprint is a Swift library that helps manage and monitor memory usage in your app. It provides a flexible approach to handling memory levels, allowing you to adapt your app's behavior based on the available memory and potential termination risks.

Key Features

  • Memory State Management: Footprint categorizes memory states into normal, warning, critical, and terminal, providing insights into your app's proximity to termination due to memory constraints.

  • Dynamic Memory Handling: Change your app's behavior dynamically based on the current memory state. For instance, adjust cache sizes or optimize resource usage to enhance performance.

  • SwiftUI Integration: Easily observe and respond to changes in the app's memory state within SwiftUI views using the onFootprintMemoryStateDidChange modifier.

Installation

Add the Footprint library to your project:

  1. In Xcode, with your app project open, navigate to File > Add Packages.
  2. When prompted, add the Firebase Apple platforms SDK repository:
https://github.com/naftaly/Footprint

Usage

Initialization

Initialize Footprint as early as possible in your app's lifecycle:

let _ = Footprint.shared

Memory State Observation

Respond to changes in memory state using the provided notification:

NotificationCenter.default.addObserver(forName: Footprint.stateDidChangeNotification, object: nil, queue: nil) { notification in
    if let newState = notification.userInfo?[Footprint.newMemoryStateKey] as? Footprint.Memory.State,
       let oldState = notification.userInfo?[Footprint.oldMemoryStateKey] as? Footprint.Memory.State {
        print("Memory state changed from \(oldState) to \(newState)")
        // Perform actions based on the memory state change
    }
}

SwiftUI Integration

Use the SwiftUI extension to observe changes in memory state within your views:

Text("Hello, World!")
    .onFootprintMemoryStateDidChange { newState, oldState in
        print("Memory state changed from \(oldState) to \(newState)")
        // Perform actions based on the memory state change
    }

Memory Information Retrieval

Retrieve current memory information:

let currentMemory = footprint.memory
print("Used Memory: \(currentMemory.used) bytes")
print("Remaining Memory: \(currentMemory.remaining) bytes")
print("Memory Limit: \(currentMemory.limit) bytes")
print("Memory State: \(currentMemory.state)")

Memory Allocation Check

Check if a certain amount of memory can be allocated:

let canAllocate = footprint.canAllocate(bytes: 1024)
print("Can allocate 1KB: \(canAllocate)")

License

Footprint is available under the MIT license. See the LICENSE file for more info.

Description

  • Swift Tools 5.9.0
View More Packages from this Author

Dependencies

  • None
Last updated: Fri Apr 12 2024 03:35:43 GMT-0900 (Hawaii-Aleutian Daylight Time)