JVM basics and tuning (1)

Source: Internet
Author: User

In recent projects, memory and performance problems have occurred and need to be optimized. So I took this opportunity to sort out my Java Virtual Machine (VM). You are welcome to point out something wrong.

Data Types, because during Java optimization, the data types detected are generally relatively basic. After all, complex data types are combined.

In Java virtual machines, data types can be divided into two types: basic type and reference type. A variable of the basic type stores the original value, indicating the value of the data itself. It is the most basic part of the data and generally includes:

Byte, short, Int, long, Char, float, double, Boolean, returnaddress

The byte, short, Int, long, Char, float, double, and Boolean commands of Java virtual machine are used by JSR, RET, and jsr_w commands of Java virtual machine. The return address type value points to the operation code of a VM command. Different from the previous types, the returnaddress type does not exist in the Java language, and the value of the returnaddress type cannot be changed during the program running.

The reference type variable stores the reference value. This is the house name, corresponding to a house, but the house address is saved, that is, the first address of the variable stored in the memory, generally including:

Class reference, interface reference, array

The important concept of heap and stack is worth mentioning.

From the perspective of program running, a program can run mainly divided into data and logic, corresponding to the ing between stack and stack.

Stack is the logical storage of the program running. The stack frame of the method is compared to that of the method, indicating the unit of the program running.

A heap is the place where data is stored during running. For example, a new object represents the unit of program storage.

Stack solves program running problems, that is, how the program is executed or how the data is processed;

Heap solves the problem of data storage, that is, how to store and store data.

In Java, a thread has a corresponding thread stack, which is easy to understand. Because different thread execution logic is different, an independent thread stack is required. The heap is shared by all threads. Because the stack is a running unit, the information stored in the stack is related to the current thread (or program. Stack frames are composed of local variables, program running status, and method return values. The stack stores object information only.

Why is this design? Why are the JVM specifications designed to separate them? We can only guess one or two based on our own speculation.

① In line with the object-oriented philosophy, we should separate the changing and changing (the feeling of yin and yang in Taiji ), we can know that there is no difference between object-oriented and process-oriented programs in terms of execution or satisfying requirements, but object-oriented references are more similar to natural ways of thinking. When writing a program, we can think carefully about the variables of the class members we have compiled. The attributes of the object are stored in the heap, but the method is stored in the stack, in this way, when we write the data structure of the corresponding object, we actually process the data and the corresponding logic, and achieve the unification of the Taiji. Think about the beauty of object-oriented design.

② From the perspective of software design: the stack represents the processing logic, while the heap represents the processed data. In this way, the processing and processing are separated, yes, the processing logic is clearer. This modular and isolated idea is embodied in all aspects of software design.

③ The separation of heap and stack not only enables the corresponding thread of the stack to better focus on its own processing logic, but also enables the content in the heap to be shared with multiple stacks, it can be understood that multiple threads access the same object, which provides an effective way of data interaction, such as sharing data. On the other hand, it also saves space. Of course, thread security is also a problem.

④ Because the stack needs to run, it needs to save the runtime context and divide the address segments. The reference to data in the stack is only the first address of the memory, so in this case, objects in the heap can dynamically grow as needed.

If the heap and stack are separated, what is stored in the heap and what is stored in the stack?

Objects are stored in the stack, and basic data types and references of objects in the stack are stored in the stack. The size of an object cannot be estimated, or it can be dynamically changed. However, only one reference and four bytes are stored in the stack, therefore, Java program theory has limitations on the number of instances.

Why do we put the basic types in the stack? One reason is that the occupied space is small and the occupied space is small. In the heap, we need to add four bytes of references, besides, the length of the basic type is fixed and will not increase dynamically.

Currently, the basic types and object references are both stored in the stack and are a number of several bytes. Therefore, they can be consistent during logic processing, but the basic types and object references can be consistent, the object itself is different. A basic problem corresponding to this time is the parameter passing problem in Java:

When passing parameters in Java? Or upload reference?
To clarify this problem, we must first clarify two points:
1. Do not try to make an analogy with C. Java has no pointer concept.

2. The program is always running in the stack. Therefore, when passing parameters, there is only a problem of passing basic types and object references. The object itself is not directly transmitted.

Therefore, all Java methods are called by passing values, but how is the illusion of passing reference calls:

In the running stack, the basic type and reference processing are the same and both are passed values. Therefore, if the reference method is called, it can also be understood as the call to transfer the reference value, that is, the reference processing is exactly the same as the basic type. However, when a method is called, The referenced value passed is interpreted (or searched) by the program to the object in the heap, which corresponds to the real object. If the modification is made at this time, the corresponding object is modified instead of the reference itself, that is, the data in the heap is modified. Therefore, this modification can be maintained.

Object, in a sense, is composed of basic types. You can view an object as a tree. If the object's attribute is still an object, it is still a tree (that is, a non-leaf node). The basic type is the leaf node of the tree. When passing program parameters, the passed value itself cannot be modified. However, if the value is a non-leaf node (that is, an object reference ), you can modify all the content under this node.

This section is excerpted from the Internet and I feel better about it.

In Java, the stack size can be set through-XSS. When there is a large amount of data stored in the stack, you can increase this value appropriately. Otherwise, Java will appear. lang. stackoverflowerror. A common exception is loop recursion. The heap size can be set through-xmx3/-XMS, which will be detailed later.

After talking about the storage structure, how big is the next Java object?

The basic data type is fixed. in Java, the size of an empty object is 8 bytes, in addition, the size of a non-attribute object in the heap is saved in this trivial matter. If reference is included, that is, object instance = new object (); that is, 12 bytes, 4 bytes indicates the space occupied by the reference.

Class student {

Int nmber;

Boolean isgood;

Object teacher;

}

Size: 8 (empty object) + 4 (INT size) + 1 (Boolean size) + 4 (size referenced by teacher) = 17 bytes, in Java, the object memory is allocated by an integer multiple of 8 and is allocated to 24 in sequence. The object size is 24 bytes.

In addition, references are the categories referenced in Java:

Strong reference, soft reference, weak reference, and Virtual Reference

Each category is not detailed, because in the optimization process, our Code generally adopts strong references, and soft references and weak references are generally used in the cache processing logic.

JVM basics and tuning (1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.