Skip to content

Commit a3e529c

Browse files
committed
optimize process start
1 parent cddcd84 commit a3e529c

File tree

2 files changed

+29
-29
lines changed

2 files changed

+29
-29
lines changed

app/src/main/java/io/nekohasekai/sagernet/SagerNet.kt

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,21 @@ class SagerNet : Application(),
5454
override fun onCreate() {
5555
super.onCreate()
5656

57-
System.setProperty(DEBUG_PROPERTY_NAME, DEBUG_PROPERTY_VALUE_ON)
5857
Thread.setDefaultUncaughtExceptionHandler(CrashHandler)
5958

6059
if (isMainProcess || isBgProcess) {
60+
externalAssets.mkdirs()
61+
Seq.setContext(this)
62+
Libcore.initCore(
63+
process,
64+
cacheDir.absolutePath + "/",
65+
filesDir.absolutePath + "/",
66+
externalAssets.absolutePath + "/",
67+
DataStore.logBufSize,
68+
DataStore.logLevel > 0,
69+
nativeInterface, nativeInterface, LocalResolverImpl
70+
)
71+
6172
// fix multi process issue in Android 9+
6273
JavaUtil.handleWebviewDir(this)
6374

@@ -67,39 +78,29 @@ class SagerNet : Application(),
6778
}
6879
}
6980

70-
Seq.setContext(this)
71-
updateNotificationChannels()
72-
73-
// nb4a: init core
74-
externalAssets.mkdirs()
75-
Libcore.initCore(
76-
process,
77-
cacheDir.absolutePath + "/",
78-
filesDir.absolutePath + "/",
79-
externalAssets.absolutePath + "/",
80-
DataStore.logBufSize,
81-
DataStore.logLevel > 0,
82-
nativeInterface, nativeInterface, LocalResolverImpl
83-
)
84-
8581
if (isMainProcess) {
8682
Theme.apply(this)
8783
Theme.applyNightTheme()
8884
runOnDefaultDispatcher {
8985
DefaultNetworkListener.start(this) {
9086
underlyingNetwork = it
9187
}
88+
89+
updateNotificationChannels()
9290
}
9391
}
9492

95-
if (BuildConfig.DEBUG) StrictMode.setVmPolicy(
96-
StrictMode.VmPolicy.Builder()
97-
.detectLeakedSqlLiteObjects()
98-
.detectLeakedClosableObjects()
99-
.detectLeakedRegistrationObjects()
100-
.penaltyLog()
101-
.build()
102-
)
93+
if (BuildConfig.DEBUG) {
94+
System.setProperty(DEBUG_PROPERTY_NAME, DEBUG_PROPERTY_VALUE_ON)
95+
StrictMode.setVmPolicy(
96+
StrictMode.VmPolicy.Builder()
97+
.detectLeakedSqlLiteObjects()
98+
.detectLeakedClosableObjects()
99+
.detectLeakedRegistrationObjects()
100+
.penaltyLog()
101+
.build()
102+
)
103+
}
103104
}
104105

105106
override fun onConfigurationChanged(newConfig: Configuration) {

libcore/nb4a.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"libcore/device"
66
"os"
77
"path/filepath"
8-
"runtime"
8+
"runtime/debug"
99
"strings"
1010
_ "unsafe"
1111

@@ -29,7 +29,7 @@ func NekoLogClear() {
2929
}
3030

3131
func ForceGc() {
32-
go runtime.GC()
32+
go debug.FreeOSMemory()
3333
}
3434

3535
func InitCore(process, cachePath, internalAssets, externalAssets string,
@@ -52,6 +52,8 @@ func InitCore(process, cachePath, internalAssets, externalAssets string,
5252

5353
// sing-box fs
5454
resourcePaths = append(resourcePaths, externalAssets)
55+
externalAssetsPath = externalAssets
56+
internalAssetsPath = internalAssets
5557

5658
// Set up log
5759
if maxLogSizeKb < 50 {
@@ -69,9 +71,6 @@ func InitCore(process, cachePath, internalAssets, externalAssets string,
6971
defer device.DeferPanicToError("InitCore-go", func(err error) { log.Println(err) })
7072
device.GoDebug(process)
7173

72-
externalAssetsPath = externalAssets
73-
internalAssetsPath = internalAssets
74-
7574
// certs
7675
pem, err := os.ReadFile(externalAssetsPath + "ca.pem")
7776
if err == nil {

0 commit comments

Comments
 (0)