I have my JSF Handler defined in request scope as follows
public class JsfHandler {
private List<ManagedBean> managedBeanList; // managed bean List in session scope
}
<managed-bean>
<managed-bean-name>jsfHandler</managed-bean-name>
<managed-bean-class>com.test.JsfHandler</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>managedBeanList</property-name>
<value>#{managedBeanList}</value>
</managed-property>
</managed-bean>
I have a session scoped JSF managed bean list in JSF defined as follows.
<managed-bean>
<managed-bean-name>managedBeanList</managed-bean-name>
<managed-bean-class>java.util.ArrayList</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
Approach 1:
managedBeanList.clear();
managedBeanList.addAll(service.getResultList());
// assuming service.getResultList() would return a new ArrayList();
Approach 2:
managedBeanList = service.getResultList();
FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("managedBeanList",managedBeanList);
We are currently using approach 1.
Is approach 2 correct? Is there is any advantage in terms of memory usage when Java Garabage collector is called in appraoch 2.
Neither is correct. That list has to be a property of a session scoped managed bean. You should avoid manually fiddling with the session map as much as possible.
Neither of the one is more “GC-friendly” than the other. In approach 1 you end up with an unreferenced instance to the list as returned by
service.getResultList(). In approach 2 you end up with an unreferenced instance of the list which was previously referenced bymanagedBeanList. In both cases the GC has to do the same job: cleaning an unreferenced instance.