Commit 920acfa4 authored by Daniel Wolf's avatar Daniel Wolf
Browse files

Clear some memory when the system is running low

Closes #209
parent 67c4ec29
......@@ -3,10 +3,7 @@ package com.frostnerd.smokescreen.service
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.Service
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.content.*
import android.content.pm.PackageManager
import android.net.*
import android.os.*
......@@ -98,6 +95,8 @@ class DnsVpnService : VpnService(), Runnable {
private var lastVPNStopTime:Long? = null
private val coroutineScope:CoroutineContext = SupervisorJob()
private var queryCount = 0
private var dnsCache:SimpleDnsCache? = null
private var localResolver:LocalResolver? = null
private val addressResolveScope:CoroutineScope by lazy {
CoroutineScope(newSingleThreadContext("service-resolve-retry"))
}
......@@ -525,6 +524,15 @@ class DnsVpnService : VpnService(), Runnable {
return if (destroyed) Service.START_NOT_STICKY else Service.START_STICKY
}
override fun onTrimMemory(level: Int) {
if(level >= ComponentCallbacks2.TRIM_MEMORY_BACKGROUND ||
level == ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL ||
level == ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW) {
dnsCache?.clear()
localResolver?.cleanup()
}
}
private fun setServerConfiguration(intent: Intent?) {
log("Updating server configuration..")
userServerConfig = BackgroundVpnConfigureActivity.readServerInfoFromIntent(intent)
......@@ -1020,12 +1028,15 @@ class DnsVpnService : VpnService(), Runnable {
}
log("Creating DNS proxy with ${1 + handles.size} handles")
dnsCache = createDnsCache()
localResolver = createLocalResolver()
dnsProxy = SmokeProxy(
defaultHandle!!,
handles + createProxyBypassHandlers(),
createDnsCache(),
dnsCache,
createQueryLogger(),
createLocalResolver()
localResolver
)
log("DnsProxy created, creating VPN proxy")
vpnProxy = RetryingVPNTunnelProxy(dnsProxy!!, vpnService = this, coroutineScope = CoroutineScope(
......
......@@ -236,7 +236,13 @@ class DnsRuleResolver(context: Context) : LocalResolver(false) {
} ?: throw IllegalStateException()
}
override fun cleanup() {}
override fun cleanup() {
cachedWildcardWhitelisted.clear()
cachedNonWildcardWhitelisted.clear()
cachedResolved.clear()
cachedWildcardResolved.clear()
cachedNonIncluded.clear()
}
// Handle CNAME Cloaking
// Does not need to handle whitelist as the query has already been forwarded
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment