- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
以下示例表明,在 LRUCache 之外分配新空间时,使用有限大小的 LRUCache 会导致 OutOfMemory 错误。
属性:64MB 进程大小; 10MB LRUCache 大小;我循环放入 LRUCache 的 1MB block 。
在 57 次(64MB - 7MB)次尝试后,我得到:
05-15 09:05:51.385: E/AndroidRuntime(11630): FATAL EXCEPTION: main
05-15 09:05:51.385: E/AndroidRuntime(11630): java.lang.OutOfMemoryError
05-15 09:05:51.385: E/AndroidRuntime(11630): at com.example.testlrucachewithpathes.MyDataClass.<init>(MyDataClass.java:14)
在 lrucache.evictall() 之后,缓存被释放并且有足够的空间再次分配。但我想那不是做事的方式。
有什么提示吗?
这是我的代码:
public class StartActivity extends Activity {
int iMegabyte=1000000;
LruCache<String, Object> nativelrucache=new LruCache<String, Object>(iMegabyte*10);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
// Do my stuff
Log.v("MEMORY STATE", getMemoryStatus());
// Case with MyDataClass ------------------------------------------------------------------------------------
for(int i=0;i<100;i++){
MyDataClass mdataclass=new MyDataClass(iMegabyte);
//lrucachemanager.put("ID_" + i, mdataclass);
Log.v("MEMORY STATE", "put data into cache : " + i);
nativelrucache.put("ID_" + i, mdataclass);
//nativelrucache.evictAll();
}
}
}
public class MyDataClass {
byte[] bytes;
public MyDataClass(int iSize){
//Arrays.fill( bytes, 0 );
bytes=new byte[iSize];
}
}
最佳答案
来自 http://developer.android.com/reference/android/util/LruCache.html
By default, the cache size is measured in the number of entries. Override sizeOf(K, V) to size the cache in different units.
所以你应该这样做:
LruCache<String, MyDataClass> nativelrucache=new LruCache<String, MyDataClass>(iMegabyte*10){
protected int sizeOf(String key, MyDataClass value) {
return value.bytes.length;
};
};
关于android - LRUCache 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23671687/
我的应用程序中的多个 Activity 在 ListView 中显示图像,其中 ListView 的每一行都包含一个 ImageView. 这方面的一个示例是用户搜索、获取结果并显示每个结果的图片的搜
一 点睛 强引用(Strong Reference)是我们使用最多的一种对象引用,当一个对象被关键字 new 实例化出来的时候, JVM 会在堆(heap)内存中开辟一个内存区域,用于存放与该实例对应
我使用了代码 android lrucache example (Memory Cache)缓存下载的图像(总共将近 120 KB)并且它可以工作,但是当我从 Activity (或应用程序)中退出时
我正在阅读这篇文章来了解 Java 中的 LRUCache - Java LRU cache using LinkedList 这篇文章中有一个解决方案: public class LRUCache
我正在考虑为 Android 实现离线模式,但为了实现这一目标,我需要实现一个缓存系统。我读到人们建议 LruCache 并在这里看到了图像的示例 LruCache Example 。现在我不缓存图像
我正在尝试遵循有关 LruCache 使用的 2 年历史的 android 教程,到目前为止我在 Google 上搜索的一些示例具有相同的方法,即传递一个转换为 KiB 的值(int)。 final
我不能完全理解 LruCache 的内存管理组件。 从某种意义上说,我希望它能充当 iOS 平台上的 NSCache(最近刚刚从该操作系统中学到了一些知识)。 如果应用程序内存不足,LruCache
以下示例表明,在 LRUCache 之外分配新空间时,使用有限大小的 LRUCache 会导致 OutOfMemory 错误。 属性:64MB 进程大小; 10MB LRUCache 大小;我循环放入
我正在使用 LruCache 来缓存我的应用程序中使用的大量小 BitmapDrawables。问题是尺寸因我使用图像的不同位置而异。 我在从缓存中检索可绘制对象时设置边界,然后再将其设置为 Imag
我是 android 中 LruCache 的新手,我想在此缓存上放置和获取位图图像 (JPEG) 以防止内存错误和内存异常,所以我不明白为什么我的代码不起作用。这是我的代码: ImageView i
我已经在 Android 中实现了一个存储对象的标准 LRUCache。每个键都是与存储的对象关联的唯一 ObjectId。我的问题是从缓存中检索对象的唯一方法是通过 ObjectId(无迭代器)。实
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024); final int cacheSize = max
例如对 网络加载图片进行缓存 : ? 1
我尝试解决 LeetCode 中的问题,要求实现一个LRUCache。当我提交代码时,系统告诉我结果是错误答案。 由于测试用例太长,我在代码中找不到问题。当我选择“运行代码”来提交我的代码时,它是正确
我可以在 Cachetools 的 LRUCache 实现中使用多个/组合键吗?我想像下面这样使用它 def fun(a,b): pass x = LRUCache(maxsize=100,missi
我有一个 LRUCache,用于缓存缩略图和其他类似的小位图。目前,我在我的主要 Activity 中使用它,但是,我也想在我的其他 Activity 中使用它。这让我想知道将这个 LRUCache
我从 developer.android.com 上读到了关于使用 LruCache 的信息,我从一个 Activity 中创建了一个模糊的 Bitmap 并将其放入缓存中,现在我想访问来自另一个 A
我一直在尝试在我的应用程序中实现 LruCache,但我在连接点和在不同组件之间传递位图时遇到困难。 我想知道如何在我的应用程序中集成 LruCache。我也想了解实现 LruCache 的过程,所以
我正在尝试在 android 中使用 LruCache 来缓存一些图像,但它没有缓存 这是代码 int cacheSize1 = 4 * 1024 * 1024; // 4MiB bit
我有以下代码来缓存位图,当我尝试使用它们的键检索它们时。他们总是返回 null 。请帮我 。谢谢你 final int memClass = ((ActivityManager) mcontext.
我是一名优秀的程序员,十分优秀!