Skip to content

Commit

Permalink
Merge pull request #4950 from andydotxyz/fix/iosmemoryfree
Browse files Browse the repository at this point in the history
Fix crashes on low memory iOS devices
  • Loading branch information
andydotxyz authored Jun 19, 2024
2 parents a17e7b9 + 6857054 commit 510bb87
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
5 changes: 1 addition & 4 deletions internal/driver/mobile/app/android.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ import (
"log"
"mime"
"os"
"runtime"
"runtime/debug"
"strings"
"time"
"unsafe"
Expand Down Expand Up @@ -278,8 +276,7 @@ func onConfigurationChanged(activity *C.ANativeActivity) {

//export onLowMemory
func onLowMemory(activity *C.ANativeActivity) {
runtime.GC()
debug.FreeOSMemory()
cleanCaches()
}

var (
Expand Down
5 changes: 5 additions & 0 deletions internal/driver/mobile/app/darwin_ios.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,11 @@ func lifecycleVisible() { theApp.sendLifecycle(lifecycle.StageVisible) }
//export lifecycleFocused
func lifecycleFocused() { theApp.sendLifecycle(lifecycle.StageFocused) }

//export lifecycleMemoryWarning
func lifecycleMemoryWarning() {
cleanCaches()
}

//export drawloop
func drawloop() {
runtime.LockOSThread()
Expand Down
4 changes: 4 additions & 0 deletions internal/driver/mobile/app/darwin_ios.m
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ - (void)applicationWillTerminate:(UIApplication *)application {
lifecycleDead();
}

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
lifecycleMemoryWarning();
}

- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray <NSURL *>*)urls {
if ([urls count] == 0) {
return;
Expand Down
15 changes: 15 additions & 0 deletions internal/driver/mobile/app/mobile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//go:build ios || android

package app

import "C"
import (
"runtime"
"runtime/debug"
)

// clean caches - called when the OS wants some memory back
func cleanCaches() {
runtime.GC()
debug.FreeOSMemory()
}

0 comments on commit 510bb87

Please sign in to comment.