Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the concepts of JVM tuning

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today I will introduce you to the concepts of JVM tuning. The content of the article is good. Now I would like to share it with you. Friends who feel in need can understand it. I hope it will be helpful to you. Let's read it along with the editor's ideas.

Data type

In the Java virtual machine, data types can be divided into two categories: basic types and reference types. The basic type of variable holds the original value, that is, the value it represents is the value itself, while the variable of the reference type holds the reference value. "reference value" represents a reference to an object, not the object itself, which is stored at the location of the address represented by the reference value.

Basic types include: byte,short,int,long,char,float,double,Boolean,returnAddress

Reference types include class types, interface types, and arrays.

Stack and stack

Heap and stack are the key to program running, and it is necessary to explain their relationship clearly.

The stack is the unit of runtime, while the heap is the unit of storage.

The stack solves the running problem of the program, that is, how the program executes, or how to deal with the data; the heap solves the problem of data storage, that is, how to put the data and where to put it.

In Java, a thread will have a corresponding thread stack, which is easy to understand, because different threads have different execution logic, so a separate thread stack is required. The heap is shared by all threads. Because the stack is the running unit, the information stored in it is related to the current thread (or program). It includes local variables, program running state, method return value, and so on, while the heap is only responsible for storing object information.

Why separate the heap from the stack? Isn't it possible to store data in the stack?

From a software design point of view, the stack represents the processing logic and the heap represents the data. This separation makes the processing logic clearer. The idea of divide and rule. This idea of isolation and modularization is reflected in all aspects of software design.

Second, the separation of the heap from the stack enables the contents of the heap to be shared by multiple stacks (which can also be understood as multiple threads accessing the same object). There are a lot of benefits from this 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 the needs of the runtime, such as saving the context in which the system is running. Because the stack can only grow upward, it limits the ability of the stack to store content. Unlike the heap, the objects in the heap can grow dynamically as needed, so the split of the stack and the heap makes dynamic growth possible, and only one address in the heap needs to be recorded in the corresponding stack.

Fourth, object-oriented is the combination of heap and stack. In fact, there is no difference in execution between object-oriented programs and previously structured programs. However, with the introduction of object-oriented, the way of thinking about problems has been changed, and it is closer to the natural way of thinking. When we take the object apart, you will find that the properties of the object are actually data stored in the heap, while the behavior (method) of the object is to run the logic and put it on the stack. When we write objects, we actually write both the data structure and the logic to deal with the data. I have to admit that 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 a program to run. Like C, Main in java is that starting point. No matter what java program, if you find main, you will find the entrance to program execution:)

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

Objects are stored in the heap. What is stored in the stack are the basic data types and references to objects in the heap. The size of an object is inestimable, or can be changed dynamically, but in the stack, an object corresponds to only one reference to 4btye (benefits of stack separation:).

Why not put the basic types in the heap? Because it generally takes up 1 to 8 bytes of space-less space is needed, and because it is a basic type, there is no dynamic growth-the length is fixed, so storage in the stack is enough. it doesn't make sense to store it in the heap (and it wastes space, as explained later). It can be said that references to basic types and objects are stored on the stack and are a number of bytes, so they are handled in a uniform way when the program is running. But the basic type, the object reference, and the object itself 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 the problem of passing parameters in Java.

What about the value when the parameter in Java is passed? Or pass the quotation?

To explain this problem, we must first make two points clear:

1. Don't try to compare with C, there is no concept of pointer in Java.

two。 Programs always run on the stack, so when passing parameters, there is only the problem of passing basic types and object references. Will not directly pass on the object itself.

After clarifying the above two points. When Java passes parameters in a method call, it always makes a value call because it does not have a pointer (you can refer to the C value call for this). Therefore, many books say that Java is to pass value calls, which is no problem, but also simplifies the complexity of C.

