Skip to content

Commit ea609fb

Browse files
committed
Added LruCache function to save memory
1 parent ddacf81 commit ea609fb

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* YukiHookAPI - An efficient Hook API and Xposed Module solution built in Kotlin.
3+
* Copyright (C) 2019-2023 HighCapable
4+
* https://github.com/fankes/YukiHookAPI
5+
*
6+
* MIT License
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in all
16+
* copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
* SOFTWARE.
25+
*
26+
* This file is Created by fankes on 2023/4/20.
27+
*/
28+
package com.highcapable.yukihookapi.hook.utils.memory
29+
30+
import android.util.LruCache
31+
32+
/**
33+
* [LruCache] 内存定义类
34+
*/
35+
internal object LruCacheMemory {
36+
37+
/** 当前最大内存压缩倍数 */
38+
private const val MAX_COMPRESSION_FACTOR = 16
39+
40+
/** 当前可用内存大小 (KB) */
41+
private val maxMemorySize = (Runtime.getRuntime().maxMemory() / 1024).toInt()
42+
43+
/** 当前可用缓存大小 (KB) */
44+
internal val maxCacheSize = maxMemorySize / MAX_COMPRESSION_FACTOR
45+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* YukiHookAPI - An efficient Hook API and Xposed Module solution built in Kotlin.
3+
* Copyright (C) 2019-2023 HighCapable
4+
* https://github.com/fankes/YukiHookAPI
5+
*
6+
* MIT License
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in all
16+
* copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
* SOFTWARE.
25+
*
26+
* This file is Created by fankes on 2023/4/20.
27+
*/
28+
package com.highcapable.yukihookapi.hook.utils.memory.factory
29+
30+
import android.util.LruCache
31+
import com.highcapable.yukihookapi.hook.utils.memory.LruCacheMemory
32+
33+
/**
34+
* 创建 [LruCache]<[K], [V]>
35+
* @param sizeOf 回调自定义缓存大小计算数值 - 默认不设置
36+
* @return [LruCache]<[K], [V]>
37+
*/
38+
internal fun <K, V> createLruCache(sizeOf: (() -> Int)? = null) = object : LruCache<K, V>(LruCacheMemory.maxCacheSize) {
39+
override fun sizeOf(key: K?, value: V?) = when {
40+
sizeOf != null -> sizeOf()
41+
value is Array<*> -> {
42+
var allLengths = 0
43+
if (value.isNotEmpty()) value.forEach { allLengths += it.toString().length }
44+
allLengths / 1024
45+
}
46+
value is Map<*, *> -> {
47+
var allLengths = 0
48+
if (value.isNotEmpty()) value.forEach { allLengths += it.toString().length }
49+
allLengths / 1024
50+
}
51+
value is List<*> -> {
52+
var allLengths = 0
53+
if (value.isNotEmpty()) value.forEach { allLengths += it.toString().length }
54+
allLengths / 1024
55+
}
56+
value is Set<*> -> {
57+
var allLengths = 0
58+
if (value.isNotEmpty()) value.forEach { allLengths += it.toString().length }
59+
allLengths / 1024
60+
}
61+
value is String -> value.length / 1024
62+
else -> value.toString().length / 1024
63+
}
64+
}

0 commit comments

Comments
 (0)