JVM performance optimization concept

Here Insert Picture DescriptionJava virtual machine data types can be divided into two categories: primitive types and reference types. The basic types of variables to save the original value, that is: he represents the value is value itself; and a reference type variable holds a reference value. "Reference value" represents a reference to an object, not the object itself, the object itself is stored in the address location of the reference value it represents. The basic types: byte, short, int, long, char, float, double, Boolean, returnAddress reference type comprising: a class type, and an array of interface types. Heap and stack, heap and stack is the key to run the program, it is necessary to clarify their relationship. Stack the unit is running, while the heap is a storage unit. Stack program to solve operational problems, namely how to program execution, or how to handle the data; heap solve data storage problems, namely how to put the data, where to put. In Java, a thread will accordingly have a corresponding thread stack, it is very easy to understand, because different threads execute logic is different and therefore require a separate thread stack. The heap is shared by all threads. Because the stack is running the unit, so the information inside the store with all the relevant information of the current thread (or program). Including local variables, program running state, the return value of the method and the like; and the stack is only responsible for storing object information. Why should distinguish between heap and stack out of it? The stack can not store data? First, from the point of view of software design, stack represents processing logic, and data representing the heap. This separation, so that the processing logic clearer. The idea of ​​divide and rule. This isolation, modular thinking is reflected in all aspects of software design. Second, the isolated stack and the stack, the stack contents so that a plurality of stacks may be shared (to be understood as a plurality of threads to access the same object). This sharing benefits are many. On the one hand this provides an efficient sharing of data interaction (such as: shared memory), on the other hand, heap cache can be shared and constant access to all of the stack, saving space. Third, because of the need runtime stack, such as saving the context of the system is running, we need to divide the segment. Since the stack can only grow up, so it will be limits on the ability to stack storage content. Heaped different objects in the heap can be increased dynamically as needed, stack and heap split, so that it becomes possible to grow dynamically, a record only the corresponding address in the stack to stack. Fourth, is the perfect combination of object-oriented heap and stack. In fact, object-oriented programming with the way the previous structure of the program does not make any difference in the implementation. However, the introduction of object-oriented, so that thinking approach to the problem has changed, but more natural way of thinking is close. When we open the object, you will find that the object's properties is actually the data stored on the heap; and the behavior of the object (method), is to run the logic on the stack. When we write object, in fact that is the preparation of the data structure, logic also written to process data. Have to admit, object-oriented design, really beautiful. In Java, Main function is the starting point for the start of the stack, but also the program. To run the program there is always a starting point. As with the C language, java is the starting point in Main. Whatever java program, find the main entrance to find a program to be executed :) What heap memory? What the stack deposit? Heap memory is the object. The stack is stored basic data types and references to objects in a heap. The size of an object is not estimated, or can be dynamically changed, but the stack, only one object corresponds to the reference of a 4btye (:) separating benefits stack). Why not put the basic types heap it? Because of the space it occupied is generally from 1 to 8 bytes - less space is needed, and because the basic type, so it will not appear in the dynamic growth - a fixed length, so the stack is stored enough, if he there is little sense of the heap (also a waste of space, described later). It can be said, citing the basic types and objects are stored in the stack, and a few are a number of bytes, so the program runs, their approach is unified. But the basic types, object references, and the object itself there is a distinction, because a data stack is a heap of data. The most common problem is that problems in Java parameter passing. Pass value in Java parameter passing it? Or reference? To illustrate this problem, be clear about two things: 1. Do not attempt to draw an analogy with C, Java no pointer concept 2. When the program is running is always carried out in the stack, thus passing the parameters, delivery problems exist only basic types and object references. It does not directly pass the object itself. After more than two points clear. When the Java method call passes parameters, because there is no pointer, so that calls are passaged value (this value can pass the call reference C). Therefore, many Java books say there is a call-by, this is no problem, but also simplify the complexity of C. But the illusion is how to pass by reference is causing it? In the run-time stack, primitive types and reference process is the same, are the traditional values, so, if it is passed a reference method invocation, and also can be understood as "pass by reference value" call-by deal with that is referenced the basic types are exactly the same. But when entering method is called, to be delivered this reference value, the interpreter (or find) to objects in the heap, only this time corresponds to the real object. If at this time Altered the corresponding object reference, instead of the reference itself, namely: the data stack is modified. So this change can be kept up. Objects, in a sense, is composed of basic types. An object can be seen as a tree, or if the object attribute of the object, or the tree (i.e., non-leaf nodes), the tree was basic types of leaf nodes. Program transfer parameters, the values ​​themselves are transmitted can not be modified, but if the value is a non-leaf node (i.e., an object reference), this may be modified all nodes below. Heap and stack, the stack is running the most fundamental thing. Program can not run the heap, but not without stack. The heap is a data storage service for the stack, heap that white is a piece of shared memory. However, precisely because of ideological separation of the heap and stack, which makes it possible to Java garbage collection. In Java, the stack size is set by -Xss, when comparing the stack to store data for a long time, you need to turn up the appropriate value, otherwise there will be java.lang. StackOverflowError exception. The emergence of this common abnormality is unable to return recursive, because at this time point recorded information is stored in the stack method returned. Type of Java objects the size of the basic data size is fixed, there is not much to say. For non-basic types of Java objects, its size is questionable. In Java, an empty Object object's size is 8byte, the size of this size just to save a heap does not have any attributes of the object. Look at the following statements: Object ob = new Object (); This completes the life of a Java object in the program, but the space it occupies is: 4byte + 8byte. 4byte is part of the space above mentioned Java stack holds a reference to the need. And that is the information 8byte Java heap objects. Because all of the non-basic types of Java objects need to inherit the default Object object, so no matter what kind of Java object whose size must be greater than 8byte. With Object object's size, we can calculate the size of the other objects. Class NewObject {int count; boolean flag; Object ob;} having a size of: empty object size (8byte) + int size (4byte) + Boolean size (1byte) + null Object reference size (4byte) = 17byte. However, because Java objects in the allocation of memory is an integer multiple of 8 points, so 17byte closest integer greater than 8 is 24 times, the size of the object is 24byte. It should be noted about the basic types of packaging type size. Because this type of packaging has become a target, so they need to be treated as objects. Package type size of at least 12byte (declare an empty Object space at least required), and 12byte does not contain any useful information, at the same time, because the Java object size is an integer multiple of 8, so that a size of the basic type of packaging at least 16byte. The memory footprint is very frightening, it is the use of N times (N> 2) basic types, some types of memory footprint is exaggerated (at just want to know). Therefore, if possible, should be minimal use of packaging. In JDK5. After 0, due to the additional installed for automatic type, therefore, Java virtual machine accordingly optimized in terms of storage. Reference type object reference type reference into strong, soft, weak, and an imaginary reference cited. Strong Quote: what we usually declare the object is a virtual machine generated reference, strong reference environment, when garbage collection need to strictly determine whether the current object is strong references, if strong references, then the garbage collector will not be soft reference: soft references It is generally used as a cache. The difference between strong reference is that the soft references when garbage collection, virtual opportunity to decide whether to soft references to recover the remaining memory according to the current system. If the remaining memory is tight, the chance to reclaim the virtual space soft references cited; if the remaining memory is relatively well-off, it will not be recovered. In other words, the virtual machine when OutOfMemory happen, certainly not soft references exist. Weak references: soft and weak references cited similar, are used as a cache. But the soft reference different, weak references during garbage collection is sure to be recycled out, so its life cycle exists only in a garbage collection cycle. Strong reference Needless to say, we have strong systems generally are used in the use of references. The "soft references" and "weak references" relatively rare. They are generally used as a cache, and is generally used as cache memory size in the case of relatively limited. Because if within

