Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cancellable stored for every executed action #4

Closed
robinmayerhofer opened this issue Jul 27, 2020 · 0 comments · Fixed by #6
Closed

Cancellable stored for every executed action #4

robinmayerhofer opened this issue Jul 27, 2020 · 0 comments · Fixed by #6
Labels
enhancement New feature or request

Comments

@robinmayerhofer
Copy link

Everytime an action is executed, a new AnyCancellable is stored in the cancellables of the Reactor. This can lead to evergrowing memory usage. (10k actions lead to ~3.6 MB increased memory usage), see here

For reference: ReactorKit creates the state stream only once and uses a custom observable type that takes actions.

The ReactorKit approach would solve the described issue for the SwiftUIReactor framework.

Example test showing increasing memory consumption:

import XCTest
import Combine
@testable import SwiftUIReactor

final class TestMemoryConsumption: XCTestCase {
    func testMemoryConsumption() {
        let reactor = CountingReactor()

        var cancellables = Set<AnyCancellable>()

        print("MB after", getMegabytesUsed())

        let amount = 10000
        for _ in 0..<amount {
            Just(CountingReactor.Action.countUp)
                .sink(receiveValue: reactor.action)
                .store(in: &cancellables)
        }

        cancellables = Set()
        sleep(3)
        print("MB after", getMegabytesUsed())
    }
    func mach_task_self() -> task_t {
        return mach_task_self_
    }

    func getMegabytesUsed() -> Float {
        var info = mach_task_basic_info()
        var count = mach_msg_type_number_t(MemoryLayout.size(ofValue: info) / MemoryLayout<integer_t>.size)
        let kerr = withUnsafeMutablePointer(to: &info) { infoPtr in
            return infoPtr.withMemoryRebound(to: integer_t.self, capacity: Int(count)) { (machPtr: UnsafeMutablePointer<integer_t>) in
                return task_info(
                    mach_task_self(),
                    task_flavor_t(MACH_TASK_BASIC_INFO),
                    machPtr,
                    &count
                )
            }
        }
        guard kerr == KERN_SUCCESS else {
            fatalError()
        }
        return Float(info.resident_size) / (1024 * 1024)
    }
}

final class CountingReactor: Reactor {

    enum Action {
        case countUp
    }

    enum Mutation {
        case countUp
    }

    struct State {
        var currentCount: Int
    }

    var cancellables: Set<AnyCancellable> = []

    var state: State = State(currentCount: 0)

    func mutate(action: Action) -> AnyPublisher<Mutation, Never> {
        return Just(Mutation.countUp).eraseToAnyPublisher()
    }

    func reduce(mutation: Mutation) {
        var newState = state

        switch mutation {
        case .countUp:
            newState.currentCount += 1
        }

        state = newState
    }

}

The code for getMegabytesUsed() is taken from the Apple Developer Forums.

@julianpomper julianpomper added the enhancement New feature or request label Jul 27, 2020
@julianpomper julianpomper mentioned this issue Aug 1, 2020
@julianpomper julianpomper linked a pull request Aug 1, 2020 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants