2011-06-16 8 views
6

मैं टॉमकैट 6.0.18 का उपयोग कर रहा हूं। मेरे ऐप्लिकेशन के undeployemnt के बाद, HttpClient इसलिए स्मृति रिसाव के कारण WebappClassLoader के लिए एक संदर्भ के आयोजन किया जाना है, प्रकट होता है।sun.net.www.http.Http क्लाइंट मेमोरी लीक टॉमकैट 6

कुछ जांच के बाद, मैं समाधान में बिलाव 7.0.6, JreMemoryLeakPreventionListenerkeepAliveProtection के साथ पाया है विशेषता। लेकिन इस दृष्टिकोण Tomcats 6 (मैं JreMemoryLeakPreventionListener कस्टमाइज़ कर दिया है, ताकि इस विशेषता के लिए समर्थन जोड़ने के लिए) के साथ काम नहीं कर रहा है।

किसी को भी कैसे बिलाव 6 में इस रिसाव को ठीक करने के लिए एक समाधान है? Thanx!

+0

एक जवाब के रूप में अपने समाधान पोस्ट करें, और इसे स्वीकार्य के रूप में चिह्नित करें, अन्यथा सवाल खुला रहेगा। – skaffman

+0

@ स्काफमैन हो गया! – Igor

उत्तर

8

मुझे स्मृति रिसाव का समाधान मिला है।

एक ServletContextListener के कार्यान्वयन करना चाहिए, निम्नलिखित के रूप में:

package org.example; 

public class MyServletContextListener implements ServletContextListener { 

    public void contextDestroyed(ServletContextEvent sce) { 
     tomcatLeakPreventionForHttpClient(); 
    } 

    private void tomcatLeakPreventionForHttpClient() { 
     try { 
      final Field kac = HttpClient.class.getDeclaredField("kac"); 
      kac.setAccessible(true); 
      final Field keepAliveTimer = KeepAliveCache.class.getDeclaredField("keepAliveTimer"); 
      keepAliveTimer.setAccessible(true); 

      final Thread t = (Thread) keepAliveTimer.get(kac.get(null)); 
      if(t.getContextClassLoader() == Thread.currentThread().getContextClassLoader()) { 
       t.setContextClassLoader(ClassLoader.getSystemClassLoader()); 
      } 
     } catch(final Exception e) { 
     } 
    } 

    public void contextInitialized(ServletContextEvent event) { 
    } 

} 

और, ज़ाहिर है, web.xml में श्रोता रजिस्टर करने के लिए:

<listener> 
    <listener-class>org.example.MyServletContextListener</listener-class> 
    </listener> 
संबंधित मुद्दे