title: Java Heap Space Error due to add function
body: <p>Whenever I am calling the getFriendList function from the xhtml page I am getting an exception. The page keeps on loading and after that I stop the browser. I get the given stackstrace. Its is telling me that there is an error at line no.78 <code>friends.add(friend);</code> . I think there is something wrong with my syntax. It would be helpful if someone point the error.</p>
  
  <p>1)Here is my FriendBean it is request scoped.</p>
  
  <pre><code>package com.bean;
  
  import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.List;
  
  import javax.faces.context.FacesContext;
  import javax.persistence.EntityManager;
  import javax.persistence.EntityManagerFactory;
  import javax.persistence.Persistence;
  import javax.persistence.Query;
  import javax.servlet.http.HttpSession;
  import com.entity.Friend;
  import com.entity.User;
  
  public class FriendBean {
  
      Friend friend;
      private EntityManager em;
      public FriendBean(){
          friend = new Friend();
          EntityManagerFactory emf=Persistence.createEntityManagerFactory("FreeBird");
           em =emf.createEntityManager();
      }
      public Friend getFriend() {
          return friend;
      }
      public void setFriend(Friend friend) {
          this.friend = friend;
      }
      public List&lt;Friend&gt; getFriendRequestList(){
          FacesContext context = FacesContext.getCurrentInstance();
          HttpSession session = (HttpSession) context.getExternalContext().getSession(true);
          User user=(User) session.getAttribute("userdet");
          Query query = em.createQuery("SELECT f FROM Friend f WHERE f.email='"+user.getEmail()+"' AND f.status=0",Friend.class);
            List&lt;Friend&gt; results =query.getResultList();
            return results;
  
  
      }
      public String friendAccept(String friendRequestId){
  
          System.out.println("friend accept called...");
          Friend f=em.find(Friend.class,Integer.parseInt(friendRequestId));
          f.setStatus(1);
          em.getTransaction().begin();
          em.persist(f);
          em.getTransaction().commit();
          Friend viceversa=new Friend();
          viceversa.setEmail(f.getFriendEmail());
          viceversa.setFriendEmail(f.getEmail());
          viceversa.setStatus(1);
          em.getTransaction().begin();
          em.persist(viceversa);
          em.getTransaction().commit();
          return "success";
  
      }
      public String friendReject(String friendRequestId){
          System.out.println("friend reject called..");
          Friend f = em.find(Friend.class,Integer.parseInt(friendRequestId));
          em.getTransaction().begin();
          em.remove(f);
          em.getTransaction().commit();
          return "success";
      }
      public List&lt;User&gt; getFriendList(){
  
          FacesContext context = FacesContext.getCurrentInstance();
          HttpSession session = (HttpSession) context.getExternalContext().getSession(true);
          User user=(User) session.getAttribute("userdet");
          Query query = em.createQuery("SELECT f FROM Friend f WHERE f.email='"+user.getEmail()+"' AND f.status=1",Friend.class);
           List&lt;Friend&gt; results =query.getResultList();
           ArrayList&lt;User&gt; friends = new ArrayList&lt;User&gt;();
  
           while(results.listIterator().hasNext()){
               User friend =em.find(User.class,results.listIterator().next().getFriendEmail());
               friends.add(friend);
           }
            return friends;
  
      }
  }
  </code></pre>
  
  <p>2)Here is the exception that I am getting.</p>
  
  <pre><code>SEVERE: Error Rendering View[/Home.xhtml]
  javax.el.ELException: /Home.xhtml @146,59 value="#{friendBean.friendList}": java.lang.OutOfMemoryError: Java heap space
      at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:114)
      at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
      at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
      at javax.faces.component.UIData.getValue(UIData.java:731)
      at javax.faces.component.UIData.getDataModel(UIData.java:1798)
      at javax.faces.component.UIData.setRowIndexWithoutRowStatePreserved(UIData.java:484)
      at javax.faces.component.UIData.setRowIndex(UIData.java:473)
      at com.sun.faces.renderkit.html_basic.TableRenderer.encodeBegin(TableRenderer.java:81)
      at javax.faces.component.UIComponentBase.encodeBegin(UIComponentBase.java:820)
      at javax.faces.component.UIData.encodeBegin(UIData.java:1118)
      at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1755)
      at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
      at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
      at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:402)
      at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
      at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
      at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
      at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
      at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
      at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
      at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:79)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
      at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
      at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
      at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
      at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
      at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
      at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
      at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
      at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
      at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
      at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
      at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
      at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
      at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
      at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
      at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
      at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
      at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
      at java.lang.Thread.run(Thread.java:722)
  Caused by: java.lang.OutOfMemoryError: Java heap space
      at java.util.Arrays.copyOf(Arrays.java:2245)
      at java.util.Arrays.copyOf(Arrays.java:2219)
      at java.util.ArrayList.grow(ArrayList.java:213)
      at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:187)
      at java.util.ArrayList.add(ArrayList.java:411)
      at com.bean.FriendBean.getFriendList(FriendBean.java:78)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:601)
      at javax.el.BeanELResolver.getValue(BeanELResolver.java:363)
      at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
      at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
      at com.sun.el.parser.AstValue.getValue(AstValue.java:138)
      at com.sun.el.parser.AstValue.getValue(AstValue.java:183)
      at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:224)
      at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
      at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
      at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
      at javax.faces.component.UIData.getValue(UIData.java:731)
      at javax.faces.component.UIData.getDataModel(UIData.java:1798)
      at javax.faces.component.UIData.setRowIndexWithoutRowStatePreserved(UIData.java:484)
      at javax.faces.component.UIData.setRowIndex(UIData.java:473)
      at com.sun.faces.renderkit.html_basic.TableRenderer.encodeBegin(TableRenderer.java:81)
      at javax.faces.component.UIComponentBase.encodeBegin(UIComponentBase.java:820)
      at javax.faces.component.UIData.encodeBegin(UIData.java:1118)
      at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1755)
      at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
      at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
      at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:402)
      at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
      at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
  
  WARNING: StandardWrapperValve[Faces Servlet]: PWC1406: Servlet.service() for servlet Faces Servlet threw exception
  java.lang.OutOfMemoryError: Java heap space
      at java.util.Arrays.copyOf(Arrays.java:2245)
      at java.util.Arrays.copyOf(Arrays.java:2219)
      at java.util.ArrayList.grow(ArrayList.java:213)
      at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:187)
      at java.util.ArrayList.add(ArrayList.java:411)
      at com.bean.FriendBean.getFriendList(FriendBean.java:78)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:601)
      at javax.el.BeanELResolver.getValue(BeanELResolver.java:363)
      at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
      at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
      at com.sun.el.parser.AstValue.getValue(AstValue.java:138)
      at com.sun.el.parser.AstValue.getValue(AstValue.java:183)
      at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:224)
      at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
      at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
      at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
      at javax.faces.component.UIData.getValue(UIData.java:731)
      at javax.faces.component.UIData.getDataModel(UIData.java:1798)
      at javax.faces.component.UIData.setRowIndexWithoutRowStatePreserved(UIData.java:484)
      at javax.faces.component.UIData.setRowIndex(UIData.java:473)
      at com.sun.faces.renderkit.html_basic.TableRenderer.encodeBegin(TableRenderer.java:81)
      at javax.faces.component.UIComponentBase.encodeBegin(UIComponentBase.java:820)
      at javax.faces.component.UIData.encodeBegin(UIData.java:1118)
      at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1755)
      at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
      at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
      at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:402)
      at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
      at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
  </code></pre>