Summary of basic JVM concepts: data types, stacks, and stacks

Source: Internet
Author: User

This article is from a blog with you. The original title is "JVM tuning Summary (1)-some concepts".

Data 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.


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: 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.