Experience in solving tomcat memory overflow problem

Experience in solving tomcat memory overflow problem

Some time ago, I submitted a product version to testers for testing, and the test results were simply unexpected!

After testing for a while, the page got stuck. Based on this phenomenon, I subconsciously suspected that it was stuck at the database layer. Then I checked the parameters related to the database connection. As expected, the number of connections was too large! After solving the problem of database connection number, I thought the bug was solved, but...

After testing for a while, the page got stuck again! ! !

I opened the task manager and found that the tomcat memory exceeded 1.5G, and tomcat could not be shut down! What is the cause? After thinking about it, I thought of a point that might cause the increase of tomcat's memory, that is, multi-threading. Then I looked through the code for the configuration of the thread pool, and found nothing suspicious.

Then let's solve the problem that tomcat can't be shut down first. Baidu... checked the code... and found it after dozens of minutes. The thread pool was not closed in the destruction method (contextDestroyed) of the tomcat listener. In this case, since the thread pool cannot be closed, tomcat cannot be shut down.

Change the code to:

public class InitListener implements ServletContextListener {
  private Logger logger = Logger.getLogger(InitListener.class);
  @Override
  public void contextInitialized(ServletContextEvent sce) {
    logger.info("Start tomcat");
  }
  @Override
  public void contextDestroyed(ServletContextEvent sce) {
    logger.info("Close tomcat, close thread pool");
    ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("classpath*:applicationContext.xml");
    ThreadPoolTaskExecutor myTaskExecutor = (ThreadPoolTaskExecutor) classPathXmlApplicationContext.getBean("myTaskExecutor");
    myTaskExecutor.shutdown();
  }
}

Well, the problem of Tomcat not being able to shut down has been solved.

Next, solve the memory overflow problem (see the log first):

Checking the tomcat log, I found that the Spring configuration file of the background interface is initialized every time the page calls it, that is, spring will re-inject the bean every time it is requested, and the occupied memory will not be recycled!

Then I wondered when the spring configuration file would be initialized: when tomcat is started; when the keyword new is used, that is,

ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("classpath*:applicationContext.xml");

Then I searched the code globally, and sure enough, I found it in the filter. Every time an interface came, a new object would be created. What a terrible code! I kept cursing myself in my heart for what I was thinking at the time! I will take this experience as a warning and write it down to tell myself not to make similar mistakes again in the future.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • How to solve the problem of Tomcat memory overflow in linux
  • The perfect solution for tomcat memory overflow
  • TOMCAT memory overflow and size adjustment implementation method
  • A solution to the tomcat memory overflow caused by a JSP page
  • Tomcat6.0/7.0 installation version memory overflow setting method
  • Analysis and solution of Tomcat memory overflow

<<:  The pitfalls and solutions caused by the default value of sql_mode in MySQL 5.7

>>:  Detailed explanation of the execution order of JavaScript Alert function

Recommend

Use of MySQL triggers

Triggers can cause other SQL code to run before o...

Use of MySQL trigger

Table of contents 1. Trigger Introduction 1. What...

JavaScript MouseEvent Case Study

MouseEvent When the mouse performs a certain oper...

MySQL 5.7.17 and workbench installation and configuration graphic tutorial

This article shares the installation and configur...

JavaScript to implement the web version of the snake game

This article shares the specific code for JavaScr...

Tutorial on installing and configuring remote login to MySQL under Ubuntu

This article shares the MySQL installation and co...

Three ways to communicate between Docker containers

We all know that Docker containers are isolated f...

Detailed explanation of how to use Teleport, a built-in component of Vue3

Table of contents 1. Teleport usage 2. Complete t...

Vue3.x uses mitt.js for component communication

Table of contents Quick Start How to use Core Pri...

js basic syntax and maven project configuration tutorial case

Table of contents 1. js statement Second, js arra...

11 ways to remove duplicates from js arrays

In actual work or interviews, we often encounter ...

How to Set Shortcut Icons in Linux

Preface Creating shortcuts in Linux can open appl...

Teach you how to build the vue3.0 project architecture step by step

Table of contents Preface: 1. Create a project wi...