可能由于org.hibernate.internal.SessionFactoryImpl导致的内存泄漏 [英] Possible Memory Leak due to org.hibernate.internal.SessionFactoryImpl

查看:3015
本文介绍了可能由于org.hibernate.internal.SessionFactoryImpl导致的内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Java中创建了MVC webapp,但是当我每天运行一次时,由于内存错误而再次关闭。



这个错误是this:
在线程http-apr-12136-exec-42中的异常java.lang.OutOfMemoryError:Java堆空间

java.sql.SQLException: java.lang.OutOfMemoryError:Java堆空间

我拥有崩溃统计信息的hprof,其中具体介绍了如何使用内存。如果我打开Eclipse Memory Analizer的hprof,我得到了这个结果:



在rar中: https://mega.co.nz/#!Ht41xJDJ!MooePBSv5yOYSNN5OuvF7Afn2rcN-KJ2tXGSsgqtsaI



或者在一个文件夹中: https://mega.co.nz/#F!6hJUyKbQ!D_Kb23E3KfAJqcd5EeAt0A



在Overview报告中,我有这张图片(OverviewEMA.JPG):
我不知道这张图片说什么...我不知道'b
$ b

在第二个选项卡中,默认报告,我有这个图形(DefaulReport_EMA.JPG):
它说问题可能是一个org.hibernate.internal.SessionFactoryImpl的实例。但我不知道如何解决这个问题。



在下一个标签中。在支配树中,再次出现前一个实例,它使用大约42MB的内存(与显示第一个图形相同)。图像是DominatorTree_EMA.JPG



如果我展开第一个类(出现问题的类),我有这个图形(DominatorTreeExpanded_EMA.JPG):



在下一个选项卡的直方图中,图形是this(Histogram_EMA.JPG):

在Unreachable对象中,结果是这样的(UnreachableObjects_EMA.JPG):



我不太明白这2张最后一张图片

最后,我还有Java VisualVM的报告,我得到了这个结果(Heapdump_JVM.JPG):

根据这个图形,HashMap对象是问题,除了Integer和String对象。我认为HashMap对象是类发送给jsp文件的模型对象,它从JPA对象(Hibernate的对象)出发,所以问题可能在于此,但我不知道如何解决它...



有人可以帮我吗?有人知道我该如何解决它?你需要更多的信息吗?

回顾DominatorTree_Expanded,你似乎反复创建SessionFactor(在内存中有144个)。这应该只在创建时创建一次,然后用于创建任意数量的会话。



另请参阅以下关于正确使用Hibernate Session的评论。






您的Hibernate会话应该是 local 到请求 - 并在请求结束时关闭。您可以使用OpenSessionInView模式在Controller处理过程中将会话绑定到线程。查看(JSP)呈现。



我怀疑,因为您要使用OutOfMemory,因此您将Hibernate Session作为控制器的实例变量一个静态的地方。因为Web请求可能是并发的,所以Controller不应共享请求处理状态(例如Hibernate Sessions或可变变量)作为实例变量。这会在不同的请求和不同请求之间造成不希望的交互。线程。


I have made and MVC webapp in Java, but when I run it, once a day, it turns down again due to a memory error.

This error is this: Exception in thread "http-apr-12136-exec-42" java.lang.OutOfMemoryError: Java heap space

java.sql.SQLException: java.lang.OutOfMemoryError: Java heap space

I have the hprof with the stats of the crash, where specific how is the memory used. If I open the hprof with the Eclipse Memory Analizer, I have this results:

In rar: https://mega.co.nz/#!Ht41xJDJ!MooePBSv5yOYSNN5OuvF7Afn2rcN-KJ2tXGSsgqtsaI

Or in a folder: https://mega.co.nz/#F!6hJUyKbQ!D_Kb23E3KfAJqcd5EeAt0A

In the Overview report, I have this graphic (OverviewEMA.JPG): I don't know what this graphic say... I don't understand it.

In the second tab, the default report, I have this graphic (DefaulReport_EMA.JPG): It say the problem can be one instance of "org.hibernate.internal.SessionFactoryImpl". But I don't know how to solve that instance.

In the next tabs. In the dominator tree, again appears the previous instance, which use around 42MB of memory (the same that show the first graphic). The image is DominatorTree_EMA.JPG

If I expand the first class (the class that gives problems), I have this graphic (DominatorTreeExpanded_EMA.JPG):

In the next tab, in the histogram, the graphic is this (Histogram_EMA.JPG):

And in the Unreachable objects, the result is this (UnreachableObjects_EMA.JPG):

I don't understand very well this 2 last graphics

At last, I also have the report of Java VisualVM, where I have this results (Heapdump_JVM.JPG):

According to this graphic, the HashMap objects are the problem, besides Integer and String objects. The Hashmap objects I think are the objects of the model that the classes send to jsp files, and it proceed from the JPA Objects (the objects of Hibernate), so the problem could be this, but I don't know how to solve it...

Could someone help me? Someone know how can I fix it? Do you need some information more?

Thanks!

解决方案

On reviewing DominatorTree_Expanded, you appear to be creating SessionFactorys repeatedly (there are 144 in memory). This should be created only once, at startup, then used to create any number of Sessions.

See also my comments below about proper use of Hibernate Session.


Your Hibernate session should be local to the request -- and closed when the request ends. You can use the "OpenSessionInView" pattern to bind a session to the thread during Controller processing & View (JSP) rendering.

I suspect, since you are going OutOfMemory, that you are keeping a Hibernate Session as an "instance variable" of your Controller -- or as a static somewhere. This should never be done.

Since web requests may be concurrent, a Controller should never share request-processing state (such as Hibernate Sessions, or mutable variables) as instance variables. This would cause unwanted interaction between separate requests & threads.

这篇关于可能由于org.hibernate.internal.SessionFactoryImpl导致的内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