本文介绍了用HashMap进行Java内存管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个从数据库中读取大量数据到 Map< String,Map< String,Map< String,String>>>< / $ c的应用程序$ c>,处理它,并使用内部xml编写器将处理后的报告写入电子表格。整个运行可能需要大约12个小时。



我发现我得到了

 在线程CursorController-Thread-0中的异常java.lang.OutOfMemoryError:Java堆空间$ b $在java.lang.AbstractStringBuilder。< init>(AbstractStringBuilder.java:45)
在java.lang.StringBuilder。< init>(StringBuilder.java:68)

当我试图写这个巨型文件。出于这个原因,我认为最好在完成处理时写入每一个 Map< String,Map< String,String>>< / code>(注意更深一层)。 / p>

我的问题是,如何确保 Map< String,Map< String,String>> 在我写完后,它不会保留在内存中,因为Map >>仍然会包含它?

解决方案

完成后通过键key映射到 Map< String,Map< String,String>>

  hugeMap.remove(key); 

这会将 hugeMap 并使 Map< String,Map< String,String>>> 符合垃圾回收的条件(即永远不会造成堆空间不足) 。


I am working on an application which reads in a huge amount of data from a database into a Map<String,Map<String,Map<String,String>>>, processes it, and writes the processed reports to a spreadsheet using an in-house xml writer. The whole run can take about 12 hours.

I'm finding I'm getting

Exception in thread "CursorController-Thread-0" java.lang.OutOfMemoryError: Java heap space
    at java.lang.AbstractStringBuilder.<init>(AbstractStringBuilder.java:45)
    at java.lang.StringBuilder.<init>(StringBuilder.java:68)

When I attempt to write this jumbo file. For this reason I think it would be best to write each Map<String,Map<String,String>> (notice that's a layer deeper)as it's finished processing.

My question is, how can I make sure that the Map<String,Map<String,String>> is not retained in memory after I write it, since the Map>> will still contain it?

解决方案

Once you're done with the Map<String,Map<String,String>> mapped to by the key "key" you simply do

hugeMap.remove("key");

This will "null" out the entry in the hugeMap and make the Map<String,Map<String,String>> eligible for garbage collection (i.e., never be part of causing a heap space out of memory).

这篇关于用HashMap进行Java内存管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 01:26