I’m trying to tune my JSF application memory consuption by setting JVM arguments because I’m getting out of memory error.
I’m able to increase memory heap and restart the server twice a day, but it’s not a solution…
JVM arguments:
-XX:+UseConcMarkSweepGC
-XX:+UseParNewGC
-XX:+CMSClassUnloadingEnabled
-XX:+CMSParallelRemarkEnabled
-XX:CMSInitiatingOccupancyFraction=30
-XX:+CMSIncrementalMode
-XX:+CMSIncrementalPacing
-XX:ParallelCMSThreads=2
-XX:+UseCMSCompactAtFullCollection
-XX:+DisableExplicitGC
-XX:MaxHeapFreeRatio=70
-XX:MinHeapFreeRatio=40
-XX:MaxTenuringThreshold=30
-XX:NewSize=512m
-XX:MaxNewSize=512m
-XX:SurvivorRatio=2
-XX:PermSize=150m
-Xms1024m
-Xmx1024m
Everything seems to work fine, tenured space is at 0 MB, eden space is continuosly cleared but survivor space is still growing and when it reach its limit, object are moved to tenured space and never been released. And after half a day of running application I get the out of memory error. So I have to schedule automatical restarts of tomcat server.

So I think, there must be some problem in my application, because its memory consuption is too high for such a small appl (about thousands movements in database per day).
There is part of my code:
Bean.java
/* save datalist */
public void save()
{
session = DaoSF.getSessionFactory.openSession();
try
{
Dao.save(dataList);
}
catch (Exception e) {...}
finally
{
session.close();
}
}
Dao.java
public static void save(List<? extends Object> dataList)
{
for (Object dataItem : dataList)
{
save(dataItem);
}
}
public static void save(Object dataItem)
{
try
{
Transaction tx = session.beginTransaction();
session.save(dataItem);
tx.commit();
}
catch(Exception e) {....}
}
DaoSF.java
public class DaoSF implements Serializable
{
private static final long serialVersionUID = 1L;
private static SessionFactory sessionFactory;
private static Session hibSession;
private static synchronized void initSessionFactory
{
Configuration config = new Configuration();
config.configure("hibernate.cfg.xml");
sessionFactory = config.buildSessionFactory();
hibSession = sessionFactory.getCurrentSession();
}
public static SessionFactory getSessionFactory
{
initSessionFactory;
return sessionFactory;
}
public static Session getSession()
{
return hibSession;
}
}
Could you tell me, what are the best practices in saving (updating, deleting) of object to database so that the saved object don’t stay in memory?
Try this. It’s not yet perfect, but I think you must get rid of the static session attribute in Dao. See hibernate docs: http://docs.jboss.org/hibernate/envers/3.5/javadocs/org/hibernate/Session.html:
It is not intended that implementors be threadsafe. Instead each thread/transaction should obtain its own instance from a SessionFactory.
You can also obtain the session inside Dao itself, so that Bean does not have any knowledge of the underlying persistence mechanisms, but I did not want to change that much.
Bean.java
Dao.java