In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
1. The organizational structure of jvm (1) the relationship between jvm and system calls
The corresponding nouns of explain:
-classloader: loads the required class into jvm memory when jvm starts or when the class runs
-execution engine: responsible for executing byte instructions contained in class files
-memory area: the memory area that manipulates lock allocation while jvm is running. Runtime memory is divided into five parts: heap, method area, stack, local method stack, program counter
-Local method API: mainly calls local methods implemented by c or C++ and returns results.
Memory structure of (2) jvm
The memory structure of jvm consists of three main blocks: heap memory, method area, and stack.
heap memory: the largest piece of jvm is made up of the younger generation and the older generation, while the younger generation is divided into three parts: Eden space (for the location where the relevant was just created), From Survivor () space, and To Survivor (storage area for inventory objects). By default, the younger generation is allocated according to the 8:1:1 ratio.
method area (permanent generation): stores information, constants, static variables and other data, is the thread sharing area, in order to distinguish from the java heap, the method area also has an alias Non-Heap (non-heap) jvm function.
stack: it is divided into java virtual machine stack, local method stack and program counter, which is mainly used for method execution.
(3) Regulation of parameters in each region
Settings for heap memory:
-Xms: set the minimum space size of the heap-Xmx: set the maximum space size of the heap-XX:NewSize sets the minimum space size of the new generation-XX:MaxNewSize: set the maximum space size of the new generation-XX:PermSize sets the minimum space size of the permanent generation-XX:MaxPermSize sets the maximum space size of the permanent generation-Xss sets the stack size of each thread without directly setting the parameters of the old era However, two parameters, heap space size and Cenozoic space size, can be indirectly controlled: old age space size = heap space size-younger generation large space size II, the role of each memory region of jvm (1) Overview of each area: shared area method area private area of heap thread java stack local method stack program counter (2) java stack (heap):
for most applications, the Java heap (Java Heap) is the largest block of memory managed by the Java virtual machine. The Java heap is an area of memory shared by all threads and is created when the virtual machine starts. The sole purpose of this memory area is to store object instances, where almost all object instances allocate memory.
The java heap is the main area managed by the garbage collector, so it is often referred to as the GC heap. From the perspective of memory recovery, because the current collectors basically use generation-by-generation collection algorithms, the java heap can also be divided into the new generation and the old age; in a more detailed division: the new generation also includes: Eden space, From Survivor space, To Survivor space.
throws an OutOfMemoryError exception if there is no memory in the heap to complete instance allocation (there is no memory in the heap to hold objects) and the heap can no longer be extended.
Supplement (the process from newborn to old age): in most cases, objects are allocated in the Cenozoic Eden area. When there is not enough space in the Eden area to allocate, the virtual machine will initiate a Minor GC, and the object will enter the survivor zone and enter the old age when the object meets some conditions.
-long-lived objects will enter the old age: the virtual machine defines an object age (Age) counter for each object. If the object is born in Eden and is still alive after the first Minor GC, and can be accommodated by Survivor, it will be moved to Survivor space and the age of the object will be set to 1. Each time an object "survives" a Minor GC in the Survivor area, its age increases by 1 year, and when its age increases to a certain extent (the default is 15 years old), it will be promoted to the old age. The age threshold for the promotion of an object can be set by parameter-XX:MaxTenuringThreshold.
-if the sum of all objects of the same age in the Survivor space is more than half the size of the Survivor space, objects older than or equal to that age can go straight into the old age without waiting for the age required in MaxTenuringThreshold.
-the virtual machine provides a-XX:PretenureSizeThreshold parameter that allows objects larger than this setting to be allocated directly in the old days. The goal is to avoid a large amount of memory replication between the Eden area and the two Survivor zones.
(3) method area (method area):
-method zone, like java, is a shared memory area of each thread. It is used to store data such as class information, constants, static variables, and code compiled by the compiler loaded by jvm.
The jvm specification has very loose restrictions on this area, and you can choose not to implement garbage collection in addition to not requiring contiguous memory and optionally having a fixed size like the java heap. Relatively speaking, garbage collection behavior is relatively rare in this area, where not the data into the method area will exist "forever". The goal of memory recovery in this area is mainly aimed at constant pool recovery and type unloading. Generally speaking, the recovery "results" of this area are not satisfactory, especially type unloading, and the conditions are very harsh. But the recovery of this part of the area is really necessary.
according to the Java virtual machine specification, an OutOfMemoryError exception will be thrown when the method zone cannot meet the memory allocation requirements.
(4) Program counter:
program counter is a small piece of memory space, its function can be regarded as the line number indicator of the bytecode executed by the current program, the bytecode interpreter works by changing the value of this counter to select the next bytecode instruction to be executed, branch, loop, jump, exception handling, thread recovery and other basic functions need to rely on this counter.
because the multithreading of the Java virtual machine is realized by the thread switching in turn and allocating the processor execution time, at any given moment, a processor (a core for a multi-core processor) will only execute instructions in one thread. Therefore, in order to restore to the correct execution position after thread switching, each thread needs to have an independent program counter, and the counters between each thread do not affect each other and store independently. We call this kind of memory area "thread private" memory.
if the thread is executing a Java method, this counter records the address of the virtual machine bytecode instruction being executed; if the native method is being executed (using the underlying instruction when called), the counter value is Undefined.
this memory region is the only one that does not specify any OutOfMemoryError conditions in the Java virtual machine specification.
(5) java virtual machine stack:
The Java virtual machine stack (Java Virtual Machine Stacks) is also thread-private and has the same life cycle as threads. Virtual machine stack describes the memory model of Java method execution: when each method is executed, a stack frame (Stack Frame) is created at the same time to store local variables, operation stack, dynamic link, method exit and other information. Each method is called until the completion of execution, corresponding to a stack frame in the virtual machine stack from the stack to the stack process.
The local variable table stores a variety of basic data types known at the compilation time, and the memory space needed for the local variable table is allocated during compilation. When entering a method, how much local variable space needs to be allocated in the frame is completely determined, and the size of the local variable table will not be changed during the operation of the method.
exception about java virtual machine stack: 1) if the stack depth requested by the thread is greater than the depth allowed by the virtual machine, a StackOverflowError exception will be thrown. 2) if the virtual machine stack can be dynamically expanded, an OutOfMemoryError exception will be thrown when enough memory cannot be applied for.
(6) Local method stack:
methods decorated with native are stored in the local method stack. Like the virtual machine stack, the local method stack area throws StackOverflowError and OutOfMemoryError exceptions.
3. Introduction of jvm garbage collection mechanism (1) introduction of jvm garbage collection:
garbage collection Garbage Collection, often referred to as "GC", was born in MT's Lisp language in 1960. In Jvm, program counters, virtual machine stacks and local method stacks all grow and die with threads. Stack frames enter and exit the stack with the entry and exit of methods, realizing automatic memory cleanup. Therefore, our memory garbage collection is mainly concentrated in the Java heap and method area. During the running of the program, the allocation and use of this part of memory are dynamic.
(2) introduction of garbage collector (Garbage Collection):
GC is actually an automatic memory management tool behavior that consists of two steps: allocating space for newly created objects and reclaiming memory space for useless objects in the and Java heap
(3) benefits of implementing multiple GC:
The Java platform is deployed on a variety of hardware resources. Secondly, various applications are deployed and run on the Java platform, and users expect different performance indicators (throughput and latency) of different applications. In order to meet the different needs of memory management of different applications, JVM provides a variety of GC to choose from.
The main performance metrics of GC include:
-maximum pause time: the maximum application pause time caused by recycling
-throughput: ratio of garbage collection pause time to total application running time
example: once the application runs for 60s, and then the length of the GC is 2s (4 times of GC:0.5,0.8,0.2,0.5), then the maximum pause time is 0.8. the throughput is (60-2) / 60.
The types of GC are roughly as follows:
-serialized GC: suitable for applications with less memory footprint
-parallel GC or throughput GC is suitable for applications that take up a lot of memory, have more CPU and pursue high throughput.
-concurrent GC: suitable for applications with more memory and more CPU, and applications with latency requirements.
parallelism: when multiple garbage collection threads work in parallel, while the user thread is still waiting.
concurrency: means that the user thread and the garbage collection thread execute at the same time (but not necessarily in parallel and may replace execution).
(4) judgment of the survival of the object:
-reference count: each object has a reference count property, which adds 1 when a reference is added, minus 1 when a reference is released, and can be recycled when the count is 0. This method is simple, but the disadvantage is that it can not solve the problem of circular reference between objects.
-reachability analysis: search down from GC Roots, and the path taken by the search is called the reference chain. When an object does not have any reference chain to the GC Roots, it is proved that the object is unavailable and unreachable.
Reachable object: an object that can be reached by reference search through the root object.
Unreachable object: an object that is not referenced by a reference search through the root object.
In the Java language, GC Roots includes:
The class static attribute entity in the object method area referenced in the virtual machine stack refers to the objects in the constant local method stack in the object method area (5) MinorGC and Full GC:
Cenozoic GC (Minor GC): refers to the garbage collection actions that occur in the Cenozoic generation. Because most Java objects are born and wiped out, Minor GC is very frequent and the collection speed is generally fast.
Old GC (Full GC): is to clean up the entire heap space-including the younger and older or permanent generations. Full GC is generally more than 10 times slower than Minor GC.
4. Garbage collection algorithm: (1) Mark-clear algorithm:
"marking-clearing" (Mark-Sweep) algorithm is divided into two stages: marking and clearing: first, all the objects that need to be recycled are marked, and all the marked objects are recycled uniformly after the marking is completed.
Disadvantages of :
-one is efficiency, the labeling and cleaning processes are not efficient.
-one is a space problem, where a large number of discontiguous memory fragments are generated after mark removal, and too much space debris may result in not finding enough contiguous memory to trigger another garbage collection action in advance when the program needs to allocate larger objects later in the run.
(2) replication algorithm:
The collection algorithm of " replication" (Copying), which divides available memory into two equal blocks of capacity, using only one of them at a time. When this piece of memory is used up, the surviving objects are copied to the other, and then the used memory space is cleared at once.
Disadvantages of :
-the cost of this algorithm is to reduce the memory by half, while continuous replication of long-lived objects leads to inefficiency.
(3) tag collation algorithm:
tag-collation (Mark-Compact) algorithm, the marking process is still the same as the "mark-clear" algorithm, but the next step is not to directly clean up recyclable objects, but to move all surviving objects to one end, and then directly clear the memory beyond the end boundary.
(4) Generation collection algorithm:
The Generation Collection (Generational Collection) algorithm divides the Java heap into the new generation and the old age, so that the most appropriate collection algorithm can be adopted according to the characteristics of each age.
The younger generation of (which has a short life cycle and a large number of objects are junk) uses replication algorithms.
The older generation of (long life cycle, junk objects when a small number of objects) use markup, or tag cleanup.
5. Garbage collector:
if the garbage collection algorithm is the methodology of memory collection, then the garbage collector is the concrete implementation of memory collection. Virtual machine implementations vary greatly from vendor to vendor and from version to version. The collectors included in HotSpot are as follows:
(1) Serial collector (single-core server, fastest, single-threaded version):
The serial collector is the oldest, most stable and efficient collector, which may produce long pauses and use only one thread for recovery. Serial recycling is used in new generation and old age; new generation replication algorithm and old age mark-compression are used; Stop The World (service pauses) in the process of garbage collection.
The disadvantage of this algorithm is that other threads will be stopped during garbage collection.
(2) ParNew Collector (multithreaded version)
The ParNew collector is actually a multithreaded version of the Serial collector.
new generation parallel, old age serial; new generation replication algorithm, old age mark-finishing
(3) Parallel collector (multithreaded)
The Parallel Scavenge collector is similar to the ParNew collector, and Parallel focuses on the multi-threaded ParNew evolution of throughput. New generation replication algorithm, old age marking-finishing algorithm.
(4) CMS collector (multithreaded)
The CMS (Concurrent Mark Sweep) collector is a collector that aims to obtain the shortest recovery pause time. At present, a large part of Java applications are focused on the service side of the Internet website or BBUBG S system. This kind of application pays special attention to the response speed of the service, hoping that the system will have the shortest pause time in order to bring a better experience to users.
Advantages: concurrent collection, low pause
Disadvantages: a large number of space debris are generated, and the throughput of concurrent nodes is low.
Parameter configuration:
-XX:+UseConcMarkSweepGC uses CMS collector-XX:+ UseCMSCompactAtFullCollection FullGC to defragment once; the defragmentation process is exclusive and causes a longer pause time-XX:+CMSFullGCsBeforeCompaction setting does FullGC several times, then defragment once-XX:ParallelCMSThreads sets the number of threads of CMS (generally approximately equal to the number of CPU available) (5) G1 collector (multithreaded)
features:
-spatial integration: the G1 collector uses the tag demarcation algorithm and does not produce memory space fragmentation. When allocating large objects, the next GC is not triggered in advance because contiguous space cannot be found.
-G1 collector, the memory layout of the Java heap is very different from that of other collectors. It divides the entire Java heap into several equal-sized independent areas (Region). Although it still retains the concept of the new generation and the old age, the new generation and the old age are no longer physical barriers, they are both collections of part (can be discontiguous) Region.
The new generation collection of G1 is similar to ParNew, when the new generation occupies a certain proportion, it starts to trigger collection. In this way, when a certain proportion is reached, garbage collection is triggered, and those who do not use region can still be provided for external use, which effectively avoids stop the world.
Comparison of and CMS:
-Generation: in CMS, the heap is divided into PermGen,YoungGen,OldGen; and YoungGen is divided into two survivor regions. In G1, the heap is evenly divided into several region, and in each region, although the outline of the new and old generations is retained, the collector collects on a region-by-region basis.
-algorithm: compared to CMS's "mark-clear" algorithm, G1 uses the "mark-clean" algorithm to ensure that no extra fragments are generated.
-pause time can be controlled: to shorten the pause time, G1 establishes a pre-storable pause model, so that within the range of pause time set by the user, G1 will select the appropriate area for collection to ensure that the pause time does not exceed the time specified by the user.
6. Jvm parameter configuration and commonly used analysis tools: (1) jvm parameter list:
Allocation of each memory size:
Java-Xmx3550m-Xms3550m-Xmn2g-Xss128k-XX:NewRatio=4-XX:SurvivorRatio=4-XX:MaxPermSize=16m-XX:MaxTenuringThreshold=0-Xmx3550m: maximum heap memory is 3550M-Xms3550m: initial heap memory is 3550m generally:-Xmx and-XMs values are set to the same to avoid JVM redistributing memory after each garbage collection. -Xmn2g: set the younger generation size to 2G-Xss1m: set the stack size for each thread-XX:NewRatio=4: set the ratio of the younger generation (including Eden and two Survivor zones) to the older generation (excluding persistent generations). Set to 4, the ratio of the younger generation to the older generation is 4:1, and the younger generation accounts for the 4/5-XX:SurvivorRatio=4 of the entire stack: sets the size ratio of the Eden region to the Survivor region in the younger generation. -XX:MaxPermSize=16m: set the persistent generation size to 16m. -XX:MaxTenuringThreshold=15: set the maximum age of garbage.
Collector settings:
-XX:+UseSerialGC setting Serial Collector-XX:+UseParallelGC setting parallel Collector-XX:+UseParalledlOldGC setting parallel older Collector-XX:+UseConcMarkSweepGC setting concurrent Collector
Garbage collection statistics:
-XX:+PrintGC-XX:+PrintGCDetails-XX:+PrintGCTimeStamps-Xloggc:filename
Parallel collector settings:
-XX:ParallelGCThreads=n sets the number of CPU used by the parallel collector for collection. Number of parallel collection threads-XX:MaxGCPauseMillis=n sets the maximum pause time for parallel collection-XX:GCTimeRatio=n sets the percentage of garbage collection time as a percentage of program running time. The formula is 1 / (1cm n)
Concurrent collector settings:
-XX:+CMSIncrementalMode is set to incremental mode. Suitable for single CPU case-XX:ParallelGCThreads=n sets the number of CPU parallel collection threads used when the concurrent collector young generation collection mode is parallel collection (2) jvm analysis tool:
Jps: viewing java processes
-Q displays only the pid, not the class name, the jar file name and the parameters passed to the main method-m outputs the parameters passed to the main method, on the embedded jvm it may be the full package name of the null-l output application main class or the full pathname of the jar file of the application-v output the parameter jps host passed to JVM to view the jps of host (premise: host provides jstatd service)
Jstatd: start the jvm monitoring service. It is an application based on rmi (remote interface invocation) that provides information about native jvm applications to remote machines. The default port is 1099. -p specifies the port.
Jmap1: observe the physical memory usage of running jvm
-heap: print jvm heap (garbage collector type)-histo: print the histogram of the jvm heap. The output information includes the class name, the number of objects, and the size of the object occupation. -histo:live: ditto, but only print surviving objects-permstat: print permanent generation heap (method area)-finalizerinfo: print information about objects waiting to be reclaimed
Jmap2: use jmap to dump the process memory usage into a file, and then use jhat analysis to see.
Jmap-dump:format=b,file=dumpFileName pidjmap-dump:format=b,file=4574.heap20151215 4574
Jinfo: printing command line parameters and system properties
-flags print command line arguments-sysprops print system properties
Jstack1: you can get information about java stack and native stack running java programs. You can easily know the running status of the current thread.
-l long list. Print additional information about the lock, such as the ownable synchronizers list belonging to java.util.concurrent-m prints all stack information for java and native cstroke + frames
Jstat:
Options-option. We generally use-gcutil /-gc to check the process number of gc pid-VM, that is, the currently running java process number: interval. interval time (in seconds or milliseconds). The default is ms. Must be positive integer count-print times, if default, print numerous examples: jstat-gc 4645 4645 10 means that the viewing process is 4645 gc each 500ms is printed once, for a total of 10 times
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.