Tomcat rose to a record memory overflow experiences

Some time ago submitted a product version to testers test results simply surprise!

After a period of testing the page on the card is dead, then to suspect that this phenomenon is the subconscious level this card to the database, and then check the database connection-related parameters, such as expected similar number of connections too much! When the database connections resolved, the thought that this bug is resolved, but ...

After a period of testing the page and stuck up! ! !

Open the Task Manager, find tomcat memory than 1.5G, and can not afford to shut tomcat! What causes it? After much thought, thought of a tomcat point may cause memory prices, that is multi-threaded, then turned to find the code to configure the thread pool and found nothing suspicious.

Then try to solve the problem it can not afford to shut down tomcat, Baidu ... ... check the code to find a few minutes later, the thread pool is not closed in the destruction methods tomcat listener (contextDestroyed) in this case, Since the thread pool not closed, which led to the problem can not be closed tomcat.

Code to read:

public class InitListener implements ServletContextListener{
    private Logger logger = Logger.getLogger(InitListener.class);
    
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        logger.info("启动tomcat");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        logger.info("关闭tomcat,关闭线程池");
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("classpath*:applicationContext.xml");
        ThreadPoolTaskExecutor myTaskExecutor = (ThreadPoolTaskExecutor) classPathXmlApplicationContext.getBean("myTaskExecutor");
        myTaskExecutor.shutdown();
    }

}

Well, tomcat can not afford to shut the problem is solved.

Next to solve the problem (look at the log) memory leak:

View tomcat logs found pages every call to the interface background Spring configuration files are initialized again , that every request will be re-injected into a spring bean, but also takes up memory will not be recovered!

Then I wanted to be initialized under what circumstances the spring configuration file: When tomcat starts; by keyword new came out, that is,

ClassPathXmlApplicationContext = new ClassPathXmlApplicationContext ClassPathXmlApplicationContext ( "the CLASSPATH *: applicationContext.xml" );

and then a global search to find the code, she found in the filter, each time the interface will be a new target, more terrible code, has been criticized in the heart own how to think! I will learn a lesson from this experience, but also down to tell myself this will not repeat a similar problem.

 

Welcome concern public micro-channel number] [Java books, Java technology watch more dry!

   ▼ sweep the next micro-channel two-dimensional code Follow FIG ↓↓↓

 

Guess you like

Origin www.cnblogs.com/bingyimeiling/p/11505844.html