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的viewscoped 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页面:
<!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屋!