【发布时间】:2017-05-20 02:24:48
【问题描述】:
我正在尝试将从相机接收到的帧保存到哈希表中,以供以后在 Android 应用程序中使用。
1. Receive Frame ( data of type byte [] )
2. I just have a counter variable increased each frame received.
3. Using counter variable i keep in the Hash Table along with 'data'.
4. If i reach the counter > 100, then i start deleting from the beginning.
所以,它在一起是一种保持固定大小的哈希表。
我尝试了以下代码。
Callback -> Received 'data'
counter++;
myHashTable.put(counter, data.clone());
if ( counter > 100) {
byte [] b = (byte[]) myHashTable.get(counter-100);
//use 'b' for some other purpose.
myHashTable.remove(counter-100);
}
我的观察
我检查了应用程序的内存使用情况,它一直在增长,并且在一段时间后收到 OOM 异常。我究竟做错了什么 ?我检查了 Android Studio 内存使用监控。
以下是错误:
01-04 22:54:55.232 7966-7999/sample.app.com E/art: Throwing OutOfMemoryError "Failed to allocate a 460812 byte allocation with 216448 free bytes and 211KB until OOM"
【问题讨论】:
-
当您说“将
b用于其他目的”时,您在使用b做什么?如果某些东西保留了对b的引用,则它不能被垃圾回收。 -
请注意,
byte[] b = (byte[]) myHashTable.remove(counter - 100);比单独获取和删除更容易。 -
为什么不简单地将计数器重置为 100 以覆盖旧内容?
-
@Tanis.7x 感谢您的积分。将检查相同。
-
@Andy Turner 谢谢。会检查这个。