JAVA Server SETUP Summary
Responsible for maintaining the web server construction and maintenance of the company's products. I recently encountered a situation. I would like to briefly summarize it here today. I hope it will help some of my new friends and avoid detours.
First: Tomcat memory settings:
I. There are three common Java memory overflow types:
1. java. lang. OutOfMemoryError: Java heap space ---- JVM Heap (heap) Overflow
The JVM automatically sets the JVM Heap value at startup. The initial space (-Xms) is 1/64 of the physical memory, and the maximum space (-Xmx) cannot exceed the physical memory.
You can use options such as-Xmn-Xms-Xmx provided by JVM to set the configuration. The Heap size is the sum of Young Generation and Tenured Generaion.
In JVM, if 98% is used for GC and the available Heap size is less than 2%, this exception is thrown.
Solution: manually set the JVM Heap size.
2. java. lang. OutOfMemoryError: PermGen space ---- PermGen space overflow.
PermGen space stands for Permanent Generation space, which is the Permanent storage area of the memory.
Why is the memory overflow? This is because the memory is mainly used by JVM to store Class and Meta information. When the Class is loaded, it is placed in the PermGen space area, it is different from the Heap region where the Instance is stored. sun's GC will not clean up the PermGen space during the main program running, so if your APP will load many classes, permGen space overflow is likely to occur.
Solution: manually set the MaxPermSize
3. java. lang. StackOverflowError ---- Stack Overflow
Stack Overflow. The JVM is still a stack-type virtual machine, which is the same as C and Pascal. The call process of the function is reflected in the stack and rollback.
There are too many "layers" for calling constructors, so that the stack zone overflows.
Generally, the stack zone is much smaller than the heap zone, because the function call process is usually no more than thousands of layers, even if each function call requires 1 K space (which is equivalent to declaring 256 int-type variables in a C function), the stack zone only needs 1 MB space. The stack size is usually 1-2 MB.
Generally, recursion should not involve too many recursive layers, so it is easy to overflow.
Solution: modify the program.
Set the initial memory for Tomcat startup. The initial space (-Xms) is 1/64 of the physical memory, and the maximum space (-Xmx) is 1/4 of the physical memory.
You can use options such as-Xmn-Xms-Xmx provided by JVM to set
Linux:
Add catalina. sh in the/usr/local/apache-tomcat-5.5.23/bin directory:
JAVA_OPTS = '-Xms512m-xmx1024m'. Adding "m" indicates that it is MB. Otherwise, it is KB. When tomcat is started, insufficient memory is reported.
-Xms: Initial Value-Xmx: Maximum Value-Xmn: Minimum value
Windows
Add set JAVA_OPTS =-Xms128m-Xmx350m to the front of catalina. bat.
If you start tomcat with startup. bat, the OK setting takes effect. The memory is allocated MB.
However, if you do not execute startup. bat starts tomcat but uses the windows System Service to start the tomcat service. The above settings do not take effect, that is, set JAVA_OPTS =-Xms128m-Xmx350m does not work. the above MB memory is allocated to OOM ..
The windows service executes bin \ tomcat.exe. It reads the value in the registry, instead of setting catalina. bat.
Solution:
Modify the Registry HKEY_LOCAL_MACHINE \ SOFTWARE \ Apache Software Foundation \ Tomcat Service Manager \ Tomcat5 \ Parameters \ JavaOptions
The original value is-Dcatalina. home = "C: \ ApacheGroup \ Tomcat 5.0"-Djava. endorsed. dirs = "C: \ ApacheGroup \ Tomcat 5.0 \ common \ endorsed"-add Xrs-Xms300m-Xmx350m
Restart the tomcat service and the settings take effect.
Analysis of java. lang. OutOfMemoryError: PermGen space found that many people attributed the problem to: spring, hibernate, and tomcat, because they dynamically generate classes, resulting in permanent heap Overflow in JVM. There are different solutions. Some people say that upgrading tomcat to the latest version does not even require tomcat. Some people suspect that the issue of spring is very intense in the spring Forum, because the use of CBLIB in spring in AOP will dynamically generate many classes. But the question is, why is the open source of these trump cards having the same problem? Is it a more basic reason? Tomcat answers this question in A vague manner in Q & A. We know this question, but this question is generated by A more basic question. So someone checked the more basic JVM and found the key to the problem. In the past, SUN's JVM divided the memory into different zones, one of which is the permenter area used to store a lot of classes and class descriptions. Originally SUN thought that this region was fixed during JVM startup, but he did not expect that the current dynamics would be so widely used. In addition, this region has a special garbage collection mechanism. The problem now is that gc cannot be recycled after classes are dynamically loaded to this region!
For the above two problems, my solution is:
Add the following in the first line of catalina. bat:
Java code: set JAVA_OPTS =-Xms64m-Xmx256m-XX: PermSize = 128 M-XX: MaxNewSize = 256 m-XX: MaxPermSize = 256 m
Add the following in the first line of catalina. sh:
Java code: JAVA_OPTS =-Xms64m-Xmx256m-XX: PermSize = 128 M-XX: MaxNewSize = 256 m-XX: MaxPermSize = 256 m
Second: the server memory usage often exceeds 90%
This is what I feel most terrible, because I cannot find a problem in the program. This problem occurs mostly because the server configuration is too low and there are too many services under Tomcat, which eventually leads to a high server pressure strike.
My solution to this problem is: 1. apply to improve the server configuration; 2. Open the services under Tomcat to different servers; 3. Increase the server's virtual memory size.
For the first two solutions, I 'd like to hear about them. Let's focus on how to manually increase the virtual memory size of the server:
My computer --> right-click and choose Properties --> advanced system settings --> performance options --> advanced --> virtual memory --> change (for details, see)
It should be noted that when we set the virtual memory size for the first time, click OK to set it successfully. When we set it for the second time, if the setting is smaller than the previous one, click OK, the system will prompt us to restart the computer. If we set it bigger than before, it will be consistent with the first setting. When we click OK, we can complete the setting.
Third, the server memory usage suddenly reaches 99%
For this question, I also just encountered it. I found that the service named sychost.exe is used for a large memory temporarily. After searching on DU Niang, this service should be a windows upgrade service, but we cannot close it blindly because if we close it directly, may cause a blue screen. Here I will explain one way: start --> all programs --> management tools --> services
Set the Windows Update Service to manual.
Well, here we will briefly describe the summary of service establishment. If there is any improper situation, I would like to give some advice. Thank you.