In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what are the basics of learning Java GC". Friends who are interested may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what are the basics of learning Java GC"?
Catalogue (Java GC Learning practice)
Talking about the Foundation
1.1 Runtime data area (Java 1.8)
1.2 garbage collection algorithm
1.3 garbage collector
Parsing GC Log
JVM monitoring tool
Related to Linux monitoring
First, talk about the foundation 1. Runtime data area
1.1 Program counter
(Program Counter Register) [Thread isolation]
Can be simply understood as the bytecode line number indicator executed by the current thread
If it is the java method, the counter record points to the address of the virtual machine bytecode instruction; if it is the native method, the counter value is undefined
[exception related] the only area where OOM is not specified.
1.2 Virtual Machine Stack
(VM Stack) [Thread isolation]
It describes the memory model executed by the Java method: [key] each method execution process will create a stack frame (Stack Frame) to store local variables, Operand stack, dynamic links, method exit and other information. Method call to the end of execution, corresponding to the process of a stack frame going into and out of the stack in the virtual machine stack
[exception related] if the stack depth requested by the thread is greater than the depth allowed by the virtual machine, a StackOverflowError exception will be thrown. Every time the method is called, it will create a stack frame, and then press the stack frame, so you can't press the stack indefinitely (I didn't understand it at first, searched it, and found that some people didn't understand it, balance in mind)
[exception dependent] most virtual machine stacks can be expanded dynamically. If you cannot apply for enough memory during expansion, you will OOM.
For code examples, see code01.StackOverflowError at the end of the article.
1.3 Local method stack
(Native Method Stack) [Thread isolation]
The difference from the virtual machine stack is that the virtual machine stack executes the java method (bytecode) service; this non-method stack serves the native method
[exception related] StackOverflowError, OOM.
1.4 heap
(Heap) [thread sharing]
GC heap (garbage dump), which is the main area managed by the garbage collector
After java1.8 [young generation, old age].
For code examples, see code02.OOM-heap at the end of the article.
1.5 yuan data area
(Metaspace) [thread sharing]
Jvm config example:-XX:MetaspaceSize=8m-XX:MaxMetaspaceSize=50m
The metadata area, which replaces the permanent generation, is essentially the implementation of the method area, which is used to store the class information, constants, static variables and JIT compiled code loaded by the virtual machine.
For code examples, see code03.OOM-metaspace at the end of the article.
two。 Garbage collection algorithm
Is the date there? Citation counting algorithm, accessibility analysis algorithm
Mark-clear algorithm (first mark, then clear, after clearing, the space is discontinuous, resulting in a lot of memory fragmentation)
Replication algorithm
Younger generation: Eden: Survivor = 1:8, 10% of the memory will be "idle"
After each GC, the surviving objects are placed in the remaining 10% of memory, that is, To Survivor
Of course, if the remaining 10% of the memory is not enough, you need to rely on the old age to allocate the guarantee.
Marking-finishing algorithm
If the survival rate of the object is high, then the replication algorithm will not work well.
After the mark-clear algorithm, all surviving objects are moved to one end, and then the memory outside the boundary is cleared.
Generation collection algorithm
The typical is divided into the new generation and the old age.
The new generation, with a low survival rate, uses the replication algorithm.
In the old days, the survival rate was high, and there was no extra space for guarantee, so the "mark-clear" or "mark-organize" algorithm was used.
3. Garbage Collector 3.1 New Generation Collector Serial
[single thread] has a long history, new generation collector, replication algorithm
STW for GC until GC is finished (your mother is cleaning and you are littering confetti, so you have to STW, you have to sit still)
The default new generation collector in Client mode (simple and efficient compared to other single-threaded collectors).
3.2 Cenozoic Collector ParNew
[parallel multithreading] New generation collector, replication algorithm, multithreaded version of Serial collector
Single CPU is no better than Serial; even double CPU cannot surpass Serial at 100%.
The Cenozoic collector is preferred in Server mode, not least because it works with CMS (the real concurrent collector).
3.3 Cenozoic Collector Parallel Scavenge
[Throughput] Throughput priority
[parallel multithreading] new generation collector, replication algorithm
The focus is different, the goal is controllable throughput (Throughput), and the other focus is to minimize GC STW time
Throughput = time to run user code / (time to run user code + GC time)
3.4 Old Collector Serial Old
[single thread] Old Age Collector, Mark-finishing algorithm
Virtual machine in Client mode for main users; in Server mode, 1. JDK1.6 was previously used with PS; 2. CMC collector back preplan.
3.5 Old Age Collector Parallel Old
[Throughput] Throughput priority
[parallel multithreading] Old Age Collector, Mark-finishing algorithm
Before jdk1.6, if you chose PS, you could not choose CMS, but Serial Old.
Throughput is the first combination.
3.6 Old Collector CMS
[concurrent multithreading] Old Age Collector, based on tag-cleanup (initial-concurrent-relabeling, concurrent purging)
The focus is different, the goal is controllable throughput (Throughput), and the other focus is to minimize GC STW time
Low concurrent pauses; disadvantages: CPU resources are very sensitive, cannot handle floating garbage, and remove multiple fragments based on tags.
3.7 G1 Collector
It's a little too much. Put it on hold.
Second, parse the GC log 1. Full GC log 2019-11-04T16:05:43.267+0800: 147.981: [GC (Allocation Failure) [PSYoungGen: 150496K-> 5938K (147456K)] 198958K-> 57548K (202752K), 0.0304547 secs] [Times: user=0.05 sys=0.00, real=0.03 secs] Heap after GC invocations=39 (full 3): PSYoungGen total 147456K, used 5938K [0x00000000f6700000, 0x0000000100000000, 0x0000000100000000) eden space 141312K, 0 used [0x000000f67000000x0000000000000000000000ff100000) from space 6144K, 96% used [0x00000000ffa000000x00000000000000fc8f8f8re0x0000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ff8000 ParOldGen total) Used 51610K [0x00000000e3400000, 0x00000000e6a00000, 0x00000000f6700000) object space 55296K, 93% used [0x000000e3400000re0x000000e6666870re0x000000e6a00000) Metaspace used 80445K, capacity 83414K, committed 83584K, reserved 1122304Kclass space used 10018K, capacity 10577K, committed 10624K, reserved 1048576K} {Heap before GC invocations=40 (full 4): PSYoungGen total 147456K, used 5938K [0x00000000f6700000, 0x0000000100000000, 0x0000000100000000) eden space 141312K, 0% used [0x000000f6700000000x0000f6700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000fcfcf8f0000000000000000000) 93% used [0x00000000e34000000e6666870] 0x000000e6a00000) Metaspace used 80445K, capacity 83414K, committed 83584K, reserved 1122304Kclass space used 10018K, capacity 10577K, committed 10624K, reserved 1048576K = split line = 2019-11-04T16:05:43.298+0800: 148.011: [Full GC (Ergonomics) [PSYoungGen: 5938K-> 0K (147456K)] [ParOldGen: 51610K-> 48605K (83968K)] 57548K-> 48605K (231424K), [Metaspace: 80445K-> 80445K (1122304K)], 0.3256949 secs] [Times: user=0.55 sys=0.00, real=0.32 secs]. = = 2. Extract main content 2019-11-04T16:05:43.267+0800: 147.981: [GC (Allocation Failure) [PSYoungGen: 150496K-> 5938K (147456K)] 198958K-> 57548K (202752K), 0.0304547 secs] [Times: user=0.05 sys=0.00, real=0.03 secs] 2019-11-04T16:05:43.298+0800: 148.011: [Full GC (Ergonomics) [PSYoungGen: 5938K-> 0K (147456K)] [ParOldGen: 51610K-> 48605K (83968K)] 57548K-> 4848605K (231424K), [Metaspace: 80445K-> 80445K (1122304K)] 0.3256949 secs] [Times: user=0.55 sys=0.00, real=0.32 secs] 3. Analysis log
147.981 and 148.011: seconds elapsed since JVM was started
GC and Full GC: represent the garbage collection pause type. Note: it is not used to distinguish between the new generation and the old age.
GC (Allocation Failure) Allocation Failure refers to allocation failure, that is, insufficient space
Full GC (Ergonomics) Ergonomics can be understood as adaptive, which means automatically adjusting the balance between STW time and throughput
Full GC (System) calls the GC triggered by System.gc ().
[PSYoungGen: 150496K-> 5938K (147456K)]
PSYoungGen,PS stands for Parallel Scavenge collector
DefNew (Default New Generation), that is, using the Serial collector
[ParOldGen: 51610K-> 48605K (83968K)]
ParOldGen,ParOld stands for Parallel Old collector, throughput priority
[XXXXXX: 150496K-> 5938K (147456K)]
150496K-> 5938K (147456K) used capacity of this memory area before GC-> used capacity of this memory area after GC (total capacity of this memory area)
198958K-> 57548K (202752K) Java heap used capacity before GC-> Java heap used capacity after GC (total Java heap capacity)
Total 0.0304547 secs GC time (secs seconds)
[Times: user=0.05 sys=0.00, real=0.03 secs] user mode CPU time, kernel state CPU time and wall clock time
The difference between CPU time and wall clock time: Wall clock time includes all kinds of non-operational waiting time, such as waiting for disk and thread blocking
When multi-CPU or multi-core, multi-threading will stack these CPU times, so it is perfectly normal for user or sys to exceed real.
Code example code01.StackOverflowErrorpublic class StackOverflowMain {public static void main (String [] args) {/ / will throw java.lang.StackOverflowError Test test = new Test (); try {test.increment ();} catch (StackOverflowError e) {System.out.println ("sof error, this count is" + test.count); e.printStackTrace () }} static class Test {private static int count; void increment () {count++; increment ();} code02.OOM-heappublic class OOMMain {private static String STR = "string" / *-verbose:gc-XX:+HeapDumpOnOutOfMemoryError *-XX:HeapDumpPath=C:\ Users\\ User\\ Desktop\\ gc * will throw oom by Java heap space * / public static void main (String [] args) {List list = new ArrayList (); while (true) {list.add (STR + = STR) } code03.OOM-metaspacepublic class OOMByCglibMain {/ *-verbose:gc-XX:+HeapDumpOnOutOfMemoryError *-XX:HeapDumpPath=C:\\ Users\\ User\\ Desktop\\ gc *-XX:MetaspaceSize=9m-XX:MaxMetaspaceSize=9m * will throw oom by Metaspace * / public static void main (String [] args) {ClassLoadingMXBean loadingBean = ManagementFactory.getClassLoadingMXBean (); while (true) {Enhancer enhancer = new Enhancer () Enhancer.setSuperclass (OOMByCglibMain.class); enhancer.setCallbackTypes (new Class [] {Dispatcher.class, MethodInterceptor.class}); enhancer.setCallbackFilter (new CallbackFilter () {@ Override public int accept (Method method) {return 1) } @ Override public boolean equals (Object obj) {return super.equals (obj);}}); Class clazz = enhancer.createClass (); System.out.println (clazz.getName ()) / / display quantity information (total number of types loaded, currently valid types, number of types that have been unloaded) System.out.println ("total:" + loadingBean.getTotalLoadedClassCount ()); System.out.println ("active:" + loadingBean.getLoadedClassCount ()); System.out.println ("unloaded:" + loadingBean.getUnloadedClassCount ()) At this point, I believe you have a deeper understanding of "what is the basis for learning Java GC". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.