Basic resources for performance optimization cpu& memory (JVM)

Source: Internet
Author: User

This chapter mainly introduces some basic resources of the computer and some basic resources after the operating system processing.

Mainly include

    • Cpu
    • Memory
    • Disk
    • Internet
    • Thread

This chapter describes some of the principles of these resources, and describes how to view the number of resources, usage, and some of the effects on performance and overall computer execution.
Many of the content in this chapter is based on Linux, not a special description, but a case for Linux. may not be applicable in other operating systems.

There is also a special explanation for some of the content on top of the JVM, because a lot of the accumulation is on top of the JVM, and the internal system is basically built on the JVM.

    • JVM directives
    • JVM Memory Usage
Cpu

The CPU is the most important resource in the computer, not one, and in the field of performance optimization, is also the most need to understand the content.

In the code execution aspect, the CPU executes the final instruction of the code, dispatches the resources, the operation memory, has the big influence to the completion one task each aspect.

First, the basic instructions for viewing CPU information in Linux are to look at the hardware information of the CPU

Cat/proc/cpuinfoo

You can see the number of cores, the frequency of a single core, and some of the features supported by the CPU.

The other most exposed content is the load average triad, which will be reflected in many commands, W,top,uptime, and so on.

Represents the last 1, 5, and 15 minutes of CPU usage stress, respectively.

About CPU usage%, and CPU load, is two similar and different concepts, in Linux, cpu% also has different interpretations, specific differences, see below explanation.

CPU Usage%

    • The topmost part of top
    • US overall user-state CPU usage%
    • Sy nuclear mentality
    • WA CPU Wait, here is mostly waiting IO
      • Iowait is due to the CPU processing request in addition to iowait there is no other things can be done, or all the threads, if the runable state is doing IO operation
    • ID Idle
    • The top line to see the cpu%, is 1 CPU corresponding to 100%, the above said several values are all CPU corresponding 100%, so in the multi-CPU, you will see the thread is more than 200%, and the above data is still normal

Load

    • The main concern is the length of the queue waiting to be executed
    • Because the waiting queue is long, the context switch is also more
      • Context switches can see the CS section of Vmstat
        • The use of the general Vmstat tool is done by two numeric parameters, the first parameter is the number of time intervals sampled, the unit is seconds, the second parameter is the number of samples, such as:
        • Specific vmstat parameters Please Google, or degree Niang details, here do not introduce
    • In addition if the CPU is used and the load is out of sync, you need to pay attention to the task large size.
      • If the task is too small, it will cause load "cpu%
      • Great job, cpu% "load
    • The value of load is also to see the number of cores of the CPU, if the number of load <=cpu, indicating a period of time, no task is to wait for the CPU resources to start processing
Memory

Memory management

Here we mainly introduce the memory management inside the Java Virtual machine.

JVM-related memory is mainly divided into the following sections

    • Method Stacks & Local method stacks
      • Magnifying the call stack
    • Method area
      • Perm area, class information metadata, static objects, etc.
    • Heap
      • A variety of objects, new out of something
    • Native Memory (C Heap)
      • The Directbuffer of NiO
      • Jni
      • Results of Compile JIT

The key part of the heap, according to the current popular usage is divided into

    • Cenozoic
      • Eden
      • S0
      • S1
    • Old age

With Java parameters,-xmn can adjust the size of the new generation and the old age
-xx:survivorratio can adjust the size of Eden and S0s1 s1=s0=mn/(survivorratio+2)
Number of-xx:maxtenuringthreshold objects surviving in the Cenozoic

-xx:maxpermsize can adjust the size of the perm area
-XSS can adjust the size of the method stack
-xx:maxdirectmemorysize can adjust the size of native memory

There are exceptions to the general strategy for survival of objects in Eden and S0 and in the old age.

    • New when Eden,eden full, do a YGC
    • The remaining objects are placed in the S0, emptying Eden
    • Continue new, when the Eden,eden full, do YGC and S0 do together
    • The remainder of the object is S1, this time, if there are objects in S0 or S1 more times than Maxtenuringthreshold, this object is placed in the old area
    • If the S1 is full, all objects to the old area
    • If the old area is full, do FULLGC.
    • Loop in turn

From the use of the most satisfactory way is that after a few rounds to the old area after the promotion, old area tends to be unchanged. The old area can hold objects such as cache objects and so on, which require long-term survival for container-related objects.
The Eden and S0 areas are large enough for the target to survive in the new generation no more than maxtenuringthreshold.

The basic approach is to design objects in 2 directions from the beginning, either for long-term survival or for short-term survival.
Reasonable design new Laosheng generation size.

Memory optimization, in fact, is mainly reflected in the use of CPU time, including object creation, old object address lookup, object garbage collection, we can do is to minimize the creation of objects, as well as as little garbage collection, for the old object address search, there is no too many methods.

Memory leaks

Memory leaks, mainly related to garbage collection methods, now popular method is through some gcroot, to find all the objects in use, save, and other objects are GC off.

The general gcroot has the following types of objects

    • The currently surviving thread
    • The class of the current load
    • Method parameters and local parameters on the surviving thread stack
    • JNI-related global information, as well as method parameters on the relevant stack, and local parameters
    • Monitor object used when synchronizing
    • Some reserved objects for the JVM

If there are objects and these related content has not been out of the relationship, it is possible to cause memory overflow.

A few typical cases

    • An app uses a static map on a class as a cache, and key gets more and more, and then it overflows.
    • An application needs to create a type dynamically, and if the object is created using the same classloader, it will eventually result in more classes of load, and overflow in the perm area.

Need to differentiate between memory overflow and memory usage too quickly, and the associated conversion process

The primary way to view configurations

With the following command, you can see the configuration you want, and if there are other parameters that are configured on-line, add the parameters directly.

java-server-xmx128m-xx:+printflagsfinal | grep maxheapsize

JVM directives

After the Java code is translated into class, it becomes the specific format that the JVM can recognize, and you can refer to the JVM Virtual machine specification.

Several recent versions can be obtained here, this address is also wrapped in the Java language Specification, the 2 difference is that one is about how to write Java Virtual machine, one is to describe how to write Java code.

http://docs.oracle.com/javase/specs/

Aside from the Jit,java, it is a step-to-step implementation of the JVM directive.

The JVM does not have any requirements for the pre-compilation language, and now there are many other languages based on the JVM's syntax, such as Scala,groovy,jruby and so on.

ASM is a tool that knows about JVM directives, or the class file format.

When the JVM loads the class, it provides some functionality that allows the human to modify the class object, or the byte stream that the class eventually loads into the JVM.

By using ASM to modify the class byte stream, we modify the behavior that class actually executes, and achieve our goal.

A few simple examples

    • By parsing class, adding code before and after a particular class of methods, the beginning of the method records a current time, and at the end of the method, a comparison between the current time and the previously recorded time is a simple execution time profiler
    • By monitoring the constructor methods of a class, you can find all of the classes and the number of child classes created
    • By returning the parameters and results of a call to a class method, you can record some specific parameters and results of the method for debug, or to confirm that the method has been called.
    • By intercepting the beginning of the method, directly returning the desired result is a call to the mock frame

"~ ~ ~ Next More Wonderful ~ ~ ~"

Basic resources for performance optimization cpu& memory (JVM)

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.