But how is the illusion of biography and quotation caused? In the run stack, the basic type and reference are treated the same, both passing values, so if it is a method call of passing reference, it can also be understood as a value call of "passing reference value", that is, the treatment of reference is exactly the same as that of the basic type. But when entering the called method, the value of the reference passed is interpreted (or looked up) by the program to the object in the heap, which corresponds to the real object. If you make a change at this time, you are modifying the corresponding object, not the reference itself, that is, you are modifying the data in the heap. So this change can be maintained.

Objects, in a sense, are made up of basic types. You can think of an object as a tree. If the attribute of an object is still an object, it is still a tree (that is, a non-leaf node), and the basic 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 everything below that node.

In the stack and stack, the stack is the most fundamental thing for the program to run. A program can run without a heap, but not without a stack. The heap is the data storage service for the stack, to put it bluntly, the heap is a shared memory. However, it is the idea of separation of heap and stack that makes garbage collection of Java possible.

In Java, the size of the stack is set by-Xss. When there is a lot of data stored in the stack, you need to increase this value appropriately, otherwise a java.lang.StackOverflowError exception will occur. The most common occurrence of this exception is unreturnable recursion, because the information stored in the stack is the record point returned by the method.

The size of the Java object

The size of the type of basic data is fixed, so I won't say much here. For non-basic types of Java objects, their size is open to question.

In Java, the size of an empty Object object is 8byte, which is simply the size of an object in the heap that does not have any properties. Look at the following sentence:

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 the space needed to hold references in the Java stack mentioned in the above section. And that 8byte is the information about the objects in the Java heap. Because all Java non-primitive objects need to inherit Object objects by default, no matter what kind of Java object they are, they must be larger than 8byte.

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

Class NewObject {int count; boolean flag; Object ob;} / / its size is: empty object size (8byte) + int size (4byte) + Boolean size (1byte) + size of empty Object reference (4byte) = 17byte. But because Java allocates memory to an object as an integer multiple of 8, the nearest integer multiple of 8 of 17byte is 24, so the size of this object is 24byte.

You need to pay attention to the size of the basic types of wrappers here. Because this type of wrapper has become an object, they need to be treated as objects. The size of the wrapper type is at least 12byte (declaring at least the space required for an empty Object), and the 12byte does not contain any valid information, and because the size of the Java object is an integral multiple of 8, the size of a base type wrapper class is at least 16byte. This memory footprint is scary, it is N times that of the basic type (N > 2), and some types of memory footprint is even more exaggerated (just think about it). Therefore, wrapper classes should be used as little as possible. After JDK5.0, because automatic type swapping was added, the Java virtual machine optimized its storage accordingly.

Reference type

Object reference types are divided into strong references, soft references, weak references, and virtual references.

Strong reference: it is the reference generated by the virtual machine when we generally declare that the object is a strong reference. In a strong reference environment, garbage collection needs to strictly determine whether the current object is strongly referenced. If it is strongly referenced, it will not be garbage collected.

Soft references: soft references are generally used as caches. The difference between a soft reference and a strong reference is that when a soft reference is garbage collected, the virtual machine decides whether to reclaim the soft reference based on the remaining memory of the current system. If the remaining memory is tight, the virtual machine reclaims the space referenced by the soft reference; if the remaining memory is relatively rich, it will not be reclaimed. In other words, when an OutOfMemory occurs in a virtual machine, there must be no soft references.

Weak references: weak references are similar to soft references and are used as caches. But unlike soft references, weak references are bound to be recycled when garbage collection is carried out, so their life cycle only exists in one garbage collection cycle.

Strong references needless to say, our systems generally use strong references when using them. "soft reference" and "weak reference" are relatively rare. They are generally used as caches and are generally used as caches when the memory size is limited. Because if the memory is large enough, you can directly use the strong reference as the cache, and it is more controllable. Therefore, what they are common is the cache used in desktop applications.

These are all the contents of JVM tuning concepts, and more related to JVM tuning concepts can be searched for previous articles or browse the following articles to learn ha! I believe the editor will add more knowledge to you. I hope you can support it!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report