【问题标题】:Remove the hash table element does not release the memory删除哈希表元素不释放内存
【发布时间】: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 谢谢。会检查这个。

标签: java android hashtable


【解决方案1】:

我认为简单的解决方案是当计数器达到 > 100 时,您没有将计数器设置为 0 在 if(){ counter=0} 里面添加我猜是因为它是一个无限循环 尝试通过添加打印语句来调试它并查找您的控制台。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-21
    • 2013-11-09
    • 2013-07-21
    • 2011-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多