Solution to tomcat Memory Overflow Caused by an JSP Page

  • 2021-07-01 08:04:26
  • OfStack

Today, my colleagues in the new energy test group came to me to see a strange phenomenon. An tomcat application with only one simple jsp page, and this jsp page without any java code (want to use this jsp page to test the largest QPS of an tomcat on her server). But after a few minutes of pressure measurement with loadrunner, the tomcat that allocated 1024M heap memory actually packed heap space outofmemory! The code for this page is as follows:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<p>hello world!</p>

The initial analysis is that a dead JSP page will produce a corresponding java file, and then this java file will be compiled into class file and loaded into memory. That is, one class object will be loaded into PermGen space. It has nothing to do with heap space. But the last report is the overflow of space. Therefore, it is assumed that an jsp page will generate an object every time it is requested.

Baidu checked once, and found that every time an JSP page is requested, an session object will be generated. There is such a configuration in web. xml of tomcat:


That is to say, every time an jsp page is requested, an session object will be generated, and this object will expire after 30 minutes. We calculated that the QPS at that time was 5000, which means that 5000 session objects were generated every second. 300K objects are generated per minute, and session is an map object, which is relatively large, so it will soon burst the memory.

The solution is as follows:

1. Add session=false to the page directive.

2. Set the expiration time of session to 0.

Now her loadrunner runs steadily. I have never used jsp since I worked, and it is still laborious to check the problems of jsp

Related articles: