Java Virtual Machine Learning-JVM Tuning Summary (5)

Source: Internet
Author: User

Data Type

In a Java virtual machine, a data type can be divided into two categories: the base type and the reference type . A variable of the underlying type holds the original value, that is, the value he represents is the value itself, whereas a variable of the reference type holds the reference value. A reference value represents a reference to an object, not the object itself, where the object itself resides at the address represented by the reference value.

Basic types include: byte,short,int,long,char,float,double,boolean,returnaddress

Reference types include: class type , interface type , and array .

Heap and Stack

Heaps and stacks are key to the program's operation, and it's important to make their relationship clear.

1. The stack is the unit of the runtime, and the heap is the unit of storage

Stack solves the problem of how the program executes, or how the data is handled, and what the heap solves is the problem of data storage, that is, where the data is placed and where it is placed.

It is easy to understand that a thread in Java has a line stacks corresponding to it, because different thread execution logic differs and therefore requires a separate line stacks. The heap is shared by all threads. The stack is a running unit, so the information stored in it is related to the current thread (or program). This includes local variables, program run states, method return values, and so on, while the heap is only responsible for storing object information.

2. Why should the heap and stack be differentiated? Is it possible to store data in the stack?

First, from the point of view of software design, the stack represents the processing logic, and the heap represents the data. This separation makes the processing logic clearer. The idea of divide and conquer. This idea of isolation and modularity is reflected in every aspect of software design.

Second, the separation of the heap from the stack allows the contents of the heap to be shared by multiple stacks (and can also be understood as multiple threads accessing the same object). The benefits of this sharing are many. On the one hand, this kind of sharing provides an effective way of data interaction (for example: Shared memory), on the other hand, the shared constants and caches in the heap can be accessed by all stacks, saving space.

Third, the stack because of the needs of the runtime, such as saving the context of the operation of the system, the address segment needs to be divided. Because the stack can only grow upward, it restricts the ability to store content on the stack. While the heap is different, the objects in the heap can grow dynamically as needed, so the stack and heap splits make the dynamic growth possible, and only one address in the heap is required to be recorded in the corresponding stack.

The object-oriented is the perfect combination of heap and stack. In fact, the object-oriented approach to the implementation of the previous structured program is not any different. However, the introduction of object-oriented, so that the way to think about the problem has changed, and closer to the natural way of thinking. When we take the object apart, you will find that the object's properties are actually data, stored in the heap, and the object's behavior (method) is running logic, placed in the stack. When we write the object, we write the data structure, and also write the logic of the processing. I have to admit, object-oriented design is really beautiful.

3. in the Java in which Main The function is the starting point of the stack and the starting point of the program.

There is always a starting point for the program to run. Like the C language, main in Java is the starting point. Whatever the Java program, find main and find the entry for the program execution:)

4. What is stored in the heap? What is stored in the stack

The object is stored in the heap. The stack is a reference to the base data type and the objects in the heap. the size of an object is not estimated, or can be dynamically changed, but in the stack, an object only corresponds to a 4btye reference (the benefit of stack separation:)).

Why not put the basic type in the heap? because the space it occupies is typically 1~8 bytes-it takes less space, and because it is a basic type, there is no dynamic growth-fixed length, so storage in the stack is enough, if there is no meaning to put him in the heap (it will be wasted space, explained later). It can be said that the basic type and object references are stored in the stack, and are a number of bytes, so when the program runs, they are handled in a uniform manner. But the basic type, the object reference, and the object itself are different, because one is the data in the stack, one is the data in the heap. One of the most common problems is the problem with parameter passing in Java.

5. Java Pass the simultaneous value in the parameter? Or to pass a reference

To illustrate this issue, first make two points clear:

1. do not attempt to analogy with C ,the concept of no pointers inJava

2. The program is always run in the stack, so when the parameter is passed, there is only the problem of passing the base type and object reference . The object itself is not passed directly.

Explicitly after the two points above. When Java passes a parameter in a method call, because there is no pointer, it is the invocation of a value (this can be referred to as a call to the value of C). As a result, many of the books say that Java is a call to value, and that this is no problem and simplifies C complexity.

But how is the illusion of citation caused? in the run stack, the basic type and the reference are treated the same, and are all passed values, so if it is a reference to a method call, it can also be understood as a value invocation of "reference value", that is, the processing of the reference is exactly the same as the base type. However, when the called method is entered, the value of the reference being passed is interpreted (or found) by the program to the object in the heap, which corresponds to the actual object. If you modify at this point, you are modifying the reference to the corresponding object instead of the reference itself, that is, the data in the heap is modified. So this is a change that can be maintained.

objects, in a sense, are made up of basic types. You can look at an object as a tree, the object's properties, if it is an object, is still a tree (that is, a non-leaf node), and the base type is the leaf node of the tree. When a program parameter is passed, the passed value itself cannot be modified, but if the value is a non-leaf node (that is, an object reference), you can modify all the content underneath the node.

Stacks and stacks are the most fundamental things that a program runs. A program can run without a heap, but not without a stack. The heap is a data storage service for the stack, and the heap is a shared memory. However, it is the idea of the separation of heaps and stacks that makes Java garbage collection possible.

Java, the size of the stack is set by-XSS, when the stack of data stored in a long time, you need to adjust this value appropriately, otherwise there will be Java.lang.StackOverflowError exception. The common occurrence of this exception is the inability to return recursion because the information saved in the stack is the record point returned by the method.

Java Virtual Machine Learning-JVM Tuning Summary (5)

Related Article

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.