preface
The Java virtual machine has its own complete hardware architecture, such as processor, stack, and corresponding instruction system.
A Java virtual machine is essentially a program that, when launched on the command line, begins executing instructions stored in a bytecode file. The portability of the Java language is based on the Java virtual machine. Bytecode files (.class) can run on any platform that has a Java virtual machine specific to that platform. This is “compile once, run many times”.
Java virtual machine is not only a cross-platform software, but also a new network computing platform. The platform includes many related technologies, such as various apis that conform to open interface standards, optimization techniques, and so on. Java technology enables the same application to run on different platforms. The Java platform can be divided into two parts, namely the Java Virtual Machine (JVM) and the Java API class library.
Share detailed analysis of 77 Java interview often asked JVM questions, I hope to help you, in addition, I summarize some information for the current Internet interview to everyone, at the end of the article have access to way (full of sincerity)
Because of the length of the article, I’m not going to break it down here,More than 400 pages of study notes for JVM performance tuning practices.
1. Is there a memory leak in Java? Please describe it briefly. 2, In 64-bit JVMS, int length is the majority. What are the differences between Serial and Parallel GC? For 32-bit and 64-bit JVMS, is the length of a variable of type int the majority? 5. The difference between WeakReference and SoftReference in Java? 6. What does the JVM option -xx :+UseCompressedOops do? Why use it
When you migrate your application from a 32-bit JVM to a 64-bit JVM, the heap memory is suddenly increased, almost doubling, as Pointers to objects are increased from 32-bit to 64-bit. This also adversely affects the amount of data cached by the CPU (which is much smaller than memory). Because the main motivation for moving to a 64-bit JVM is the ability to specify a maximum heap size, some memory can be saved by compressing OOP. With the -xx :+UseCompressedOops option, the JVM uses 32-bit OOP instead of 64-bit OOP.
7. How to determine whether the JVM is 32-bit or 64-bit using a Java program? What is the maximum heap memory for 32-bit and 64-bit JVMS? What are the differences between JRE, JDK, JVM and JIT? Explain Java heap space and GC? 11. JVM memory area
The JVM memory area is mainly divided into thread private area (program counters, virtual stack, local method area), thread shared area (JAVA heap, method area), and direct memory. Thread-private data areas have the same life cycle as threads, and are created/destroyed depending on the start/end of the user thread (in Hotspot VM, each thread maps directly to the operating system’s local thread, so the memory of this part of the memory area follows the life/death of the local thread).
Thread shared areas are created/destroyed with the startup/shutdown of the virtual machine.
Direct memory is not part of the JVM runtime data area, but is frequently used: NIO, introduced in JDK 1.4, provides Channel and Buffer based I/O. It can use Native function libraries to directly allocate out-of-heap memory, and then use DirectByteBuffer objects as references to this memory. This avoids copying data back and forth between the Java heap and Native heap, which can significantly improve performance in some scenarios.
12, Program counter (thread private) 13, virtual stack (thread private) 14, local method area (thread private) 15, can you guarantee GC execution? How to obtain the memory used by Java programs? What percentage of the heap is used?
The remaining memory, total memory, and maximum heap memory can be obtained using the memory-related methods in the java.lang.Runtime class. Using these methods you can also get the percentage of heap usage and the amount of heap memory remaining. Runtime.freememory () returns the number of bytes of freeMemory, runtime.totalmemory () returns the number of bytes of totalMemory, and runtime.maxmemory () returns the number of bytes of maximum memory.
17. What is the difference between a heap and a stack in Java? Describe how the JVM loads class files. Why GC? Heap (Heap- thread shared) – runtime data area 21 method area/persistent generation (thread shared) 22 JVM runtime memory
The Java heap can also be subdivided From a GC perspective into the new generation (Eden, From Survivor, and To Survivor) and the old generation.
23, New generation 24, Old age 25, permanent generation 26, JAVA8 and metadata 27, Reference counting 28, Accessibility analysis 29, Mark-sweep algorithm
The most basic garbage collection algorithm is divided into two stages, annotation and cleanup. The mark phase marks all objects that need to be reclaimed, and the clear phase recycles the space occupied by the marked objects. As shown in figure:
As can be seen from the figure, the biggest problem of this algorithm is the serious memory fragmentation, and the problem that large objects cannot find available space may occur later.
Copying algorithms 31, Mark-Compact 32, generational collection algorithms 33, New generation and copying algorithms 34, old generation and tag copying algorithms 35, JAVA strong references 36, JAVA soft references 37, JAVA weak references 38, JAVA Phantom reference
Virtual references need to be implemented by the PhantomReference class, which cannot be used alone but must be used in conjunction with the reference queue. The main purpose of a virtual reference is to track the status of an object being garbage collected.
In the new generation – copy algorithm 41, in the old era – tag collation algorithm 42, partition collection algorithm 43, GC garbage collector 44, Serial garbage collector (single thread, copy algorithm) 45, ParNew garbage collector (Serial+ multi-thread) The Avenge avenge avenge avenge avenge avenge avenge avenge avenge avenge
Serial Old is the tenured version of the Serial garbage collector, which is also a single-threaded collector using the mark-collation algorithm. This collector is also primarily the default tenured garbage collector running on the Client’s default Java virtual machine. In Server mode, there are two main uses:
- Used in versions prior to JDK1.5 with the new generation Parallel exploiter.
- As a backup garbage collection solution for the older generation using the CMS collector. Garbage collection process diagram of new Generation Serial and Old generation Serial
The next-generation Parallel Scavenge collector is similar to the ParNew collector in that it is a multi-threaded collector and uses replication algorithms to suspend all worker threads during garbage collection. Avenge the insane and ParNew insane.
G1 collector 51, JVM class loading mechanism 52, Class loader 53, parent delegate 54, OSGI (Dynamic Model System) 55, Dynamic change construction Modular programming and hot plug 57. JVM memory model
Thread exclusive: stack, local method stack, program counter thread shared: heap, method area
58, stack
Also known as method stack, thread private, thread execution method is to create a stack array, used to store local variable table, operation stack, dynamic link, method exit information. A method is pushed when called, and a method is pulled back from the stack.
59. Local method stack
Similar to a stack, it is used to hold information about the execution method. Java methods are executed using the stack, while Native methods are executed using the local method stack.
60. Program counter
It stores the bytecode position executed by the current thread. Each thread has an independent counter when working, which only serves for executing Java methods. When executing Native methods, the program counter is empty.
61, heap
The largest portion of JVM memory management, shared by threads, is intended to hold object instances. Almost any object instance will be placed here, and an OOM exception will be raised when the heap runs out of space. Depending on the lifetime of the object, the JVM manages the object by generation, and the garbage collector manages the garbage
64, What is a Java virtual machine (JVM)? Why is Java called a “platform independent programming language”? Describe the mechanism by which a JVM loads a class file. 71, How to determine whether an object can be recycled
There are two ways to determine whether an object is alive or not:
- Reference count: Each object has a reference count property. When a new reference is added, the count is increased by 1. When a reference is released, the count is decreased by 1. This method is simple and does not solve the problem of objects referring to each other circularly.
- Reachability Analysis: Start with GC Roots and search down the path called the reference chain. When an object is not connected to GC Roots by any reference chain, the object is unusable and unreachable.
Does garbage collection occur in JVM permanent generation When will the Minor and Full GC occur? 77. You know what JVM performance tuning is
Set heap size -Xmx: maximum heap size. Set the Cenozoic size. The new generation should not be too small, otherwise there will be a large influx of objects into the old age -xx :NewSize: the size of the new generation -xx: the proportion of the new generation and the old generation -xx :SurvivorRatio: Eden space and Survivor space ratio set garbage collector Young substitute -xx :+UseParNewGC Old substitute -xx :+UseConcMarkSweepGC
Share a set of my learning database, we see if there is a need:
- Java Core Knowledge:
- Java interview tips:
Content covers: Java, MyBatis, ZooKeeper, Dubbo, Elasticsearch, Memcached, Redis, MySQL, Spring, Spring Boot, Spring Cloud, RabbitMQ, Kafka, Linux Technologies such as stack
Dry goods share
Have you ever been in a situation like this:
Running online system suddenly stuck, the system can not access, or even directly OOM!
New project launch, the various JVM parameter Settings are confused, directly default, and then…… Wanted to solve the online JVM GC problem, but didn’t know where to start.
You have to recap the principles and concepts of the JVM before every interview, but the interviewer will often ask you how to tune the JVM parameters in a real project, how to solve GC, OOM, etc.
Space is limited, the other content will not be shown here one by one, this **JVM performance tuning actual combat more than 400 pages of learning notes ** has been organized, need friends, take yourself!
Concern public number: Kylin bug.
The last
Welcome everyone to exchange, like the article remember to pay attention to me like yo, thank you for your support!