optimize process start

This commit is contained in:
armv9 2025-09-04 12:25:37 +09:00
parent cddcd84ca6
commit a3e529cb19
2 changed files with 29 additions and 29 deletions

View File

@ -54,10 +54,21 @@ class SagerNet : Application(),
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
System.setProperty(DEBUG_PROPERTY_NAME, DEBUG_PROPERTY_VALUE_ON)
Thread.setDefaultUncaughtExceptionHandler(CrashHandler) Thread.setDefaultUncaughtExceptionHandler(CrashHandler)
if (isMainProcess || isBgProcess) { if (isMainProcess || isBgProcess) {
externalAssets.mkdirs()
Seq.setContext(this)
Libcore.initCore(
process,
cacheDir.absolutePath + "/",
filesDir.absolutePath + "/",
externalAssets.absolutePath + "/",
DataStore.logBufSize,
DataStore.logLevel > 0,
nativeInterface, nativeInterface, LocalResolverImpl
)
// fix multi process issue in Android 9+ // fix multi process issue in Android 9+
JavaUtil.handleWebviewDir(this) JavaUtil.handleWebviewDir(this)
@ -67,21 +78,6 @@ class SagerNet : Application(),
} }
} }
Seq.setContext(this)
updateNotificationChannels()
// nb4a: init core
externalAssets.mkdirs()
Libcore.initCore(
process,
cacheDir.absolutePath + "/",
filesDir.absolutePath + "/",
externalAssets.absolutePath + "/",
DataStore.logBufSize,
DataStore.logLevel > 0,
nativeInterface, nativeInterface, LocalResolverImpl
)
if (isMainProcess) { if (isMainProcess) {
Theme.apply(this) Theme.apply(this)
Theme.applyNightTheme() Theme.applyNightTheme()
@ -89,17 +85,22 @@ class SagerNet : Application(),
DefaultNetworkListener.start(this) { DefaultNetworkListener.start(this) {
underlyingNetwork = it underlyingNetwork = it
} }
updateNotificationChannels()
} }
} }
if (BuildConfig.DEBUG) StrictMode.setVmPolicy( if (BuildConfig.DEBUG) {
StrictMode.VmPolicy.Builder() System.setProperty(DEBUG_PROPERTY_NAME, DEBUG_PROPERTY_VALUE_ON)
.detectLeakedSqlLiteObjects() StrictMode.setVmPolicy(
.detectLeakedClosableObjects() StrictMode.VmPolicy.Builder()
.detectLeakedRegistrationObjects() .detectLeakedSqlLiteObjects()
.penaltyLog() .detectLeakedClosableObjects()
.build() .detectLeakedRegistrationObjects()
) .penaltyLog()
.build()
)
}
} }
override fun onConfigurationChanged(newConfig: Configuration) { override fun onConfigurationChanged(newConfig: Configuration) {

View File

@ -5,7 +5,7 @@ import (
"libcore/device" "libcore/device"
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime/debug"
"strings" "strings"
_ "unsafe" _ "unsafe"
@ -29,7 +29,7 @@ func NekoLogClear() {
} }
func ForceGc() { func ForceGc() {
go runtime.GC() go debug.FreeOSMemory()
} }
func InitCore(process, cachePath, internalAssets, externalAssets string, func InitCore(process, cachePath, internalAssets, externalAssets string,
@ -52,6 +52,8 @@ func InitCore(process, cachePath, internalAssets, externalAssets string,
// sing-box fs // sing-box fs
resourcePaths = append(resourcePaths, externalAssets) resourcePaths = append(resourcePaths, externalAssets)
externalAssetsPath = externalAssets
internalAssetsPath = internalAssets
// Set up log // Set up log
if maxLogSizeKb < 50 { if maxLogSizeKb < 50 {
@ -69,9 +71,6 @@ func InitCore(process, cachePath, internalAssets, externalAssets string,
defer device.DeferPanicToError("InitCore-go", func(err error) { log.Println(err) }) defer device.DeferPanicToError("InitCore-go", func(err error) { log.Println(err) })
device.GoDebug(process) device.GoDebug(process)
externalAssetsPath = externalAssets
internalAssetsPath = internalAssets
// certs // certs
pem, err := os.ReadFile(externalAssetsPath + "ca.pem") pem, err := os.ReadFile(externalAssetsPath + "ca.pem")
if err == nil { if err == nil {