使用 Map,而不是 ArrayList 存储大量数据
Map map = new HashMap();
map.put("key1", "value1");
map.put("key2", "value2");
int cacheSize = (int) (Runtime.getRuntime().maxMemory() / 1024);
LruCache memoryCache = new LruCache(cacheSize) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
return bitmap.getByteCount() / 1024;
}
};
memoryCache.put("image.png", bitmap);
// 停止活动
finish();
// 在 MainActivity.java 中添加以下代码
import android.os.Debug;
// 在 onCreate 方法中添加以下代码
Debug.startMethodTracing("myapp");
// 在 onDestroy 方法中添加以下代码
Debug.stopMethodTracing();
上述代码会将应用程序的跟踪信息写入到 sdcard/myapp.trace 文件中。可以使用 Android Studio 中的 Trace 文件查看器分析跟踪文件。