Before boss let do some outsourcing interview, my question is very simple:
Tell us about the work more interesting problem solved.
HashMap use need to pay attention to the point.
The first major problem is to look at the other gold project experience, the second question is whether a test to know some details, such as HashMap is not thread-safe, use HashMap cache to do so may lead to memory leaks, etc., feel problems can also be designed to: D ~ but saw other colleagues on the subject of the tears collapse:
Design Patterns XXX
Garbage collection XXX
Rub, how I do not feel this problem. . .
It feels like a virtual machine operating system, compiler: the very tall. But Java programs run on top of the problems encountered have to go to troubleshooting, performance optimization not have to go, or basic knowledge required!
Java Virtual Machine in the implementation process will it manages memory is divided into a number of different data regions
Functions of each part as follows:
The program counter can be seen as the current thread of execution bytecode line number
VM stack for local variables, stack operation etc.
Native method stack is similar to the virtual machine stack, but is to serve the local method
Heap storage objects
Method area to store such information, constant, static variables, JIT compiled code, etc.
When the runtime constant pool compiled a variety of literal and symbolic use
External memory via direct memory allocation NIO
In memory management is a relatively large piece of content GC (garbage collection), is the so-called garbage collection will reclaim the memory garbage out. Then the first question: What is spam?
Reference counting algorithm: The reference number 0 objects.
Root search algorithm: find references from GC Roots along the object.
Here are mentioned in references after JDK 1.2 Java has been the concept of reference were expanded, then the second question: What type of reference?
Strong reference: Object o = new Object () that are strong references.
Weak Quote: Also useful but not essential, to be recovered before the OOM.
Soft Quote: weaker reference to be recovered in the next GC time.
Phantom reference: the weakest, the only action is to be notified when the object is recovered.
There is only strong reference to the impact on the lifetime of an object. It evolved in the course of the development of many of the virtual machine garbage collection algorithm, such as:
Mark - sweep algorithm
Mark - Collation Algorithm
Generational collection algorithm
In the actual device is used in the recovery of these types of combination algorithms, such as seen from VisualVM Memory is such
On the whole a generational collection algorithm, and S0, S1 can be seen as two parts labeled - Collation Algorithm. Then the third question: Common CMS garbage collector implementation process like?
Initial Tags: GC Roots objects directly associated with.
Concurrent mark: Root Tracing.
Relabeled: repair because the program run lead mark fluctuates.
Can be seen only in the initial marking and re-marking the only time required Stop The World, and other user threads are executed together, do not think this is perfect, parallel execution process will consume some CPU resources.
The Java source threw JVM certainly can not be executed, you need to use javac compiled into class files for the job, then the first question: What is the structure of the class file?
Class index, the parent index and index interfaces
Virtual machine specification does not specify at what time you want to load the class, but sets out in the face of new, reflection, the parent class, Main needs initialization is complete.
Performed by ClassLoader class load in a virtual machine, this place needs to understand:
Two classes are the same, except for the name of the outer class ClassLoader also need to determine whether or not the same.
Parents delegate model is not a hard constraint.
After the completion of the class loader can begin, and things related to the operation and threads are placed in the stack frame, its structure is as follows:
Property role / meaning
Local variables Local variable table method parameters and methods defined inside
Operand stack is used for instruction
Dynamic connection point to the runtime constant pool reference method stack frame belongs
Method returns the address of the top position of the method call this method
Additional information debugging information
Which method performs specific call is a headache, you need to be addressed:
Static assignment: the same name but different parameter types.
Dynamic assignment: The replication method inheritance.
Bytecode instructions are stack-based operations, such as 1 + 1 to complete such a calculation, the corresponding command as follows:
iconst_1 // constants 1 onto the stack
iadd // adding the two values of the stack and the stack, and then put the result back into the stack
istore_0 // top of the stack into the value of the local variable table 0 Solt
Interpreted benefits are downloaded fast start, but OK also very clear: to run slow. JIT it is used to solve this problem, the method can be called multiple times, the body of the loop is executed many times compiled to native code.
Optimization is a very interesting topic, I remember participating in a game when the code into gcc -O3 compiled after the printf () did not export. . In the JIT more common optimization methods are:
Common subexpression elimination If an expression has been calculated, and then the latter does not need to repeat the calculation
Array bounds checking is not necessary to eliminate once without missing check
Copy the code within the method of linking to the calling process
Escape analysis to determine whether the object may be referenced outside the method
Program execution will be related to memory operations, defined in Java eight operations to complete:
lock the variable identified as a thread exclusive state
unlock release variables
read the variable read from main memory into the working memory
load variable value to be read into the working memory copy
use the working memory of the variable is passed to the execution engine
Assign the value passed to the engine returns a copy of the working memory
store the working memory of the variable is passed to main memory
write the variable obtained from the working memory write variables corresponding to the main memory
Here it is necessary to talk about the volatile role in the use of time to be able to understand the following two:
Guaranteed variable for all threads are visible.
Prohibit instruction rearrangement optimization.
If all the operations in Java programmers need to control it, there will be a lot of duplicate code, and write very tired, so we can advance two parallel operating principles to determine whether there is a conflict:
Program sequence rules: a single-threaded programs written in accordance with the order.
Tube Blocking rule: unlock must precede the lock.
volatile variable rules: first occurrence of a write operation to the read operation.
Thread Start Rule: Thread.start () prior to thread any other method.
Thread Termination rules: thread in all operations prior to the termination of this thread detection.
Thread interrupts rules: interrupt () prior to the break detection.
End of rule objects: the object is initialized prior to the completion of its finalize () method.
Transfer rules: If A precedes B, B precedes C, then A precedes C.
Thread the underlying implementation is quite troublesome, but should at least know how the state of Thread conversion