繁体   English   中英

用过的堆大小不断增加

[英]Used heap size increasing continuously

我有JSF Web应用程序。 我正在使用JSF 2.1.9,Hibernate 4.1.4,GlassFish 3.1.2,PrimeFaces 3.4.1。 问题是,使用过的堆大小缓慢增加,并且在2-3天后达到最大堆大小。 然后,我必须重新启动GlassFish。

堆转储:

一开始,我单击了应用程序中的所有网页,使用的堆大小为100 MB:

堆转储之前

在1-2天之后,已使用的堆大小将增加到300 MB(这段时间内使用了相同的网页):

之后的堆转储

我拍摄了堆中最常用的类的屏幕截图。

char[]类实例中,有太多这样的SQL查询字符串: 堆转储字符

也许不仅存在一个问题,而且我可能会从这个问题开始解决。 通常,在我的网页中,我从数据库中选择一些对象并进行渲染。 这是一些bean:图片(索引控制器):

@Named("indexController")  
@SessionScoped  
public class IndexController implements Serializable {  
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("imagePU");  

    public List<Image> getImages() {  
        EntityManager em = emf.createEntityManager();  
        List<Image> result;  
        try {  
            EntityTransaction entr = em.getTransaction();  
            boolean committed = false;  
            entr.begin();  
            try {  
                Query query = em.createQuery("SELECT i FROM Image i ORDER BY i.imageId DESC").setMaxResults(12);  
                result = query.getResultList();  
                entr.commit();  
                committed = true;  
            } finally {  
                if (!committed) {  
                    entr.rollback();  
                }  
            }  
        } finally {  
            em.close();  
        }  
        return result;  
    }  
}  

标记的图像:

@Named("galleryBean")  
@SessionScoped  
public class GalleryBean implements Serializable {  

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("imagePU");  

    public List<TaggedImage> getTaggedImages() {  
        EntityManager em = emf.createEntityManager();  
        List<TaggedImage> result;  
        try {  
            EntityTransaction entr = em.getTransaction();  
            boolean committed = false;  
            entr.begin();  
            try {  
                Query query = em.createQuery("SELECT ti FROM TaggedImage ti GROUP BY ti.tag ORDER BY ti.taggedImagesId DESC");  
                result = query.getResultList();  
                entr.commit();  
                committed = true;  
            } finally {  
                if (!committed) {  
                    entr.rollback();  
                }  
            }  
        } finally {  
            em.close();  
        }  
        return result;  
    }  
}  

顺便说一句,我不应该在吸气剂中执行业务逻辑,但是我认为这不是我遇到问题的主要原因。 我需要帮助和一些建议。 如果需要,我可以提供更多信息。

谢谢你的帮助。

我曾经在JSF页面上遇到过类似的问题,经过大量研究证明,问题在于JSF在会话中保持的视图数量。 不确定这是否是您的问题,但是请查看配置视图数。 希望这可以帮助。

不知道这是否能解决您的问题,但是您正在使用会话作用域支持Bean,并且基于以上代码片段,是否真的可以请求作用域的Bean,因为我并没有真正看到它们需要进行会话保存。 我还将考虑使用一个可能的视图范围(如果有)并重新构造Bean,这样您就不必从数据库中多次调用中受益,因为jsf多次调用支持Bean是众所周知的。

@Named("galleryBean")  
@RequestScoped  // or @ViewScoped
public class GalleryBean implements Serializable {  

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("imagePU");  

    private List<TaggedImage> images = null;

    public List<TaggedImage> getTaggedImages() {  
        if (this.images != null) {
            return this.images;
        }
        EntityManager em = emf.createEntityManager();  
        try {  
            EntityTransaction entr = em.getTransaction();  
            boolean committed = false;  
            entr.begin();  
            try {  
                Query query = em.createQuery("SELECT ti FROM TaggedImage ti GROUP BY ti.tag ORDER BY ti.taggedImagesId DESC");  
                images = query.getResultList();  
                entr.commit();  
                committed = true;  
            } finally {  
                if (!committed) {  
                    entr.rollback();  
                }  
            }  
        } finally {  
            em.close();  
        }  
        return images;  
    }  
}  

更好的是,如果使用漂亮的面孔和getTaggedImages()实际上只是成为另一个getter /,则可以从数据库中分离出TaggedImage的实际提取,并使用@PostConstruct或@URLAction将其放置在Bean构造阶段的一部分,称为@PostConstruct或@URLAction。二传手

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM