Although Java automatically recycles memory, it is best for applications, especially server programs, to specify the memory allocation limit based on business conditions.
Otherwise, the application may be down.
Example description:
-Xms128m
The minimum size of JVM heap (heap memory) is 128 MB and is initially allocated.
-Xmx512m
The maximum size allowed by JVM heap (heap memory) is 256 MB, which is allocated as needed.
NOTE: If-xmx is not specified or the value is too small, the application may cause a java. Lang. outofmemory error. This error is caused by the failure of JVM throwable and cannot be captured using try... catch.
Permsize and maxpermsize indicate that the virtual machine is a permanent Java object (permanate generation)
For example, the Class Object and method object can be reflected (reflective) Object Memory Allocation restrictions, these memories are not included in the heap (heap memory) area.
-XX: permsize = 64 MB minimum size, initial allocation
-XX: maxpermsize = 256 MB: the maximum size allowed for allocation, which is allocated on demand.
Too small will cause: Java. Lang. outofmemoryerror: permgen Space
The default value of maxpermsize is related to the-server-Client Option.
-Maxpermsize is 64 MB by default under the server option
-In the client option, the default maxpermsize is 32 MB.
Experience:
1. Use the minimum limit option XMS with caution. The permsize has saved system resources.
========================================================== ========================
Recent studies have monitored the memory usage of the JVM. Therefore, we have collected some information about the memory usage of the Virtual Machine and learned about the JVM parameter settings:
Several Basic concepts:
Permgen Space: The full name is permanent generation space, that is, permanent generation. That is to say, it is a permanent region used to store class and meta information. When the class is loaded, it is placed in this region. GC (garbage collection) should not clean the permgen space, therefore, if your app loads many classes, the permgen space error may occur.
Heap space: stores instances. Java heap is divided into three zones: young, old, and permanent. Young stores the just-instantiated object. When the area is filled up, GC moves the object to the old area. The permanent area is responsible for saving the reflection object.
Meanings of several parameter settings:
XMS/xmx: DefinitionTotal size of young + Old segments,
Ms is JVMMemory size of young + old at startup;
MX is the maximum available young + old memory size.
In the user production environment, these two values are generally set to the same,To reduce the overhead of the system in memory application during running..
Newsize/maxnewsize: defines the size of the young segment,
Newsize is the memory size of young when JVM is started;
Maxnewsize is the maximum young memory that can be occupied.
In the user production environment, these two values are generally set to the same, to reduce the overhead of the system in the memory application during running.
Permsize/maxpermsize: defines the size of the perm segment. permsize indicates the memory size of the perm when the JVM starts. maxpermsize indicates the maximum available perm memory size. In the user production environment, these two values are generally set to the same, to reduce the overhead of the system in the memory application during running.
Reset vorratio: SetYoung Generation middle region vor SpaceProportion to Eden Space
Process of applying for a piece of memory:
A. JVM will try to initialize a memory area for the relevant Java object in Eden
B. When the Eden space is sufficient, the memory application is completed. Otherwise, go to the next step.
C. JVM tries to release all inactive objects in Eden (this belongs to 1 or more advanced garbage collection); If the Eden space is not enough to put new objects after the release, try to put some active objects in Eden into region VOR/old
D. The primary vor area is used as the intermediate swap area of Eden and old. When the old area has sufficient space, objects in the primary vor area will be moved to the old area. Otherwise, objects in the primary vor area will be retained.
E. When there is not enough space in the old area, JVM will perform full garbage collection in the old area (Level 0)
F. after full garbage collection, if the primary VOR and old areas still cannot store some objects copied from Eden, the JVM cannot create a memory area for the new object in the Eden area, "Out of memory error" appears"
JVM parameter settings for one of our resin servers:
"-Xmx2000m-xms2000m-xmn500m-XX: permsize = 250 m-XX: maxpermsize = 250 m-xss256k-XX: + disableexplicitgc-XX: Required vorratio = 1-XX: + useconcmarksweepgc-XX: + useparnewgc-XX: + cmsparallelremarkenabled-XX: + usecmscompactatfullcollection-XX: bytes = 0-XX: + cmsclassunloadingenabled-XX: bytes = 128 M-XX: + usefastaccessormethods-XX: + response-XX: Response = 60-XX: softreflrupolicymspermb = 0-XX: + printclasshistogram-XX: + printgcdetails-XX: + printgctimestamps-XX: + printheapatgc-xloggc: log/GC. log"
Is a typical time-First Response configuration.
There are four different collection algorithms in Java, and the corresponding startup parameter is
-XX: + useserialgc
-XX: + useparallelgc
-XX: + useparalleloldgc
-XX: + useconcmarksweepgc
1. Serial collector
This is used by default on most platforms or forced Java-client.
Young Generation Algorithm = serial
Old Generation Algorithm = serial (mark-sweep-compact)
The disadvantage of this method is obvious: Stop-the-world, the speed is slow. Server applications are not recommended.
2. Parallel collector
This is the default option on Linux x64. This option is selected by default only when Java-server parameters are added to other platforms.
Young = parallel, multiple threads copy at the same time
Old = mark-sweep-compact = 1
Advantage: The New Generation recovery is faster. Because most of the time GC is performed by the system is a new generation, this increases the throughput (CPU is used for non-GC time)
Disadvantage: when there are too many old generation live objects running on 8g/16g server, the pause time is too long.
3. Parallel compact collector (parallelold)
Young = parallel = 2
Old = parallel, which is divided into multiple independent units. If the number of live objects in a unit is small, it is recycled. If the number of live objects is large, it is skipped.
Advantage: the performance of old generation is improved compared with that of parallel.
Disadvantages: most server systems use 60%-80% of the old generation memory, and there are not so many ideal unit live objects for fast recovery. At the same time, the Compact overhead is not significantly less than parallel.
4. concurent mark-sweep (CMS) Collector
Young Generation = parallel collector = 2
Old = CMS
Compact operations are not performed at the same time.
Advantage: pause time is reduced, and pause is sensitive but the CPU is idle. We recommend that you use the policy 4.
Disadvantage: CPU usage is too high, and CPU-intensive servers are not suitable. In addition, there are too many fragments. Each object must be stored in N consecutive places through the linked list, and the space waste problem will also increase.
Memory monitoring method:
1. jmap-heap PID
View Java heap usage
Using thread-local object allocation.
Parallel GC with 4 thread (s) // GC Mode
Heap configuration: // heap memory Initialization Configuration
Minheapfreeratio = 40 // corresponding JVM startup parameter-XX: minheapfreeratio sets the minimum JVM heap idle ratio (default 40)
Maxheapfreeratio = 70 // corresponding JVM startup parameter-XX: maxheapfreeratio sets the maximum JVM heap idle rate (default 70)
Maxheapsize = 512.0 MB // corresponding JVM startup parameter-XX: maxheapsize = set the maximum JVM heap size
Newsize = 1.0 MB // corresponding to the JVM startup parameter-XX: newsize = set the default size of the JVM heap ''
Maxnewsize = 4095 MB // corresponding to the JVM startup parameter-XX: maxnewsize = sets the maximum size of the JVM heap ''
Oldsize = 4.0 MB // corresponding JVM startup parameter-XX: oldsize = <value>: sets the size of the JVM heap 'old generation '.
Newratio = 8 // The size ratio of the JVM startup parameter-XX: newratio =: 'new generation' to 'Old generation'
Required vorratio = 8 // corresponding to the JVM startup parameter-XX: Required vorratio = set the ratio of Eden to region vor in the young generation
Permsize = 16.0 MB // corresponding JVM startup parameter-XX: permsize = <value>: sets the initial size of the JVM heap 'permanent generation '.
Maxpermsize = 64.0 MB // corresponding JVM startup parameter-XX: maxpermsize = <value>: sets the maximum size of the JVM heap 'permanent generation '.
Heap usage: // heap memory step by step
PS young generation
Eden space: // Memory Distribution in the Eden area
Capacity = 20381696 (19.4375 MB) // total capacity of the Eden area
Used = 20370032 (19.426376342773438 MB) // used in the Eden area
Free = 11664 (0.0111236572265625 MB) // remaining Eden Zone capacity
99.94277218147106% used // Eden usage rate
From space: // Memory Distribution of one of the Region vor zones
Capacity = 8519680 (8.125 MB)
Used = 32768 (0.03125 MB)
Free = 8486912 (8.09375 MB)
0.38461538461538464% used
To space: // Memory Distribution in another region vor
Capacity = 9306112 (8.875 MB)
Used = 0 (0.0 MB)
Free = 9306112 (8.875 MB)
0.0% used
PS old generation // Memory Distribution of the current old zone
Capacity = 366280704 (349.3125 MB)
Used = 322179848 (307.25464630126953 MB)
Free = 44100856 (42.05785369873047 MB)
87.95982001825573% used
PS perm generation // current "permanent generation" Memory Distribution
Capacity = 3
Http://blog.sina.com.cn/s/blog_684fe8af0100v4mt.html
JVM Virtual Machine option: XMS xmx permsize maxpermsize difference ()