Java Tuning for JVM and thread memory analysis

Source: Internet
Author: User
Tags xms

These days because of the development of a Web site in the 768M memory of the machine can not afford to run more than 100 users, because each user enabled features, the system will assign each user 8 or so independent threads, my article http://www.mzone.cc/article/311.html also introduced. The problem that often arises on small-memory machines is the Cann ' t allocate memory and OUTOFMEMORYERROR errors, which are analyzed from the JVM's RAM structure. In the JVM memory tuning process, the parameters that we often use are:

    1. -XMS the memory allocated at the start of the JVM, such as-xms200m, which represents the allocation of 200M
    2. -XMX is the maximum amount of memory allocated during the JVM run, such as-xms500m, which means that the JVM process can only consume up to 500M of memory
    3. -XSS the memory size allocated for each thread started by the JVM, which is 1M in the default JDK1.4 in 256k,jdk1.5+

The general JVM appears cannt ' Allocate memory error is that the machine is not enough RAM, resulting in the system can not allocate a given memory for the JVM, this is not prominent at startup, so it is set in the startup parameters-xms to specify; OutOfMemoryError errors usually occur after the system runs for a period of time, most of it is not enough machine memory or the JVM itself memory space has been exhausted, it will be adjusted according to the situation, if the JVM itself ran out of memory space, You need to adjust the-XMX parameter to classify the JVM's available memory, or to increase the memory or tune the program if the machine is not in enough memory.

The above two parameters are primarily to set the JVM's minimum available memory and the maximum available memory, which is a process-level memory control. For threads in Java, my previous understanding has been to use the JVM's memory directly in Java when new threads are used, but this is not the case. In Java each thread needs to allocate thread memory to store its own thread variables, in jdk1.4 each thread is 256K of memory, in jdk1.5 each thread is 1M of memory, jdk1.6 is not very clear, and the estimate is 1M. Every new thread in Java, the JVM requests a new local thread to the operating system, and the operating system uses the remaining memory space to allocate memory to the thread instead of using the JVM's memory. Thus, when the operating system has less available memory, the less new threads the JVM can create, for example:

Total Memory -xms -xmx -xss Spare Memory Jdk Thread Count
1024M 256M 256M 256K 768M 1.4 3072
1024M 256M 256M 256K 768M 1.5 768

The above table is just a rough estimate of the maximum number of threads that can be created in Java under certain memory conditions. As-XMX increases, the number of idle memory is less, and the number of threads that can be created is even less, and with different versions of JDK1.4 and 1.5, you can create threads that vary depending on the memory size of each thread.

In fact, as long as we understand the JVM's memory size designation and the memory model of the thread in Java, we can basically control how to use threads in Java and avoid memory overflow or error problems.

Category: Databases and servers

Java Tuning for JVM and thread memory analysis

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.