Optimize Java Heap size 5 tips

Source: Internet
Author: User

Summary:Java does not have enough heap size that could result in a very large performance impact, which will undoubtedly give the necessary programs and does not cause trouble. This article summarizes the ability to influence the top five bits of Java, and neatly stacks the optimization?

The author Pierre has a 10 senior system architect with years of experience, and his main areas of expertise are Java EE, middleware, and JVM technology. Based on his years of working experience, he found that many performance problems were caused by insufficient Java heap capacity and tuning.

Below he will share with you a very useful 5 Java heap optimization techniques.

1.JVM: Fear of things that are difficult to understand

Never think, through configuration, tuning. You will be able to rule out problems that you are not clear about.

Some people think that Java program apes do not need to know the internal JVM memory management.

There is no doubt that such a view is obviously wrong, assuming that you want to broaden your knowledge and improve your ability to troubleshoot. You will need to learn and learn about JVM memory management.

Java heap tuning and troubleshooting is a challenging task for Java or Java EE novices.

Some typical case scenarios are provided below:

The client environment faces a regular outofmemoryerror error and has a very large impact on the business.

Your development team is going to have to solve the problem at such a great pressure. What do you usually do?

    1. Use Google search engine to find similar problems and you will believe (or if) you are facing the same problem.

    2. You'll catch jvm-xms and the examples of keyword that exist with OutOfMemoryError anomalies, and then hope to solve the client problem at high speed with this case.
    3. Finally, you will use the same tuning method in your environment.

      Two days later, the problem still occurs (even worse or slightly better) ...

What is wrong with it?

First of all. Not figuring out the root cause of the problem? The development environment is not properly understood in the deep surface (specifications, load conditions, etc.). Web Search is an excellent learning method and knowledge sharing tool. But you have to combine your own actual project. Fundamentally analyze and solve the problem.

There may be a lack of major JVM and JVM memory management skills to prevent you from connecting all the dots.

The first tip of the day is to help you understand the main JVM principles and their unique memory space.

All this knowledge is of considerable importance. It can help you to make effective tuning strategies, better predict the impact of the future, and know in advance which tuning work you need to do in the future.

Take a look at the JVM Reference guide:

JVM memory is divided into 3 memory spaces

    • Java Heap: Suitable for all JVM vendors, often used to split Younggen (seedlings) and Oldgen (lifetime) space.

    • PermGen (permanent): for Sun HotSpot VMS (PermGen space will be removed in Java7 or Java8 update)
    • Native Heap (C-HEAP): Applies to all JVM vendors.

It is recommended to read the following articles, preferably by downloading and reading the Sun's Java Memory Management white paper and OPENJDKS implementation.

    • Sun HotSpot VMS
    • IBM VMS
    • Oracle JRockit VMS
    • Sun (Oracle) –java Memory Management white Paper
    • Openjdk–open-source Java Implementation

As you can see, JVM memory management is more complex than setting the maximum value using XMX.

You need to look at each angle, including local and permgen requirements, and view physical memory availability (CPU core) from the host.

In the larger Java heap and the smaller local heap races. 32-bit virtual machines can become quite tricky.

An attempt was made to set up a large heap on a 32-bit VM such as 2.5gb+. This exception is thrown by adding outofmemoryerror based on factors such as application occupancy and number of threads. A 64-bit JVM solves the problem, but physical resource availability and garbage collection costs are still limited (the cost is mainly concentrated on GC size collection).

The maximum does not mean the best, so please do not be able to execute 20 Java EE applications on a 16GB 64-bit virtual machine.

2. Data and Applications King: recall the static occupancy requirements

The application and related data will determine the Java heap footprint requirements.

With static memory, the following memory requirements can be "predicted":

    • Determine how many different applications will be deployed to a single, pre-planned JVM process, such as how many ear files, war files, jar files, and so on. The more applications deployed on a single JVM, the more demands are on the native heap.
    • Determine how many classes need to be loaded at execution time: contains third-party APIs.

      The more class loader and classes are loaded at execution time, the higher the demand for hotspot VM PermGen space and internal JIT-related optimized objects.

    • Determine the data cache consumption. such as an application loading internal cache data structures (and third-party APIs), such as data caches in a database, reading data from a file, and so on. The more data caches are used, the higher the Java Heap oldgen space requirements.
    • Determine the number of middleware threads that you agree to build. This is important because Java threads require enough native memory. Otherwise, the OutOfMemoryError exception will be thrown.

The more applications deployed on the JVM process, the higher the requirement for local memory and PermGen space. The data cache is not serialized as a disk or database, it will require additional memory from the Oldgen space.

Trying to make a reasonable assessment of the static memory footprint, it is useful to set some JVM capabilities starting point before a real data test.

For 32-bit JVMs, it is generally not recommended that a Java heap size exceed 2 GB (-xms2048m,-xmx2048m), and for Java EE applications and threads this will require sufficient memory and native heap PermGen.

This assessment is very important because too many applications deployed on a 32-bit JVM process are very easy to cause native heap exhaustion, especially in multi-threading environments.

For 64-bit JVMs. A 3GB or 4GB Java heap/JVM process is the recommended starting point.

