Step-by-step optimization of JVM Four: Determining the size of the Java heap and memory footprint

Source: Internet
Author: User

Original: http://blog.csdn.net/zhoutao198712/article/details/7783070

So far, no definite optimization work has been done. It's just an initialization selection, such as the JVM deployment model, the JVM runtime environment, what garbage collector information is collected, and the need to follow the garbage collection principle. This step describes how to evaluate the size of the memory that your application needs and the Java heap size. First, it is necessary to determine the size of the data that the application survives, and the size of the surviving data is an important condition for determining the size of the Java heap required by the configuration application, as well as deciding whether to revisit the application's memory requirements or modify the application to meet memory requirements.


Note: Survival data refers to the long-lived objects in the Java heap that are used in a steady state of operation. In other words, the space occupied by the Java heap after the full GC is applied in a state of steady operation.ConstraintsHow much physical memory is available for the JVM to use? Deploy multiple JVMs or a single JVM? Have a significant impact on the decisions made.   Here are a few points to help you decide how much physical memory is available for use. 1. A machine that deploys only one JVM and uses it for one application?   If this is the case, then all the physical memory of the machine can be used by the JVM. 2. How many JVMs are deployed on one machine? Or are there multiple applications deployed on one machine?   In either case, you have to decide how much memory each JVM or application needs to allocate. Either way, you need to set aside some memory for the operating system.heap structure for HotSpot VMSBefore we make a memory footprint measurement, we have to understand the structure of the hotspot VM Java heap, which is helpful for determining the size of the Java heap needed for the application and for optimizing the performance of the garbage collector.    The HotSpot VM has 3 main spaces: young generation, old generation, and permanent, as shown in. When Java applications allocate Java objects, these objects are assigned to young generation. After a few minor GC, if the object is still alive, it will be transferred to the old generation space.    The permanent space stores metadata for VMS and Java classes, such as built-in strings and static variables for classes. The two command-line options,-XMX and-XMS, specify the initial maximum and minimum values for the sum of the yound generation plus the old generation space, which is the size of the Java heap. When the value of-XMS is less than the value of-XMX, the size of the Java heap can be floated before the maximum and minimum values.    When Java applications emphasize throughput and latency, they tend to set-XMS and-xmx to the same value, because adjusting the size of young or old will require full gc,full GC to reduce throughput and increase latency. Young generation's space can be set by any of the following command-line options:[HTML]View Plaincopy
    1. 1,-xx:newsize=<n>[g|m|k]
The initial and minimum values of young generation. <n> is the size, [G|m|k] indicates the unit is G-byte, M-byte, or kilobytes. Young's size is not less than this value. When setting-xx:newsize=<n>[g|m|k],-xx:maxnewsize=<n>[g|m|k] needs to be specified.[HTML]View Plaincopy
    1. 2,-xx:maxnewsize=<n>[g|m|k]
The maximum size of the young area space. With the above in turn, specify-XX:NEWSIZE=&LT;N&GT;[G|M|K] when specifying-XX:MAXNEWSIZE=&LT;N&GT;[G|M|K].[HTML]View Plaincopy
    1. 3,-xmn<n>[g|m|k]
Specify the initial, minimum, and maximum values of young generation directly. That is, the size of the young area is fixed to this value.    This value is handy for locking Young's size. One thing to note is that if-XMS and-xmx are not set to the same value, and-xmn is used, when the size of the Java heap is resized, the space size of young will not be adjusted, and the space size of young generation will remain constant.    Therefore,-XMN should be specified only when-XMS and-xmx are set to the same time. The space size of the old generation can be calculated based on the size of young, the initial value of the old generation is the value of-xms minus-xx:newsize, the maximum is-xmx minus-xx:maxnewsize, if-xmx and-XMS are set to the same value,    And with the-XMN option or-xx:newsize and-xx:maxnewsize set to the same value, the old generation size is-xmx minus-xmn. The size of the permanent is specified by the following command-line arguments[HTML]View Plaincopy
    1. 1,-xx:permsize=<n>[g|m|k]
Represents the initial and minimum values of the permanent generation, n represents a numeric value, and g|m|k represents the unit, permanent space must not be smaller than this space.[HTML]View Plaincopy
    1. 2, -xx:maxpermsize=<< Span class= "Tag-name" >n>[g|m|k]  
