Java Hibernate 内存泄漏
我正在使用 BlazeDS 开发 Flex 应用程序,并且在休眠中使用 java 从 MySQL 查询时遇到内存泄漏。谁能告诉我如何处理这个内存泄漏?似乎每次查询调用 java.exe 都会占用更多内存。
谢谢
我的java示例
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT);
EntityManager em = factory.createEntityManager();
Query find = em.createNamedQuery("Plan.findByStudentId");
find.setParameter("studentId", studentID);
List<Plan> c = find.getResultList();
return c;
I'm developing a Flex application with BlazeDS and I'm experiencing memory leak when using java to query from MySQL in hibernate. Can anyone tell me how to deal with this memory leak? It seems that each time query is invoke java.exe takes more memory.
Thanks
Sample of my java
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT);
EntityManager em = factory.createEntityManager();
Query find = em.createNamedQuery("Plan.findByStudentId");
find.setParameter("studentId", studentID);
List<Plan> c = find.getResultList();
return c;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设在将数据返回到 flex 后,您的 EntityManager 保持打开状态,这意味着您的 MySQL 连接保持打开状态。这就是内存泄漏最有可能发生的地方。因此 - 关闭您的
EntityManager
。在另一条评论中,您说您正在使用 GlassFish。这是一个全新的场景。你的类是 Servlet 吗? EJB? GlassFish 应该管理您的 EntityManager(如果类本身是托管的),因此您不必自己创建或关闭它。在这种情况下,使用 @PersistenceContext 注释来注入 EntityManager (而不是使用 Persistence.create..),
但是你必须做的事情是setup,就是启动一个分析器并查看该内存分配在哪里。
I assume you are leaving your
EntityManager
open after you return the data to flex, which in turn means that your MySQL Connection remains open. That's where the memory leak most likely comes from. So - close yourEntityManager
.In anoter comment you said that you are using GlassFish. That's a whole new scenario. Is your class this a Servlet? An EJB?. GlassFish is supposed to manage your EntityManagers (if the class itself is managed), so you don't have to create or close it yourself. In such cases use
@PersistenceContext
annotation to inject theEntityManager
(instead of usingPersistence.create..
)But the thing you must do whatever the setup, is to start a profiler and see where is this memory allocated.
您好,您只需在 try catch 块中输出代码并关闭实体管理器。
Hi you just need to out your code in a try catch block and close the entity manager.