System optimization How to do the-JVM optimization

Source: Internet
Author: User
Tags cpu usage high cpu usage visualvm

Hello everyone, here is "chat system optimization" and synchronize updates at the following address

    • Blog Park: http://www.cnblogs.com/changsong/
    • Knowledge Column: Zhuanlan.zhihu.com/youhua
    • Think no column: Segmentfault.com/blog/youhua
    • Full network reverse telecommuting, free subscription: http://www.zsihuo.com

Here I will be based on the Java EE System and the Internet architecture, to talk about the various aspects of system optimization, dry, welcome to subscribe and attention!

Objective

In the previous issue of Tomcat optimization, the main parameters of the JVM have been given a certain description, this phase of the main introduction of some concepts and experience. The following chapters describe the basic use of the relevant tools.

Optimize priority levels

Overall, the system optimization should first optimize the architecture and code to solve the specific function point efficiency problem. Finally, through the JVM monitoring tool to find some hidden deeper problems.

Related situations
    • Memory consumption and increasing, system pressure is large, full GC frequent, the system appears in the card Dayton
    • Threads have large waits and deadlocks, high CPU usage, slow system response
    • Heap is out of memory or class loading results in JVM Crash, System downtime
When this happens, you have to use tools to analyze the JVM to determine the problem
JVM Memory model

JDK1.7 and below

JDK1.8, PermGen replaced with VM Metaspace
Heap Domain
    • Globally shared by all threads
    • Save all objects and collection objects
Method field
    • Globally shared by all threads
    • The structure definition of all classes contains properties, methods and constructors, etc.
Thread1.n
    • Local private stack, one thread, one stack
    • Keep all object references in the heap (4byte length)
    • Store local local variables (underlying data type), program run state, method return value
Classification of memory leaks
    • Heap memory leaks-more common
    • Persistent memory leaks
    • Stack memory leaks
    • System resource memory leaks-more common
Thread-related knowledge

JVM Thread State Migration

Thread state
    • Initialization (NEW): Initial creation, after launch, into executable state
    • Executable State (Runnable): Whenever CPU time is obtained, execution begins
    • Running state (Running): Using CPU execution
    • Blocking state (bloked)

      • Waiting for blocking (wait)
      • Synchronous blocking (synchronized)
      • Sleep Blocking (sleeping)
      • Join blocking: Waits for the join child thread to finish before the main thread can execute, merging the asynchronously executed thread into a synchronized thread
    • End state: Thread execution Complete or exit abnormally
performance monitoring attention points
    • Total System Threads
    • Deadlock threads require priority resolution
    • Total number of thread bloked

      Thread bloked Many cases, consider processing data for sharding, multi-channel, multithreading to improve system performance
      If the system processing is slow, but the CPU consumption has been very low, it is necessary to comb the system processing process, serial processing of the parallel processing, parallel processing process to improve concurrency to solve.
Thread deadlock

When two or more threads try to acquire locks for other resources, each thread is trapped in an infinite wait for other resource locks to be released (waiting for each other) unless a user's process is terminated.

Several deadlock scenes
    • Two threads call Thread.Join () each other, causing each other to wait for the end of synchronization.

use thread join operations with caution
    • When two threads use nested synchronization blocks, one thread consumes the required locks on another thread, and when it waits for each other to be blocked, a deadlock can occur. It is also possible for multiple threads to form a ring lock, such as thread A waits for thread B, thread B waits for thread C, and thread C waits for thread A. Thread A in order to detect deadlocks, it requires a progressive detection of all locks requested by B. Starting with the lock requested by thread B, thread A finds thread C and discovers that the lock requested by thread C is held by thread A. This is what it knew happened to be a deadlock.

    • MySQL Deadlock

MySQL two threads simultaneously read and write to two records
Avoid deadlocks
    • Security status
find a sequence of assigned resources to make all processes complete successfully
    • Banker algorithm
use a pre-allocation policy to check if the system is in a secure state when the assignment completes
Deadlock detection
    • VisualVM (or other tools)
Monitor thread status, if a deadlock occurs to get the relevant code location
    • Deadlock lock physical and chemical resource allocation diagram
analysis of the existence of deadlock using dead lock physical and chemical resource allocation graph
See the following information:
Www.coursera.org/lect ...
Summarize

This chapter focuses on some of the core knowledge, mainly to let you understand the system optimization in the end to optimize and solve what problems, what is the goal of optimization. Subsequent chapters will cover the use of Tomcat's JMX configuration, Visualvm,tprofile and other tools.

Study Questions

The state of the JVM threads, which are mostly in what state distribution, we can say that the system is running healthy.

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.