The maximum value of the permanent generation, the size of the permanent will not exceed this value.     Java applications should specify that both values become the same value, because the adjustment of this value causes the full GC.    If the Java heap size mentioned above, the size of the young generation, and the permanent generation are not specified, then the JVM will calculate itself based on the situation of the application. Garbage collection is triggered when the object cannot be allocated in any space within the young generation, old, and permanent generations, and it is important to understand this and to optimize it later. The minor GC is triggered when young generation does not have enough space to allocate Java objects.     The minor GC is more ephemeral than the full GC. Once an object has survived a certain number of minor GC, it is transferred to the old generation (the object has a "term threshold" concept, and is introduced when the delay is optimized). The HotSpot VM triggers the full GC when the old generation does not have enough space to place the object. In fact, when the minor GC is found that there is not enough space for an old generation of objects to be transferred, it will trigger FULLGC, which would have a smaller cost than the minorgc of discovering that the object's movement failed and then triggered the FULLGC.    When the space of permanent generation is not enough, it will trigger FULLGC. If the FULLGC is triggered by the old generation, the space of the old and permanent generations will be garbage collected, even if the permanent generation space is not full. Similarly, if the FULLGC is triggered by the permanent, the old and permanent spaces will be garbage collected, even if the old generation's space is not full. In addition, young generation is also garbage collected, unless the-XX:+SCAVENGEBEFOREFULLGC option is specified, and-XX:+SCAVENGEBEFOREFULLGC turns off the garbage collection of young generation when FULLGC is turned off.starting point for heap size optimizationIn order to optimize Java heap size, an appropriate starting point is important. The scenario described in this section is to start with a larger Java heap than the application requires.    The purpose of this step is to collect some initialization information and the data needed to further optimize the Java heap size. As mentioned in the "Select JVM Runtime" subsection, the Throughput garbage collector (throughput garbage collector) starts.    Remember, use the throughput garbage collector by setting the-XX:+USERPARALLELOLDGC command-line option, if you are using this option that the Hotspot VM does not support, then use-XX:+USERPARALLELGC. If you can accurately estimate the Java heap space that your application needs to consume, you can set-XMX and-XMS as the starting point for this step.    If you do not know what value to set, let the JVM choose it, anyway, will be based on the actual situation to optimize the adjustment. The "GC Optimization Fundamentals" in front of how to monitor GC logs have been described. The GC log shows the size of the Java heap in use. The initialization and maximum heap size can be viewed through-xx:+printcommandlineflags. -xx:+printcommandlineflags prints out the initial and maximum values selected when the Hotspot VM is initialized, such as-xx:initialheapsize=<n>-xx:maxheapsize=<m    Here n represents the initialized Java heap size value, and m represents the maximum value of the Java heap.    Regardless of whether you specify the size of the Java heap or use the default size, you must have the application in a stable state, and you must have the ability and the means to keep the application in the same state as the line is running stably. If you are observing outofmemoryerror in the garbage collection log when attempting to get the application into a stable state, be aware of an old generation overflow or permanent overflow. An example of the following old generation overflow:[HTML]View Plaincopy
    1. 2012-07-15t18:51:03.895-0600: [Full gc[psyounggen:279700k->267300k (358400K)]
    2. [paroldgen:685165k->685165k (685170K)]
    3. 964865k->964865k (1043570K)
    4. [pspermgen:32390k->32390k (65536K)],0.2499342 secs]
    5. [Times: user=0.08 sys=0.00, real=0.05 secs]
    6. Exception in thread "main" Java.lang.OutOfMemoryError:Java heap space
 The important part of the above is highlighted, because the use of the throughput garbage collector, old generation statistics are marked as Paroldgen. This line represents the space occupied by the old generation at the time of FULLGC. From this result, it can be concluded that the old generation of space is too small, because the FULLGC and the old generation of occupied space and allocated space is basically equal, so the JVM reported OutOfMemoryError. Compare, through Pspermgen this line can see permanent generation of space Occupy is 32390K, and his capacity (65536K) than still have a certain distance. The following example shows an example of a outofmemoryerror occurrence caused by too little permanent[HTML]View Plaincopy
    1. 2012-07-15T18:26:37.755-0600:&NBSP;[FULL&NBSP;GC&NBSP;&NBSP;
    2.   [psyounggen: 0k->0k (141632K)]  
    3.   [paroldgen: 132538k->132538k (350208K)]  
    4.   32538k->32538k (491840K)   
    5.   [pspermgen: 65536k->65536k (65536K)],  
    6.   0.2430136 secs]  
    7.   [times: user=0.37 sys= 0.00, real=0.24  secs]  
    8.   java.lang.outofmemoryerror:  permgen space  
 As above, the key line is marked out, through the Pspermgen this line can be seen in FULLGC, his space occupancy and his capacity is the same, can be concluded that the permanent generation of space bar small, which led to the OutOfMemoryError.    In this example, old's footprint (132538K) is much smaller than his capacity (350208K). If OutOfMemoryError is observed in the garbage collection log, try to extend the Java heap size to the 80%~90% of the physical memory. In particular, it is important to note the outofmemoryerror caused by heap space and the need to increase space. For example, increase the value of-XMS and-xmx to resolve the old generation of OutOfMemoryError, add-xx:permsize and-xx:maxpermsize to solve permanent generation caused outofmemoryerror. Remember that the capacity of the Java heap is limited by the hardware and whether a 64-bit JVM is used.    After expanding the size of the Java heap, check the garbage collection log until there is no outofmemoryerror. If the application runs in a stable state without outofmemoryerror, you can go to the next step and calculate the size of the active object.calculate the size of the active object