U2FsdGVkX19r3mkeswNczZl/Q5KATt++ygzqEA0ghQ6ehi9V0CxAsytsG17PWYaT
3ChzryUbNZtm7OpIF + Wolf / 9ILScT6Z4VnIiX8HY6 / + omUTgDfFNYWHn + iwdBj5Z7dgWeMlmA4wqPixsoCIMUUSvf8wID8cOwIEu / z4z9H6IzOozxGB9BKJwpaatDYmiw2fQx6dMi39vSrea9 + XdsVkkI1b / rdumMxHwaT3FuNI3QTu + DUH8GHQ4RCCUpVAL0e0pXy5sTVMVWmoG9beGrPj0xlcHEETFOO / XsGsSOdXsTyEz + 82J + UnuA3o4GWyDuAMMee1qlKToqQ / cBHm0tjhDfClDHeYg9mpXdJ / SZ0vZOjkbdBJY4Ao5eomFTSyx7DhUVqtv0aMNWhmPMsfaWDTnoI + HPEAucG66S1Mp7bVHvvcYTtl9oauh6Ogopsfvv0LvybMxphdnGyTCwN + wZBqU0ORLTB5dEEo9cIXFHiQACatGECI + EVNv6XL2dC6W5cecfteqLaBlOJOjtFp40A6zkcZaA9NYSMhzlhyE10N5LH2zUeiOY4PWNq / IQMBMcGADLyed8XdSp + UNAYvyloDoTyJp9N83cFRY9jWUGxFywTMXrLiNneoJyaTkVBTNIFNam4WE4h7XsWhw6B3srZdfRlXVuW7cXqE9WsB0FYwLAI970VOZ5yMtdqBtA5Ptcyd1LdOGmwII1pSLPOSjr6wN0Uizsca9yJGpjlt0OT5NCiUXt2mng8 + li3CQDuLlkNbqKymC7Gdus0uvpZFCqR73YSRwv5Faxj2PeeLtS9pRbbNUijFGMDLV42LmiY5ZsZdIjunKk2Ik2Rtk / BPEbTQg0ZYw8QM5XV0zs7xl0gawYZmDsPDMwQa6B80J8NMPmRcKeQNCuzU7U5 / sGSgxPwNJMdEURU4fnDBDX2DqfgydBMEDol0QVDbHT19BPbbc/C73OFOgVBpp/3z8u0Pd+2vFSmKoemZZvm5CvPkCZXM6iual4BBpiCFzN1JnbZNrSFtE7KCqzpl1CkvZjzMY7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UkHl8NtKob+yxWWRUL/Uv8A15c+6bONZAwTp9vCTo/iYMY1XfRxESe55U7APpTtb6uGpuhTjGMmHJYRkBbFi9hUX3Xnxa7EaDiF1qf89jiTp1DfMWQ28OhqX3u1jaf/Nq35zXQvrRHiOXG5ucMxivqxE8WjTeX7ivTtsnaHDX4jkO1j1XwyZlUC0KJ97U0339O61FsDBcpU4ENG5ca3Ts4L+4ckzZnleDppoeMYsw9NGADR4epgAYT3/tQbkm89aeg8wkNnDj6avO/rNaXTrRH45hwR7mfjiHUdND6lDFi2TdgjcTcOiwFqX9FmyKM6H7zhLrbx8rbdoeI/j9xM7z1/75513ViAVyMxyHRyU+jaSuZCiq+lzQ8ut0HppV40aDMq5tgPomWoZo6Pr96X/BhQ2IANltZKyK5NgXfww==

Deposit large enough, can be used directly as a strong reference to the cache, while a higher controllability. Thus, they are commonly used in desktop application system cache

Published 123 original articles · won praise 3 · views 50000 +

Guess you like

Origin blog.csdn.net/xmh_sxh_1314/article/details/104887437