[Switch] Summary of basic JVM concepts: data types, stacks, and stacks

Source: Internet
Author: User

Summary of basic JVM concepts: data type, stack and stack, basic type and reference type

 

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, that is, the value represents the value itself, and a variable of the reference type saves the reference value. The reference value represents the reference of an object, not the object itself. The object is stored in the address indicated 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.

Stack and stack

Heap and stack are the key to running the program. It is necessary to clarify their relationship.

 

Stack is the unit of runtime, while stack is the unit of storage.

Stack solves the running problem of a program, that is, how the program executes or processes data. Stack solves the data storage problem, 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. Including local variables, program running status, and method return values. The heap is only responsible for storing object information.

Why do we distinguish Stack from Stack? Isn't the stack capable of storing data?

First, from the perspective of software design, stack represents the processing logic, while stack represents the data. This separation makes the processing logic clearer. Divide and conquer. This idea of isolation and modularization is embodied in all aspects of software design.

Second, the separation of heap and stack allows the heap content to be shared by multiple stacks (it can also be understood as multiple threads accessing the same object ). There are a lot of benefits for such sharing. On the one hand, this sharing provides an effective way of data interaction (such as shared memory), on the other hand, shared constants and caches in the heap can be accessed by all stacks, saving space.

Third, the stack needs to divide the address segment because of its runtime needs, such as saving the context of the system running. Since the stack can only grow up, it will limit the stack's storage capacity. The heap is different. Objects in the heap can dynamically grow as needed. Therefore, the stack and heap splitting makes dynamic growth possible. In the corresponding stack, you only need to record an address in the heap.

Fourth, object-oriented is the perfect combination of stack and stack. In fact, there is no difference between an object-oriented program and a previously structured program in execution. However, the introduction of object-oriented makes the Way of Thinking About Problems change, and it is closer to thinking about natural ways. When we split the object, you will find that the object's attribute is actually data, stored in the heap; and the object's behavior (method) is the running logic, placed in the stack. When writing objects, we actually write the data structure and the data processing logic. I have to admit that the object-oriented design is really beautiful.

In Java, the main function is the starting point of the stack and the starting point of the program.

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

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

Objects are stored in the heap. The stack stores basic data types and references of objects in the stack. The size of an object cannot be estimated, or it can be dynamically changed. However, in the stack, an object only corresponds to a 4btye reference (the benefit of stack separation :)).

Why not place the basic type in the heap? Because the occupied space is generally 1 ~ 8 bytes-requires less space, and because it is a basic type, there will be no dynamic growth-the length is fixed, so storage in the stack is enough, it makes no sense to put him in the heap (It also wastes space, which will be explained later ). It can be said that the basic types and object references are both stored in the stack and are a few bytes. Therefore, when the program is running, their processing methods are unified. However, the basic types, object references, and objects are different, because one is the data in the stack and the other is the data in the heap. One of the most common problems is parameter passing 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.

After the above two points are clarified. When passing a parameter through a method call, Java does not have a pointer, so it calls the value (this can be referred to the value transfer call in C ). Therefore, many books say that Java is used for value transfer, which is no problem and simplifies the complexity of C.

But what is the illusion of transferring references? 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.

Stacks and stacks are the most fundamental thing for running programs. You can run programs without stacks, but not without stacks. Heap is a data storage service for stacks. To put it bluntly, heap is a shared memory. However, Java garbage collection is only possible because of the separation of stack and stack.

In Java, the stack size is set through-XSS. When there is a large amount of data stored in the stack, you need to increase this value appropriately. Otherwise, a java. Lang. stackoverflowerror exception occurs. The common exception is that recursion cannot be returned, because the information stored in the stack is the record point returned by the method.

Java object size

The size of the basic data type is fixed. The size of non-basic types of Java objects is debatable.

In Java,The size of an empty object is 8 bytes.This only saves the size of an object without any attributes in the heap. See the following statement:

Object Ob = new object ();

In this way, the life of a Java object is completed in the program, but the space occupied by it is:4 byte + 8 byte. 4 byte is the space required to save the reference in the java stack mentioned above. The 8byte is the object information in the Java heap. Because all non-basic Java objects must inherit object objects by default, No matter what Java objects are, their size must be greater than 8 bytes.

With the object size, we can calculate the size of other objects.

Class newobject {

Int count;

Boolean flag;

Object ob;

}

Its size is: null object size (8 bytes) + int size (4 bytes) + Boolean size (1 byte) + Null Object Reference size (4 bytes) = 17 bytes. However, because Java divides the object memory allocation by an integer multiple of 8, the size of the object is 24 because the nearest integer multiple of 8 is greater than 17 bytes.

Note the following:Size of the packaging type of the basic type. Because this type of packaging has become an object, we need to treat them as objects. The size of the packaging type is at least 12 bytes (minimum space required to declare an empty object), and 12 bytes does not contain any valid information. Meanwhile, because the size of the Java object is an integer multiple of 8The size of a Basic Package class must be at least 16 bytes.. This memory usage is terrible. It is n times of the basic type (n> 2), and some types of memory usage are even more exaggerated (you can find it if you want ). Therefore, the packaging class should be used as little as possible. After JDK and later, the Java Virtual Machine will optimize the storage because automatic type replacement is added.

Reference Type

Object reference types are dividedStrong reference, soft reference, weak reference, and Virtual Reference.

 

Strong reference:That is, we generally declare that the object is a reference generated by the VM. In the strong reference environment, when garbage collection is performed, We need to strictly judge whether the current object is strongly referenced. if the object is strongly referenced, it will not be garbage collection.

 

Soft reference:Soft references are generally used as cache. The difference between soft reference and strong reference is that when soft reference is garbage collection, the virtual opportunity determines whether to recycle soft reference based on the remaining memory of the current system. If the remaining memory is insufficient, the space referenced by the soft reference will be reclaimed by the virtual opportunity. If the remaining memory is relatively rich, the space will not be recycled. In other words, when a Virtual Machine occurs in outofmemory, there must be no soft reference.

 

Weak reference:Weak references are similar to soft references and are used as cache. However, unlike soft references, weak references will be recycled during garbage collection. Therefore, their life cycle only exists in one garbage collection cycle.

 

Strong references Needless to say, our system generally uses strong references for use. "Soft reference" and "weak reference" are rare. They are generally used as cache, and generally used as cache when the memory size is limited. If the memory is large enough, you can directly use the strong reference as the cache, and the controllability is higher. Therefore, they are often used in the desktop application system cache.

Address: http://developer.51cto.com/art/200911/165015.htm

[Switch] Summary of basic JVM concepts: data types, stacks, and stacks

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.