As mentioned earlier, the size of the active object is the size of the Java heap that is consumed by long-lived data when the application is in a stable state of operation.    In other words, the stable operation of the application is, after FULLGC, the space size of the old and permanent generations.   The size of the active object can be viewed through the garbage collection log, which provides some optimization information, as follows: 1. The Java heap space occupied by the old generation of the application is in a stable state of operation.    2, the application is in stable operation state, permanent generation Java heap space occupies the quantity.    To ensure accurate evaluation of the active object size of the application, it is best to look at the size of the Java heap space after several FULLGC to ensure that the FULLGC is in a state where the application is running stably. If the application does not occur FULLGC or the number of FULLGC occurs rarely, in the performance test environment, the Java Monitoring tool can be used to trigger FULLGC, such as the use of VISUALVM and jconsole, these tools in the latest JDK Bin directory can be found,    The VISUALVM integrates a JCONSOLE,VISUALVM or jconsole with a button that triggers a GC. In addition, the Jmap command can be chosen to force the hotspot VM to FULLGC. Jmap requires the-histo:live command option and the JVM process ID. The process ID of the JVM can be obtained through the JPS command. For example, the process ID of the JVM is the 348,JMAP command used to trigger FULLGC can be written as follows:[HTML]View Plaincopy
    1. $ jmap-histo:live 348
Jmap not only triggers FULLGC, but also generates a heap of summary information about the allocation of objects. However, for the purposes of this step, you can ignore the resulting heap profile information.Initialize Heap size configuration    This section describes how to use the size of the active object to determine the size of the initialized Java heap. The following figure shows the size of the object to which the application survives. It is wise to collect more FULLGC information and have more information to make better decisions.     Information about the size of the active object allows you to make decisions about the size of the Java heap, and to estimate the worst-case delays.      The general rule is that the initialization and maximum values of the Java heap size (as specified by the-XMS and-XMX options) should be 3 to 4 times times the size of the active object of the old generation.      In the FULLGC information displayed in, the size of the old generation after FULLGC is 295111K, which is almost 295M, that is, the size of the active object is 295M. Therefore, the recommended initialization and maximum value for the Java heap should be 885M to 1180M, which can be set to-xms885m-xmx1180m. In this example, the Java heap size is 1048570K approximately 1048M, within the recommended value range.      Another general rule is that the initial and maximum values of the permanent (-xx:permsize and-xx:maxpermsize) should be permanent 1.2 to 1.5 times times the size of the active object. See in the FULLGC after the permanent generation occupy space is 32390K, almost 32M. Therefore, the recommended size for the permanent generation is 38M to 48M, which can be set to-xx:permsize=48m-xx:maxpermsize=48m (1.5 times times). In this example, the space size of the permanent is 65536K or 64M, which is 17M, but in the 1G memory system, this value is fully tolerated.      Another rule is that young generation space should be 1 to 1.5 times times the size of the old generation's active object. So in this example, Young's size can be set to 295M to 442M. In this example, Young's space size 358400K, almost 358M, in the middle of the recommended values.      If the recommended Java heap's initial and maximum values are 3 to 4 times times the active object size, and Young's recommendation is only 1 to 1.5 times times, then the old generation space should be 2 to 3 times times the size.      Through the above rules, the Java commands we can use can be this:[HTML]View Plaincopy
    1. <span style="FONT-SIZE:14PX;" > java-xms1180m-xmx1180m-xmn295m -xx:permsize=48m -xx:maxpermsize=48m</span >
Some other considerationsThis section will mention some additional points to keep in mind when applying a memory footprint assessment. First, it's important to know that the size of the Java heap is just evaluated, not all the memory occupied by the Java application, and can be viewed by the top command or by the Task Manager under Windows if you want to see all the Linux applications occupied by the Java application. Although the size of the Java heap can make the greatest contribution to the memory footprint of Java applications. For example, in order to store the thread stack, the application needs additional memory, the more threads, the more memory is consumed by the thread stack, the deeper the method calls, the more the line stacks. Additionally, the local library needs to allocate additional memory, and the I/O cache requires additional memory.    The memory consumption of the application needs to be evaluated to the point where any one of the applications consumes memory. Remember, this step does not necessarily meet the needs of the application memory consumption, if not satisfied, back to see whether the requirements are reasonable or modify the application.   One way to do this is to modify the application to reduce the allocation of objects, thus reducing memory consumption. The size calculation of the Java heap is just the beginning and may be modified in the following optimization steps, as required.

Step-by-step optimization of JVM Four: Determining the size of the Java heap and memory footprint

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.