Daily Record - enhance articles

Copyright: https://blog.csdn.net/weixin_41716049/article/details/90793740

1.Java model memory (heap, stack)
    A) Java memory model
        Java virtual machine memory under its jurisdiction substantially three logical parts: the method area (Method Area), Java Java stack and heap.
        (1) the method area is statically allocated, the compiler will bind a variable storage location, and these bindings do not change at runtime.
    Constant pool, named constant in the source code, String constants and static variables stored in the method area.
        (2) Java Stack is a logical concept, is characterized by LIFO. A stack space may be continuous, or it may be discontinuous.
        The most typical application is to call the method of Stack, Java virtual machine called once every method to create a method frame (frame), which quit method frame is the corresponding pop (pop). Determine the data stored in the stack is running.
        (3) Java heap allocation (heap allocation) means that in a random order, memory management model storage space allocation and recovery at runtime.
        Data storage is often heap size, number and lifetime can not be determined at compile time. Memory Java objects are always allocated in the heap.
        We are writing the code every day, we are using the JVM memory every day.
    b) java memory allocation
         (1) base data type directly allocated stack space;
         formal parameters (2) Method of directly assigned in the stack space, when the method call completes recovered from the stack space;
         (3) reference data types, need new to create, both assigned an address space on the stack space and class variables in the heap allocated objects;
         (4) Method of reference parameters, allocating stack space in an address space, and region-pointed object heap space, when the method is called after recovering from a stack space;
         (5) out of the new local variables, stack and heap space space allocate space when the end of the life cycle of the local variables, stack space immediately recovered, recovered GC heap region wait;
         incoming (6) method call actual parameters, the stack space allocated first, after completion of the method call stack space release;
         (7) a string constant in the DATA region allocation, the this heap space partitioned;
         the actual size (8) in the array both stack space allocated array name, and heap space allocated array!

2. garbage collection
    GC must be found before the recovery of objects which are useless object
    ? How to determine which objects are useless
        ! Search algorithm
            (1) root search algorithm
                through some "GC Roots" objects as a starting point, down from these nodes route search, search by reference chain becomes, if an object is not referenced GC Roots of chain links, the object description is unavailable
            (2) cited art algorithm (obsolete)
    recovery algorithm?
        (1) labeled - clear algorithms
            mark - sweep algorithm consists of two phases: the "mark" and "clear." In the mark phase, to identify all objects to be recovered, and mark. Clear mark phase followed by stage, the object is not available to determine the phase flag cleared. Mark - sweep algorithm is based on the collection algorithm efficiency labeling and cleanup phase is not high, but after clearing back a large amount of contiguous space is not so large when the program needs to allocate memory objects, may not find enough contiguous space.
        (2) copy algorithm
            copy algorithm is to put the memory into two equal size, wherein each use one, when the garbage collection, live objects copied onto another one, and then clean out the whole piece of memory. Copy the algorithm is simple, high efficiency, but because you can only use half of them, causing memory utilization rate is not high. Now the new generation JVM collected by replication method, since the majority of the new generation of objects (98%) are Chaosheng Xi die, so the two memory ratio other than 1: 1 (about 8: 1).
        (3) mark - Collation Algorithm
            Mark - sorting algorithm and mark - sweep algorithm is the same, but the mark - sorting algorithm is not copied to another block of memory objects survive, but to live objects move toward one end of the memory, and then directly recovered memory beyond the boundary. Mark - sorting algorithm improves memory utilization, and it fits in the collection of objects survive longer in the old era.
        (4) generational collection
            generational collection is based on the survival time of the object's memory is divided into old and new generation, according to the survival characteristics of each generation of objects, each with different generations of garbage collection algorithms. The use of the new generation of replication algorithm, using old's mark - Collation Algorithm. To achieve waste algorithm involves a large number of procedural details, and different methods of virtual machine platform is also different.
3.JVM configuration
    XMX, XMS
4. Multithreading:
    sleep (), wait () the difference between
        the role of wait () is to allow the current thread into the wait state run by the blocking state, and the role sleep () is also a thread from running to let blocked
        difference is wait () will release the synchronization lock objects, and sleep () does not release the lock
        wait is usually used for inter-thread interactions, sleep is often used to pause execution.
The thread pool
6.arrayList do is thread-safe
    ArrayList, Hashset, HashMap is not thread-safe,
    the Vector and hashTable is thread-safe, open source code can be seen on their respective core method plus the synchronized keyword
    collections Tools provides relevant API, you can make three changes to the security of unsafe collection
    collections.synchronizedCollection (C)
    Collections.synchronizedList (List)
    Collections.synchronizedMap (m)
    Collections.synchronizedSet (S)
    above has several functions corresponding to the type of the return value, what type of incoming return what type of open source can be seen the core of the method is to add a set of synchronized keyword
