Java Virtual machine
When will the full GC be triggered?
- Invocation of the System.GC () method
- Lack of space in the old age
- Immortality Zone Space (the method area in the runtime data region in the JVM specification, which is also used in the Hotspot virtual machine as an immortalized or immortalized zone, permanet generation for some class information, constants, static variables, etc.)
- GC appears promotion failed and concurrent mode failure
- Statistically obtained minor GC promoted to the old generation of the average size greater than the remaining space in the old age
- A large object is allocated in the heap
can be used as root object:
- A static variable in a class, when it holds a reference to an object, it acts as root
- Active thread, can be used as root
- A parameter to a Java method or a local variable in the method, both of which can be used as root
- A local variable or parameter in the Jni method, both of which can be used as root
Example: The following something and Apple are available as root objects.
A local variable in the parameters and methods of the Java method, which can be used as root.
The triggering conditions of the Cenozoic transfer to the old age
- Long-lived objects
- Large objects go straight into the old age
- Minor GC, survivor still not fit
- Dynamic age judgment, greater than or equal to a certain age of objects more than half of survivor space, greater than or equal to an age of objects directly into the old age
Redis
Redis Single Thread Issues
Single-threaded means that the network request module uses a single thread (so there is no need to consider concurrency security), that is, one thread handles all network requests, and the other modules still use multiple threads.
Why Redis can execute quickly
- The vast majority of requests are purely memory operations (very fast)
- Single threaded to avoid unnecessary context switching and competitive conditions
- Non-blocking Io-io multiplexing
Internal implementation of Redis
The internal implementation adopts epoll, and adopts the simple event framework which is implemented by epoll+ itself. Epoll read, write, close, connect to the event, and then use the Epoll multiplexing feature, never waste a little time on Io These 3 conditions are not independent of each other, especially the first, if the request is time-consuming, single-threaded throughput and performance can be imagined. It should be said that Redis has chosen the appropriate technical solution for a particular scenario.
Redis About thread safety issues
Redis is actually a thread-gated concept that encloses a task in a thread, naturally avoiding thread-safety issues, but for composite operations that rely on multiple redis operations, locks are still required and may be distributed.
What are the benefits of using Redis?
- Fast because the data exists in memory, and the advantage of Hashmap,hashmap is that the time complexity of finding and Operating is O (1)
- Supports rich data types and supports string,list,set,sorted Set,hash
- Support transactions, operations are atomic, so-called atomicity is to change the data is either all executed, or all do not execute
- Rich features: can be used for caching, messages, press key to set the expiration time, after expiration will be automatically deleted
What are the advantages of Redis compared to memcached?
- Memcached all the values are simple strings, redis as its replacement, supports richer data types
- Redis is much faster than memcached.
- Redis can persist its data
Database
The difference between B-and + + trees
- B-Tree, each node stores key and data, all nodes make up the tree, and the leaf node pointer is nul, and leaf nodes contain no keyword information.
- B + Tree, all the leaf nodes contain the information of all the keywords, and points to contain these key records of the pointer, and the leaf node itself according to the size of the keyword in order to link, all the non-terminal nodes can be regarded as the index part, the node only contains its sub-root node in the largest (or smallest) keyword. (The non-final node of the B-tree also contains valid information that needs to be found)
Why is B + more suitable for the operating system's file index and database index in the actual application?
- B + Disk read and write costs Lower B + 's internal nodes do not have pointers to keyword-specific information. Thus its internal nodes are smaller than the B-trees. If you keep all of the same internal nodes in the same disk block, the number of keywords that the disk block can hold is more. The more keywords you need to find when you read into memory at once. The number of Io reads and writes is correspondingly lower.
- B+-tree query efficiency is more stable because a non-endpoint is not the final node that points to the content of the file, it is just the index of the keyword in the leaf node. So any keyword search must take a path from the root node to the leaf node. The path length of all keyword queries is the same, resulting in a query efficiency equivalent for each data.
Java High concurrency
JAVA Thread state transition diagram
How does the bottom of synchronized come true?
- The Synchronization code block (synchronization) is implemented based on the entry and exit enhancement (Monitor) objects. Each object has a monitor lock. When monitor is occupied, it is locked, and the thread attempts to take ownership of the monitor while executing the monitorenter instruction, as follows:
- If the monitor has an entry number of 0, the thread enters monitor and then sets the entry number to 1, which is the owner of the Monitor.
- If the thread already occupies the monitor, just re-enter, then enter the monitor number plus 1.
- If another thread has already occupied monitor, the thread goes into a blocking state until the number of Monitor's entry is 0, and then attempts to gain ownership of the monitor again.
- The synchronous method modified by synchronized is not done by instruction Monitorenter and monitorexit (in theory it can also be achieved by these two instructions), but the constant pool is more than the normal method acc_ Synchronized identifiers. The JVM implements the synchronization of the method according to the identifier: When the method is called, the calling instruction will check whether the acc_synchronized access flag of the method is set, and if it is set, the execution thread will get the monitor first and then the method body can be executed. The monitor is released after the method has finished executing. No other thread can get the same monitor object during the execution of the method. In fact, there is no difference, but the method of synchronization is an implicit way to achieve, without the bytecode to complete
Some questions about Java face