Java持久性内存泄漏 [英] java persistance memory leaks
问题描述
我在mysql表中有1M行,当我执行以下代码时,我是java持久性api,然后出现Java堆错误:
I have 1M rows in a mysql table and I am java persistence api when I execute following code then I get java heap error:
int counter = 0;
while (counter < 1000000) {
java.util.Collection<MyEntityClass> data = myQuery.setFirstResult(counter)
.setMaxResults(1000).getResultList();
for(MyEntityClass obj : data){
System.out.println(obj);
}
counter += 1000;
}
推荐答案
当您单击下一步"时,我想知道JTable是否真的挂在了所有那些旧引用上.我不认为这是一个持久性问题.无论JTable背后有什么支持的数据结构,在添加下一批记录之前,我都必须确保已清除它.这样,旧值就可以进行GC处理了.
I'd wonder if JTable is really hanging onto all those old references when you click "next". I don't believe it's a persistence problem. Whatever backing data structure you have behind the JTable, I'd make sure that I cleared it before adding the next batch of records. That way the old values can be GC'd.
您的JTable不应具有ResultSet.最好有一个持久层,从客户那里隐藏这样的细节.查询一批值(不是整个数据集的 ),将其从ResultSet加载到数据结构中,然后在finally块中关闭ResultSet和Statement.您需要在创建它们的方法范围内关闭这些资源,否则您将遇到麻烦.
Your JTable shouldn't have a ResultSet. It'd be better to have a persistence tier that hid such details from clients. Make the query for a batch of values (not the entire data set), load it from the ResultSet into a data structure, and close the ResultSet and Statement in a finally block. You need to close those resources in the scope of the method in which they were created or you're asking for trouble.
这篇关于Java持久性内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!