7. design patterns: singleton pattern
    to create a type model (five kinds of): factory method pattern, abstract factory pattern, singleton, builder mode
    structural model (a total of seven kinds ): adapter mode, proxy mode,
    type of model (a total of ten one): strategy mode, the observer mode
    
    (1) singletons:
        divided lazy and starving formula formula formula and registration for only one instance of a class the situation
        lazy style: when the first call to the method will make a judgment if it does not exist, create one, if the instance exists, then return directly. Lazy loading, such as profiles, to be used only when loading
            // singleton formula lazy  
            public class MySingleton {  
              
                // set up a static variable  
                private static MySingleton mySingleton = null;  
              
                MySingleton Private () {  
                    // Constructors privatization  
                    System.out.println ( "-> single idler embodiment mode starts calling the constructor");  
                }  
                  
                // Open a public method, it is determined whether there has been an example, there is return, not return a new  
                public static MySingleton the getInstance () {  
                    System.out.println ( "-> single idler embodiment mode start calling a public method returns an instance");  
                    IF (MySingleton == null) {  
                        System.out.println ( "-> example Lanhan the constructor is not currently being created");  
                        MySingleton MySingleton new new = ();  
                    } the else {  
                        System.out.println ( "-> example Lanhan the constructor has been created");  
                    }  
                    System.out.println ( "-> method call ends and returns singleton");  
                    return MySingleton;  
                }  
            }  
        starving formula: start loaded.
        // singleton formula starving  
        public class MySingleton2 {  
          
            // set up a static variable, directly create instances  
            Private static MySingleton2 MySingleton new new MySingleton2 = ();  
          
            Private MySingleton2 () {  
                // Constructors privatization  
                System.out.println ( "- -> starving single embodiment mode starts calling the constructor ");  
            }  
              
            // open a public method, it is determined whether there has been an example, there is return, the return of creating a new  
            public static MySingleton2 the getInstance () {  
                System.out.println ( "-> Example starving single mode call to a public method returns an instance start");  
                return MySingleton;  
            }  
        }  
        If the lazy man is time for space, then a hungry man style is space for time
        starving style although saving time, but wasted space;
        loading speed lazy man's influence program, while in the case of concurrent , lazy style is unsafe
        ways to improve: you can add the synchronized keyword
    (2) factory model
        factory model factory method pattern can be divided into abstract factory pattern and
            factory method pattern can be divided into ordinary factory pattern, factory method pattern more static factory method mode
            normal mode factory: a plurality of classes implement the same interface, the role of the factory class is instantiated class will do when a judgment
                such as a fruit that has an interface
                with a apple, banana fruit classes implement two interfaces
                in when the class is instantiated determines
                IF (apple.equals (type)) {
                    return new new the Apple ();
                } the else IF (banana.equals (type)) {
                    return new new Banana ();
                the else {}
                    System.out.println ( "Please enter the correct type");
                    return null;
                }    
            plurality of modes factory method: The method is a modification of the ordinary mode factory, the factory method in the normal mode, if the transmission error string , you can not create the correct object, and a plurality of modes to provide a plurality of methods plant factory method to create the object are
                a class factory to create a method which has a plurality of different objects, in use, the first instance of chemical classes, using a factory class calls inside the method
            static factory method pattern: that is, above the factory method pattern in multiple ways set static, do not need to create a sample, you can directly call for the
        abstract factory pattern:
            the factory method pattern there is a problem is to create a class of dependent factory class, that is to say, if you want to expand the program, you must modify the class factory, which is contrary to the principle of closure, so, from a design point of view, there are some problems, how to solve? Abstract factory model can be used to create multiple factory class, so that once the need to add new features, directly adding new class can be a plant, the need to modify the code before
    (3) Adapter design pattern
    (4) decorative patterns
8. volidate
    Once a volatile shared variable is modified (class member variables, static class member variables), then the two layers have semantics:
  1) to ensure visibility when different threads on the operating variable, i.e. a thread modifies a value of the variable, which is the new value for the other thread is immediately visible.
  2) prohibition command reordering.
    volatile nature is telling jvm current variable values in register (working memory) is uncertain, needs to be read from the main memory;
    the synchronized current variable is locked, only the current thread can access the variables, other threads are blocked live . 
    
    (1) .volatile can only be used in a variable level;
        the synchronized variables can be used in the methods, and the class level 
    (2) .volatile visibility can be achieved only modify variables, it does not guarantee atomicity;
        the synchronized variable is guaranteed changes visibility and atomicity 
    (3) .volatile will not cause obstruction thread;
        the synchronized may cause obstruction thread. 
    (4) .volatile variable mark will not be optimized compiler;
        variable mark can be synchronized optimizing compiler 

9.threadLocal
    ThreadLocal thread is used to create a local variable, we know that an object of all the threads share its global variables, these variables are not thread-safe, we can use synchronous technology. But when we do not want to use sync, we can choose ThreadLocal variable.

    Each thread will have their own Thread variable, they can use the get () \ set () methods to get their default value or change in value of their internal thread. ThreadLocal instance they usually want to associate with the thread state is private static properties.
10.mysql: open source, you can customize your own
    index: what kind of field you want to index the
11 transactions
    read uncommitted
    Read Committed
    rereading
    serialized
