Skip to content

Commit 50a4b9d

Browse files
committed
Adjust max memory factor at a step level
1 parent 3b75412 commit 50a4b9d

4 files changed

Lines changed: 10 additions & 5 deletions

File tree

app/src/main/kotlin/com/njlabs/showjava/decompilers/BaseDecompiler.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ abstract class BaseDecompiler(val context: Context, val data: Data) {
7474
private val disposables = CompositeDisposable()
7575
private var onLowMemory: ((Boolean) -> Unit)? = null
7676
private var memoryThresholdCrossCount = 0
77+
protected open val maxMemoryAdjustmentFactor = 1.25
7778

7879
init {
7980
@Suppress("LeakingThis")
@@ -121,21 +122,21 @@ abstract class BaseDecompiler(val context: Context, val data: Data) {
121122
.subscribeOn(Schedulers.io())
122123
.observeOn(Schedulers.io())
123124
.subscribe {
124-
val maxAdjusted = Runtime.getRuntime().maxMemory() / 1.25
125+
val maxAdjusted = Runtime.getRuntime().maxMemory() / maxMemoryAdjustmentFactor
125126
val used = (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()).toDouble().let { m ->
126127
if (m > maxAdjusted) maxAdjusted else m
127128
}
128129
val usedPercentage = (used / maxAdjusted) * 100
129130

130131
Timber.d("[mem] ----")
131132
Timber.d("[mem] Used: ${FileUtils.byteCountToDisplaySize(used.toLong())}")
132-
Timber.d("[mem] Max: ${FileUtils.byteCountToDisplaySize(maxAdjusted.toLong())}")
133+
Timber.d("[mem] Max: ${FileUtils.byteCountToDisplaySize(maxAdjusted.toLong())} at factor $maxMemoryAdjustmentFactor")
133134
Timber.d("[mem] Used %: $usedPercentage")
134135

135136
broadcastStatus("memory", "%.2f".format(usedPercentage), "memory")
136137

137138
if (usedPercentage > memoryThreshold) {
138-
if (memoryThresholdCrossCount > 2) {
139+
if (memoryThresholdCrossCount > 1) {
139140
onLowMemory?.invoke(true)
140141
} else {
141142
memoryThresholdCrossCount++

app/src/main/kotlin/com/njlabs/showjava/decompilers/JarExtractionWorker.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ class JarExtractionWorker(context: Context, data: Data) : BaseDecompiler(context
6464

6565
private var ignoredLibs: ArrayList<String> = ArrayList()
6666

67+
// Set a lower max memory than available.
68+
// Creating a dex file can easily use up memory before we get a chance to exit gracefully
69+
override val maxMemoryAdjustmentFactor = 1.5
70+
6771
/**
6872
* Load a list of library classes that are to be ignored from the assets
6973
*/

app/src/main/kotlin/com/njlabs/showjava/utils/UserPreferences.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class UserPreferences(private val prefs: SharedPreferences) {
3636
const val DARK_MODE = false
3737
const val SHOW_MEMORY_USAGE = true
3838
const val SHOW_SYSTEM_APPS = false
39-
const val MEMORY_THRESHOLD = 90
39+
const val MEMORY_THRESHOLD = 80
4040
const val IGNORE_LIBRARIES = true
4141
const val CHUNK_SIZE = 500
4242
const val MAX_ATTEMPTS = 2

app/src/main/res/xml/preferences.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
android:inputType="number"
9494
android:key="memoryThreshold"
9595
android:numeric="integer"
96-
android:defaultValue="90"
96+
android:defaultValue="80"
9797
android:maxLines="1"
9898
android:singleLine="true"
9999
android:title="@string/memoryThreshold" />

0 commit comments

Comments
 (0)