3. Business flow Setup rules: Review dynamic memory footprint requirements

Business traffic typically determines dynamic memory consumption. By observing various monitoring tools, it is possible to discover the JVM GC "Heartbeat" generated by concurrent users and requests. This is because of frequent creation and garbage collection of short or long term objects.

A typical 32-bit Jvm,java heap size set at 2 GB (using generational & concurrent collectors) typically allocates space and 1.5 GB of oldgen space for up to three MB Younggen.

Minimizing the frequency of significant GC collections is a key factor for optimal performance, so it is important to understand and assess how much memory is needed at peak times.

Again, the application type and data will determine the memory requirements. The type of application for the shopping cart (long-lived objects) involves both large and non-serialized session data, which typically requires a large Java heap and very much oldgen space.

Stateless and XML processing (very many short-lived objects) heavy applications need to younggen space appropriately. To minimize the frequency of the main set.

Like what:

You have 5 ear applications (more than 2000 Java classes) to deploy (including middleware code)

    1. The local heap requirement is expected to be 1GB (must be large enough to handle thread creation, and so on.) The PermGen space is approximately MB.
    2. Internal static cache approx. 500MB
    3. At peak times. Total pre-measured traffic is 5,000 concurrent users
    4. Session data for each user is approximately 500K
    5. During peak periods, the total traffic session requirement is 2.5GB.

As you can see, in this case, the 32-bit JVM process is not sufficient.

A typical workaround is to perform traffic splitting on several JVM processes or physical hosts (if sufficient hardware and CPU cores are available).

Most of the time, business traffic will drive memory consumption.

Unless you need a lot of data caching to achieve the right performance, a typical portal application site (media) is a heavy application requirement. When the data cache is too much, it should be labeled with a yellow flag. It's better to take a look at some design elements earlier.

4. Tailoring

This article, you should do:

    1. Understand the main JVM principles and memory space.
    2. Have a deep understanding of all applications and their characteristics (size, type, dynamic traffic, stateless objects vs stateful objects, internal memory caches, etc.).

    3. A good point of view for pre-measured traffic (concurrent users) is for each application-assuming you need a 64-bit virtual memory, which will be set to start.

Assume that multiple JVM (middleware) processes are required.

Wait a minute. This is not enough. Although the information above is critical, and the settings for the Java heap are "best pushed". Simulate the behavior of the application and perform appropriate analysis, load, and performance tests to verify the Java heap memory requirements.

Recommended Jprofiler tool for everyone, the best way to learn how to use a parser is to properly understand the memory footprint of your application. Another way is to use the Eclipse mat tool to perform heap dump analysis on an existing environment.

Heap dumps are powerful and allow you to view and understand the entire memory footprint of the Java heap, including class loader-related data and what is required in memory footprint analysis, especially memory leaks.

The Java parser and heap dump analysis tools agree that you understand and validate the application memory footprint. This includes the detection and resolution of memory leaks.

Load testing and performance testing is essential, and by simulating concurrent users to verify that an early assessment is correct, it exposes the application bottleneck and agrees to fine-tune it. Recommend a very easy to get started tool: Apache Jmeter.

Finally, we will look at this situation. The application is normal in the Java EE Environment until one day a completely normal device fails to start, such as a hardware problem.

What happened to the sudden decline in environmental performance and overall environmental degradation?

There are many reasons for the domino effect, but the lack of JVM tuning and the ability to handle failover (short-term additional load) is not common.

Assuming the JVM process executes in 80% + Oldgen space capacity and frequent garbage collection, how do you anticipate a failover scenario?

Before simulating the load and performance test should simulate this scenario, adjust your tuning settings so that your Java heap has enough buffering to handle the extra load (additional objects) in the short term.

This applies primarily to dynamic memory consumption because failover means that some fixed concurrent users will be redirected to the available JVM processes (middleware instances).

5. Divide and conquer

The premise of this article is that you have completed dozens of load tests. The JVM is no longer leaking, and your application's memory cannot be degraded any further. You have tried several tuning strategies. For example, use a 64-bit Java heap space above 10GB. Multiple GC policies, although this still does not find a suitable acceptable level of performance?

Compared to the current JVM specification. appropriate vertical and horizontal scaling. Contains the JVM processes on each physical host and across multiple hosts to meet the overall throughput and capacity. Assuming that the application list is broken in several logical silos, their own JVM processes, threads, and tuning values, the fault tolerance of the IT environment will be more powerful.

The "Divide and Conquer" policy contains split application traffic to multiple JVM processes, and here are some of the split tips:

    1. Reduces the Java heap size (static and dynamic occupancy) for each JVM process
    2. Reduce JVM tuning complexity.
    3. Reduces GC churn and pauses each JVM process
    4. Adding redundancy and failover capabilities
    5. Align the latest cloud and it virtualization strategy

When you find that you've spent a lot of time tuning on 64-bit JVM processes, it's time to take a good look at your middleware and JVM deployment strategies and take advantage of vertical and horizontal scaling. Many of the realizations of this strategy need to support other hardware, but in the long run. This is very effective and intentional. (Zhanglunan/series)

Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

Optimize Java Heap size 5 tips

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.