添加了清除缓存,存在BUG
This commit is contained in:
@@ -1,15 +1,7 @@
|
||||
package com.qxcm.moduleutil.event;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.os.Build;
|
||||
import android.provider.Settings;
|
||||
|
||||
import com.blankj.utilcode.util.GsonUtils;
|
||||
import com.blankj.utilcode.util.ServiceUtils;
|
||||
import com.google.gson.Gson;
|
||||
import com.qxcm.moduleutil.base.CommonAppContext;
|
||||
import com.qxcm.moduleutil.service.FloatingWindow;
|
||||
import com.qxcm.moduleutil.widget.PiaoPingManager;
|
||||
|
||||
import org.greenrobot.eventbus.EventBus;
|
||||
|
||||
@@ -27,6 +19,39 @@ public class RoomGiftRunable implements Runnable {
|
||||
private static final List<MqttBean> mqttCache = new ArrayList<>();
|
||||
private static final int MAX_CACHE_SIZE = 6;
|
||||
private static final Object lock = new Object();
|
||||
|
||||
public static synchronized void addMqttCache(MqttBean mqttBean) {
|
||||
if (mqttBean == null) return;
|
||||
|
||||
// 检查内存状态
|
||||
if (isMemoryLow()) {
|
||||
// 内存不足时清理缓存
|
||||
clearMqttCache();
|
||||
return;
|
||||
}
|
||||
|
||||
// 限制缓存大小
|
||||
if (mqttCache.size() >= MAX_CACHE_SIZE) {
|
||||
mqttCache.remove(0); // 移除最旧的项
|
||||
}
|
||||
|
||||
mqttCache.add(mqttBean);
|
||||
}
|
||||
|
||||
public static synchronized void clearMqttCache() {
|
||||
mqttCache.clear();
|
||||
// 建议进行垃圾回收
|
||||
// System.gc();
|
||||
}
|
||||
|
||||
private static boolean isMemoryLow() {
|
||||
Runtime runtime = Runtime.getRuntime();
|
||||
long usedMemory = runtime.totalMemory() - runtime.freeMemory();
|
||||
long maxMemory = runtime.maxMemory();
|
||||
double memoryUsage = (double) usedMemory / maxMemory;
|
||||
|
||||
return memoryUsage > 0.8; // 内存使用超过80%认为是低内存
|
||||
}
|
||||
public RoomGiftRunable(String data) {
|
||||
this.data = data;
|
||||
}
|
||||
@@ -36,10 +61,26 @@ public class RoomGiftRunable implements Runnable {
|
||||
MqttBean mqttBean= GsonUtils.fromJson(data, MqttBean.class);
|
||||
// 线程安全地添加到缓存
|
||||
synchronized (lock) {
|
||||
if (mqttCache.size() >= MAX_CACHE_SIZE) {
|
||||
mqttCache.remove(0); // 移除第一条数据
|
||||
|
||||
if (mqttBean == null) return;
|
||||
|
||||
// 检查内存状态
|
||||
if (isMemoryLow()) {
|
||||
// 内存不足时清理缓存
|
||||
clearMqttCache();
|
||||
return;
|
||||
}
|
||||
mqttCache.add(mqttBean); // 添加新数据
|
||||
|
||||
// 限制缓存大小
|
||||
if (mqttCache.size() >= MAX_CACHE_SIZE) {
|
||||
mqttCache.remove(0); // 移除最旧的项
|
||||
}
|
||||
|
||||
mqttCache.add(mqttBean);
|
||||
// if (mqttCache.size() >= MAX_CACHE_SIZE) {
|
||||
// mqttCache.remove(0); // 移除第一条数据
|
||||
// }
|
||||
// mqttCache.add(mqttBean); // 添加新数据
|
||||
}
|
||||
EventBus.getDefault().post(mqttBean);
|
||||
// new FloatingWindow(CommonAppContext.getInstance(),mqttBean);
|
||||
|
||||
Reference in New Issue
Block a user