Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
783 views
in Technique[技术] by (71.8m points)

jsf 2 - Memory leak with ViewScoped bean?

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 :

  • we use JMeter to call a page 200 times;
  • the page contains and calls a viewscoped bean which injects a stateful EJB;
  • we fix the session timeout at 1 minute.

At the end of the test, we check the content of the memory with VisualVM, and here what we got:

  • with a @ViewScoped bean, we still get 200 instances of the stateful MyController - 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?


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>

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;
    }
}

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");
   }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I see many developers are satisfied with @ViewAccessScoped in Myface CODI. Could you please give it a try and tell the feedback.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...