12.mysql index: hash, BTree
    (1) hash index lookup data basically once positioning data, of course, a large number of collisions, then performance will fall. The btree index have to look at the next node, and it is clear to pinpoint efficiency in the data hash index is higher than the btree;
    (2) so imprecise find it also very clear, because the hash algorithm is based on, etc. value calculation, so the "like" and other range discovery hash index is invalid, not supported;
    (3) the optimal prefix for joint index btree supported, hash is not supported by the joint index fields with either full or not full . Lift the optimal prefix were all thrown confused, and sometimes it seems too much venting;
    (4) does not support the hash indexes sort, index value and the calculated hash value is not necessarily the same size.
    
    ? View index case execution of
        executing the statement show status like '% Handler_read%' ;
        Results by:
            If the index is working, the Handler_read_key value will be high, this value represents the number of times a row index reading, the low value of the index table to increase the performance improvement obtained is not high, so the index does not often use
            a high value if Handler_read_rnd_next It means that the query run inefficient remedy should be indexed, meaning that the value is the number of requests to read the next row in the data file. If you are doing a lot of table scans, Handler_read_rnd_next value will be high. Description Index is incorrect or does not use the index.
    
    ? How do I view those sql statement execution is slow: mysql slow log
    ? Mysql statement optimization:
        (1) from the SELECT * ADMIN left the Join log ON admin.admin_id = log.admin_id the WHERE log.admin_id> 10
            after optimization: select * from (select * from admin where admin_id> 10) T1 lef join log on T1.admin_id = log.admin_id.
            When a JOIN, results to be driven with a large result of small (left join result as small as possible if the left side of the table should be placed in the left first conditional processing, right join empathy reverse)
        used between the time (2) limit the larger base
            e.g. : select * from admin order by admin_id  limit 100000,10
            Optimized for: select * from admin where admin_id between 100000 and 100010 order by admin_id.
        (3) try to avoid operation in the column, which leads to the failure index
            , for example: select * from admin where year (  admin_time)> 2014
            optimized to: select * from admin where admin_time> '2014-01-01'

13. The separate read and write: how to read and write to achieve separation program inside
    before entering the Service, use AOP to make judgments, is to use the library to write or read the library, according to the judge may determine in accordance with the method name, for example, to query, find, get, etc. beginning on commuting library, go write other libraries.
    If the transaction manager is configured in transaction policy, policy configuration is used in the transaction marked ReadOnly method is to use Slave, other use Master.
    If no transaction management policy, adopt the principle of the method name match to query, find, beginning with the get method Slave, the other with the Master.

14.Redis: Data type: string (string), dictionary (hash), list (List), collection (set), ordered collection (Sorted Set)
    commonly used commands: set, get, del, incr ( increment), decr (decreasing),
    single-threaded? Multithreading?
    SETNX command: IF SET Not EXISTS
        SETNX be used to implement the distributed lock
        SETNX key value, the value of key set value, if and only if the key does not exist
    redis persistent manner
    redis transaction
15. Load strategy: randomly polling ( configuration weight), the number of active calls, consistency hash
16.tomcat: sharing the session
    can do some settings in the tomcat inside, using a filter method for storing, using tomcat sessionmanager storage method. Will temporarily use IO resources
    may exist inside Redis
17. The single sign-on
18.zookeeper above node types
    persistent nodes: After you create has existed until the delete operation to clear the active node
    lasting order of nodes: nodes on the basis of a lasting, each parent node maintains a timing for his first-level child node, the order records for each child node created
    temporary node: life cycle and client node sessions temporary binding, if the client session expires, the node will automatically cleared
    temporary order of nodes: can be used to implement the distributed lock
19.solr: deep tab
    Solr must prepare a list of search results returned, and the return part of it. If the section from the front of the list is not difficult. But if we want to return to page 10000 (page 20 records) data, Solr need to prepare a size of 200,000 (10000 * 20) list contains. As such, it requires not only time, but also memory. As we now historical data on production reached 6 million data, if the direct jump to the last page, must be out of memory.
    Solr4.7 release changed this situation, the introduction of the concept of a cursor. The cursor is a dynamic structure, need not be stored on the server. Cursor contains the offset of the results of the query, therefore, Solr no longer need to start from scratch every time we want to iterate until the results of the recording, the cursor function can significantly enhance the performance of deep page.
    Soon as the first query, send a parameter: cursorMark, told solr return a cursor, you can get nextCursorMark information in the returned results, we use this value as a parameter to turn the next page
20.lucene: inverted index
21.MQ : to ensure that the order of the consumer
22. distributed lock
    
23. What is a deadlock (deadlock)? How to analyze and avoid deadlocks
    deadlock refers to the case of two or more threads block forever, this situation needs to produce at least two or more than two threads and resources.
    Analysis of deadlock, we need to look at Ja
    storage. We need to find those BLOCKED state of threads and the resources they are waiting for. Each resource has a unique id, use the id we can find out which thread has its own object lock.
    Avoid nested locks, only lock and avoid indefinite wait is usually a way to avoid deadlocks where needed,


 

Guess you like

Origin blog.csdn.net/weixin_41716049/article/details/90793740