ViewScoped bean 内存泄漏? [英] Memory leak with ViewScoped bean?
问题描述
在 JBoss 7.1.1 上的 JavaEE6 项目(EJB3、JSF2)中,我们似乎有SeamFaces @ViewScoped
的内存泄漏.
In our JavaEE6 project (EJB3, JSF2) on JBoss 7.1.1, it seems we have a memory leak with SeamFaces @ViewScoped
.
我们制作了一个小原型来检查事实:
We made a little prototype to check the fact :
- 我们使用 JMeter 调用页面 200 次;
- 页面包含并调用一个注入有状态 EJB 的视图范围 bean;
- 我们将会话超时修复为 1 分钟.
在测试结束时,我们使用 VisualVM 检查内存的内容,结果如下:
At the end of the test, we check the content of the memory with VisualVM, and here what we got:
- 使用
@ViewScoped
bean,我们仍然得到 200 个有状态的MyController
实例 - 并且@PreDestroy
方法永远不会被调用; - 使用
@ConversationScoped
bean,@preDestroy
方法被称为会话结束,然后我们获得了干净的内存.
- with a
@ViewScoped
bean, we still get 200 instances of the statefulMyController
- and the@PreDestroy
method is never called; - with a
@ConversationScoped
bean,@preDestroy
method is called a the session end and then we got a clean memory.
我们是否严重使用了视图范围,或者它真的是一个错误?
Do we badly use the view scope, or is it truly a bug?
这是 XHTML 页面:
Here's the XHTML page:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:s="http://jboss.org/seam/faces">
<f:metadata>
<f:viewParam name="u" value="#{myBean.uselessParam}" />
<s:viewAction action="#{myBean.callService}" />
</f:metadata>
<h:body >
<f:view>
</f:view>
</h:body>
</html>
现在包含的 bean myBean
.对于 @ConversationScoped
变体,所有注释部分都未注释.
Now the included bean myBean
. For the @ConversationScoped
variant, all commented parts are uncommented.
@ViewScoped
// @ConversationScoped
@Named
public class MyBean implements Serializable
{
@Inject
MyController myController;
//@Inject
//Conversation conversation;
private String uselessParam;
public void callService()
{
//if(conversation.isTransient())
//{
// conversation.begin();
//}
myController.call();
}
public String getUselessParam()
{
return uselessParam;
}
public void setUselessParam(String uselessParam)
{
this.uselessParam = uselessParam;
}
}
然后注入有状态的 bean MyController
:
And then the injected stateful bean MyController
:
@Stateful
@LocalBean
public class MyController
{
public void call()
{
System.out.println("call ");
}
@PreDestroy
public void destroy()
{
System.out.println("Destroy");
}
}
推荐答案
我看到许多开发人员对 Myface CODI 中的 @ViewAccessScoped 感到满意.你能不能试一试并告诉反馈.
I see many developers are satisfied with @ViewAccessScoped in Myface CODI. Could you please give it a try and tell the feedback.
这篇关于ViewScoped bean 内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!