2 Replies Latest reply on Feb 19, 2013 12:21 AM by Oleksandr Podoprygora

    JSF: Am I using ViewScoped correctly or is there memory leak?

    Oleksandr Podoprygora Newbie

      Hi all.

       

      I'm new to JSF and JBoss and maybe I'm doing something stupid.

      I'm using jboss-as-7.1.1.Final.

      I have created simple bean:

       

      package test;
      
      import java.io.Serializable;
      
      import javax.annotation.PostConstruct;
      import javax.annotation.PreDestroy;
      import javax.faces.bean.ManagedBean;
      import javax.faces.bean.ViewScoped;
      import javax.faces.context.ExternalContext;
      import javax.faces.context.FacesContext;
      
      @ManagedBean
      @ViewScoped
      public class TestClass implements Serializable {
          private String test = "test";
          private byte[] b;
      
          private static int initialize_count; 
          private static int destroy_count; 
          private static int finalize_count; 
      
          public TestClass() {
              b = new byte[1024*1024*8];
          }
      
          @PostConstruct
          public void initialize() {
              System.out.println("initialize " + initialize_count++);
          }
          @PreDestroy
          public void destroy() {
              System.out.println("destroy " + destroy_count++);
          }
          @Override
          protected void finalize() throws Throwable {
              System.out.println("finalize " + finalize_count++);
              super.finalize();
          }
      
          public String getTest() {
              return test;
          }
          public void setTest(String test) {
              this.test = test;
          }
      
          public void invalidateSession() {
              FacesContext facesContext = FacesContext.getCurrentInstance();
              ExternalContext externalContext = facesContext.getExternalContext();
              externalContext.invalidateSession();
          }
      
          public String leaveScope() {
              return "index.jsf";
          }
      }
      

       

      and simple jsf page:

       

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.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:c="http://java.sun.com/jsp/jstl/core">
      
          <h:form>
              <h:outputText value="#{testClass.test}" />
              <h:commandButton action="#{testClass.invalidateSession}" value="Invalidate Session" />
              <h:commandButton action="#{testClass.leaveScope}" value="Leave Scope" />
          </h:form>
      </html>
      

       


      Then I reloaded index.jsf page for 45 times and got the exception:

       

      02:44:58,266 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/deployment-portal].[FacesServlet]] (http-localhost-127.0.0.1-8080-1) Servlet.service() for servlet FacesServlet threw exception: java.lang.OutOfMemoryError: Java heap space
          at test.TestClass.<init>(TestClass.java:23) [classes:]
          at sun.reflect.GeneratedConstructorAccessor29.newInstance(Unknown Source) [:1.7.0_03]
          at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) [rt.jar:1.7.0_03]
          at java.lang.reflect.Constructor.newInstance(Constructor.java:525) [rt.jar:1.7.0_03]
          at java.lang.Class.newInstance0(Class.java:372) [rt.jar:1.7.0_03]
          at java.lang.Class.newInstance(Class.java:325) [rt.jar:1.7.0_03]
          at com.sun.faces.mgbean.BeanBuilder.newBeanInstance(BeanBuilder.java:188) [jsf-impl-2.1.7-jbossorg-2.jar:]
          at com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:102) [jsf-impl-2.1.7-jbossorg-2.jar:]
          at com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:409) [jsf-impl-2.1.7-jbossorg-2.jar:]
          at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:269) [jsf-impl-2.1.7-jbossorg-2.jar:]
          at com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:244) [jsf-impl-2.1.7-jbossorg-2.jar:]
          at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:116) [jsf-impl-2.1.7-jbossorg-2.jar:]
          at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176) [jsf-impl-2.1.7-jbossorg-2.jar:]
          at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203) [jsf-impl-2.1.7-jbossorg-2.jar:]
          at org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:72) [jbossweb-7.0.13.Final.jar:]
          at org.apache.el.parser.AstValue.getValue(AstValue.java:147) [jbossweb-7.0.13.Final.jar:]
          at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:189) [jbossweb-7.0.13.Final.jar:]
          at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:50) [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
          at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109) [jsf-impl-2.1.7-jbossorg-2.jar:]
          at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
          at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
          at javax.faces.component.UIOutput.getValue(UIOutput.java:169) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
          at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getValue(HtmlBasicInputRenderer.java:205) [jsf-impl-2.1.7-jbossorg-2.jar:]
          at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getCurrentValue(HtmlBasicRenderer.java:355) [jsf-impl-2.1.7-jbossorg-2.jar:]
          at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeEnd(HtmlBasicRenderer.java:164) [jsf-impl-2.1.7-jbossorg-2.jar:]
          at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
          at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1786) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
          at javax.faces.render.Renderer.encodeChildren(Renderer.java:168) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
          at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
          at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1779) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
          at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1782) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
          at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:402) [jsf-impl-2.1.7-jbossorg-2.jar:]
      

       

      Should that code works?

       

      Thanks.