org.springframework.web.servlet.view.InternalResourceViewResolver 記憶體泄露問題,resourceview

來源:互聯網
上載者:User

org.springframework.web.servlet.view.InternalResourceViewResolver 記憶體泄露問題,resourceview

Spring使用 org.springframework.web.servlet.view.InternalResourceViewResolver 記憶體泄露問題

通過 Eclipse Memory Analyzer (http://www.eclipse.org/mat/)分析工具如下,堆記憶體中50.51% 是由InternalResourceViewResolver 產生的。

我當前使用的Spring版本是:Spring 3.1.1.RELEASE

One instance of "org.springframework.web.servlet.view.InternalResourceViewResolver" loaded by"org.apache.catalina.loader.WebappClassLoader @ 0x6e0024ca0" occupies2,342,803,136 (50.51%) bytes. The memory is accumulated in one instance of"java.util.HashMap$Entry[]" loaded by"<system class loader>".

Keywords
org.springframework.web.servlet.view.InternalResourceViewResolver
java.util.HashMap$Entry[]
org.apache.catalina.loader.WebappClassLoader @ 0x6e0024ca0

在經過對線上的服務監控,發現系統發生了很多次的full gc,為什麼頻繁的Full GC不能回收掉的呢;原因是大量存活對象對HashMap持有強參考關聯性導致的。

JVM記憶體對象回收策略是經過GC ROOT可達性分析,當一個對象經過GC Root 不可達時,說明這個對象已經dead,下次就就可以被回收掉。

接下來我們從代碼中分析下,看是什麼原因導致的。

#我想做JAVA的同學,以下代碼是不是感覺很熟悉,單從這方法看是不是覺得沒有問題.....

@RequestMapping(value = "/index.do",params="userId")public ModelAndView index(String userId,ModelAndView mv) {  // TODO 業務處理...    mv.setViewName("redirect:/your_uri.do?userId="+userId);    return mv;}

那麼我們在看下Spring對這個返回重新導向的處理方式吧

在InternalResourceViewResolver的父類AbstractCachingViewResolver 

 public View resolveViewName(String viewName, Locale locale) throws Exception {    if (!isCache()) {      return createView(viewName, locale);    } else {      Object cacheKey = getCacheKey(viewName, locale);      synchronized (this.viewCache) {        View view = this.viewCache.get(cacheKey);        if (view == null && (!this.cacheUnresolved || !this.viewCache.containsKey(cacheKey))) {          // Ask the subclass to create the View object.          view = createView(viewName, locale);          if (view != null || this.cacheUnresolved) {            this.viewCache.put(cacheKey, view);            if (logger.isTraceEnabled()) {              logger.trace("Cached view [" + cacheKey + "]");            }          }        }        return view;      }    }  }

在這裡Spring 使用viewName+locale 作為Cache key 將建立的View ,Cache到HashMap了,避免每次重新建立View。以下就是Spring viewCache 執行個體對象源碼

 /** Map from view key to View instance */  private final Map<Object, View> viewCache = new HashMap<Object, View>();
由於我們return  ModelAndView 對象的URI 中存在動態參數,每個使用者返回的URI都不一樣,所有導致Cache Key無法命中,進而建立大量View ,Cache到viewCache.

"redirect:/your_uri.do?userId="+userId
如果需要使用Spring重新導向建議採用RedirectView 處理,例如:

  @RequestMapping(value = "/index.do", params = "userId")  public RedirectView index(RedirectView mv,String userId) {    // TODO 業務處理    UriComponentsBuilder builder = UriComponentsBuilder.fromPath("/xxx.do");    builder.queryParam("userID", userId);    mv.setUrl(builder.build().encode().toUriString());    return mv;  }



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.