English 中文(简体)
JSF 企业应用内存使用
原标题:JSF Enterprise application memory usage

I have an Java EE application as follows:
- Server is on Amazon (large instance, 2 CPU @ 2.27GHz, 8GB RAM) - Static content served directly by Apache
- JSF2 (Mojarra 2.1.3) and JPA 2 (Eclipselink 2.3.0) running on Glassfish 3.1.1
- Facelets/XHTML get content from ViewScoped managed beans, that connect to @Local Stateless EJB that do all the processing, including getting data from other @Local Stateless EJB that use JPA
So typically:

XHTML --> ViewScoped Managed Bean --> Service EJB --> Data EJB --> JPA

我知道我可以/应该把EJB的两层移到一个或甚至一个,因为我只运行过一只玻璃鱼,但现在我不认为这是问题所在。

The performance of the application is ok (2.2MB including images in < 5s). The problem is that when we have > 90 users online the system becomes really slow ( > 30 s per page, even if most of it is cached).
At that time the CPU was 50% used and RAM was 100% used.

So I ran JProfiler and I am not sure how to deal with one type of result.
In the homepage, we have a list of categories, and a number of product is associated to each category (a shopping website that tells how many product in each category). The code to get the list of Category is:

受观测的 Bean

public List<Category> getLiveCategoriesInfo() {
    if (liveCategories == null) {
        liveCategories = liveCategoryService.getLiveCategories(getLocale().getLang().getLanguageId());
    }
    return liveCategories;
}

getlocale () 使用管理Property 从会话Scoped Bean 中提取到已注射的本地语言

EJB服务:

public List<Category> getLiveCategories(final Integer langId) {
  List<LiveCategory> lives = categoryBean.getLiveCategories(langId);
  // ... some processing involving looping through the list above
  return livesCategories;
}

数据 EJB :

public List<LiveCategory> getLiveCategories(final Integer langId) {
  List<LiveCategory> categories = new ArrayList<LiveCategory>();
  Query cq = getEntityManager().createNamedQuery(Category.FIND_LIVE);
  try {
    categories = cq.getResultList();
  } catch (NullPointerException npe) {
     // ...
  }
  return categories;
}

JProfiler Memory View shows that at every request on the homepage (even for the same user) a new batch of Category is added to the memory (43 to be precise, which is the number of categories displayed). The Category is not managed by JPA (the list from JPA is used to create POJO manually ).
How can I release these entities from the memory. I would expect them to be GC when the view is gone. But the ViewScoped bean is itself is not GC d, there is a bunch of instances that stay in memory.

What should I look for to release these objects?
- Is using a @ManagedProperty in a ViewScoped bean to get an instance of SessionScoped bean preventing the ViewScoped one to be GC d?
- Is there some other mistake I should look for?

我查了JSF最佳做法和业绩准则的其他线索,但无济于事。

最佳回答

我认为您的问题与您如何使用视图范围和会话范围有关。 以几个字来说, 您正在用在页面寿命期内不会变化的物体填充记忆中。 相反, 您应该使用请求范围块来缓存这些结果, 只有在处理请求时, 并且使用@ ManagedProperty 批注 或任何( 创建值表达方式或调用 < a href= > http://docs. oracle.com/javax/face/ application/ Application. html# viewExpressionGet% 28javax. faces. context. FacesContext,% 20java.lang. string,% 20java. lang. clas% 29 " relass% =“ nofolpolvection” > approcess. valeExpressionGet 来获取您查看范围或会话的参数。 这样, 请求结束时, 实体的参考文件将会在请求结束时公布, 由 GC 收集 。

如果您真的对表演部分感兴趣, 请看看这个博客 :

"http://lu4242.blogspot.com/2012/05/underadidjsf-2-and-wicket.html" rel=“不跟随” > 理解JSF 2和Wicket:业绩比较

测试代码已经被调整为 JSF 和 Wicket 获得最佳性能。 调制 JSF 很简单, 所以通常您应该查看您的 ORM 工具来提供性能提示 。

问题回答

可能您可以尝试在用户登录退出时发布 FaceSContext 。





相关问题
Weak event handler model for use with lambdas

OK, so this is more of an answer than a question, but after asking this question, and pulling together the various bits from Dustin Campbell, Egor, and also one last tip from the IObservable/Rx/...

JavaScript memory problem with canvas

I m using getImageData/putImageData on a HTML5 canvas to be able to manipulate a picture. My problem is that the browser never seems to free any memory. Not until I close the tab (tested in Chrome and ...

Memory leak for CComBSTR

I have read that the following code causes memory leak. But did not understand why. CComBSTR str; pFoo->get_Bar(&str); pFoo->get_Baf(&str); How does it cause a leak when we are not ...

Hunting memory leaks

I m finding leaked heap blocks by using the following command in WinDbg !heap –l With each leaked heap block I get, I m running to following to get the stack trace. !heap -p -a leakedheapblock The ...

NSScanner memory leak

I m at my first experiences with iPhone development. I wrote some basic code to test the NSScanner class, and now I was looking into the Leaks tool. It seems that this code is leaking, when in the ...

Do Small Memory Leaks Matter Anymore?

With RAM typically in the Gigabytes on all PC s now, should I be spending time hunting down all the small (non-growing) memory leaks that may be in my program? I m talking about those holes that may ...

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

热门标签