So far, I have encountered three situations using Tomcat: First, use Eclipse, configure Tomcat in Eclipse. Second, deploy the project directly in Tomcat. Third, install Tomcat as a windows service.
In these three cases, OutOfMemoryError appears. How to solve it? Here I have to mention that I was tragically hurt by those irresponsible articles on the Internet. I tried all kinds of ways to set memory, but it just didn't work. The methods I mentioned below are all tested by myself, and there is no problem.
The first case:
As shown in the picture: I made it with a red frame. Among them, Xms and Xmx are the parameters of the initial heap size and maximum heap size of the java virtual machine, which is mainly to solve the reasons caused by insufficient Java Heap Space. XX:PermSize and XX:MaxPermSize are parameters that increase the initial permanent storage area size and the maximum permanent storage area size. They mainly solve the problem that a large number of jars or classes are used in the program. The Java virtual machine has insufficient space for loading classes, which is related to Permanent Generation space.
The second case:
Put the compiled project directly in Tomcat and use startup.bat to start the Tomcat service. As follows in Tomca's bin folder:
In this case, we need to modify the parameters in catalina.bat. Open it. Find these two sentences:
rem Guess CATALINA_HOME if not defined
set "CURRENT_DIR=%cd%"
Add this sentence to these two sentences, and be sure to: set "JAVA_OPTS=-Xms512M -Xmx1024M -XX:PermSize=256m -XX:MaxPermSize=512m"
There are many similar things on the Internet, but many of them have not been tried. Be sure to pay attention to the details such as its position and quotes.
The third situation:
If your tomcat is registered as a Windows service and started in service, then the above method is invalid, because at this time tomcat start is to read the parameters of the registry rather than the parameters of the batch file. Let's set the jvm parameters like this. : Find tomcat7w.exe in the bin directory of Tomcat and open:
Under the java options: the Initial memory pool below is the initialization of the heap memory size, and the Maximun memory pool is the maximum heap memory size. To set the size of the Perm Gen pool, you need to add parameters to the Java Option, and add:
-Dcatalina.base=%tomcat_home%
-Dcatalina.home=%tomcat_home%
-Djava.endorsed.dirs=%tomcat_home%/endorsed
-Djava.io.tmpdir=%tomcat_home%/temp
-XX:PermSize=256M
-XX:MaxPermSize=256M
-XX: ReservedCodeCacheSize=48M
-Duser.timezone=GMT+08
(Don't have spaces after each line) I've tried this method, it works. You can also add parameters directly in the registry. I won't introduce this, I haven't tried it myself.
In fact, it is not difficult to solve the Tomcat memory problem. How do you know that this is caused by Tomcat memory overflow exception. We have been bothering about this issue for a long time before. If you use Eclipse, an error message will be printed in the console. You can directly determine that it is an OutOfMemoryError. If you are directly deployed to Tomcat and started with startup.bat, you can also see the printed error message. But we installed the Tomcat service into a Windows service, so we couldn't see any error prompts. After the project was started, it was always in a waiting state, which was very slow, making people confused. I just happened to catch up with the database and even thought it was a database problem. In short, inexperienced.
The above is all about solving OutOfMemoryError in Java, and I hope it will be helpful to everyone's learning.