Skip to content

Commit aeddeba

Browse files
authored
Benchmarks: fix OOM crash on a real iOS device. Part 2. (#5211)
- add memory warning monitor to do explicit GC ## Release Notes N/A
1 parent cedd48f commit aeddeba

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

benchmarks/multiplatform/benchmarks/src/commonMain/kotlin/MeasureComposable.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,5 +124,6 @@ suspend fun measureComposable(
124124
)
125125
} finally {
126126
scene.close()
127+
runGC() // cleanup for next benchmarks
127128
}
128129
}

benchmarks/multiplatform/benchmarks/src/iosMain/kotlin/main.ios.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file.
44
*/
55

6-
import kotlinx.coroutines.runBlocking
6+
import kotlinx.coroutines.MainScope
7+
import kotlinx.coroutines.launch
78

89
fun main(args : List<String>) {
910
Args.parseArgs(args.toTypedArray())
10-
runBlocking { runBenchmarks(graphicsContext = graphicsContext()) }
11+
MainScope().launch {
12+
runBenchmarks(graphicsContext = graphicsContext())
13+
println("Completed!")
14+
}
1115
}

benchmarks/multiplatform/iosApp/iosApp/iOSApp.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,28 @@
11
import SwiftUI
22
import benchmarks
3+
import Combine
4+
5+
class MemoryWarningMonitor: ObservableObject {
6+
private var cancellable: AnyCancellable?
7+
8+
init() {
9+
self.cancellable = NotificationCenter.default
10+
.publisher(for: UIApplication.didReceiveMemoryWarningNotification)
11+
.sink { _ in
12+
self.didReceiveMemoryWarning()
13+
}
14+
}
15+
16+
private func didReceiveMemoryWarning() {
17+
// print("Memory warning received! Cleaning resources.")
18+
RunGC_nativeKt.runGC()
19+
}
20+
}
321

422
@main
523
struct iOSApp: App {
624
init() {
25+
MemoryWarningMonitor()
726
Main_iosKt.main(args: ProcessInfo.processInfo.arguments)
827
}
928

0 commit comments

Comments
 (0)