Skip to content

About

Swift package that detects memory leaks in UIViewControllers, UIViews, and SwiftUI ViewModels automatically

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

18 Commits

Folders and files

Repository files navigation

LeakSentry

Automatic memory leak detection for UIViewControllers, UIViews, and SwiftUI ViewModels.

LeakSentry demo


Installation

Add the package via Swift Package Manager:

https://github.com/iamjoaovytor/LeakSentry

Choose the module for your stack:

Module Use when
LeakSentry Core only (custom integration)
LeakSentryUIKit UIKit app
LeakSentrySwiftUI SwiftUI app

Usage

UIKit

Call once at launch — all UIViewController and UIView subclasses are monitored automatically via swizzling.

import LeakSentryUIKit

// AppDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions...) -> Bool {
    LeakSentry.startUIKit()
    return true
}

SwiftUI

  1. Adopt LeakSentinel on any ViewModel (ObservableObject or @Observable):
import LeakSentrySwiftUI

class ProfileViewModel: ObservableObject, LeakSentinel { ... }

// or with @Observable (iOS 17+)
@Observable
class ProfileViewModel: LeakSentinel { ... }
  1. Add .trackLeaks(for:) to the View that owns it:
ProfileView()
    .trackLeaks(for: viewModel)

Detection triggers automatically when the view disappears.


Configuration

LeakSentry.startUIKit(
    configuration: Configuration(
        detectionDelay: 1.5,              // seconds after disappear to check (default: 1.5)
        reporters: [OSLogReporter(),      // where to send leak reports
                    ConsoleReporter()],
        isEnabled: true,                  // set false in production if needed
        ignoredClassNames: ["MyBaseVC"]   // additional classes to skip
    )
)

Reporters

Reporter Module Output
OSLogReporter Core os.log (default)
ConsoleReporter Core Formatted print to console
AssertionReporter Core assertionFailure in DEBUG
AlertReporter UIKit UIAlertController popup in DEBUG

Custom reporter:

struct MyReporter: LeakReporter {
    func report(_ leak: LeakReport) {
        // send to Crashlytics, Sentry, Datadog, etc.
    }
    func resolved(_ leak: LeakReport) { }
}

Requirements

  • iOS 15+ / tvOS 15+ / macOS 12+ / visionOS 1+
  • Swift 5.9+

License

MIT

About

Swift package that detects memory leaks in UIViewControllers, UIViews, and SwiftUI ViewModels automatically

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages