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

How to resolve basic types and reference types in Java performance optimization

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

Share

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

In this issue, the editor will bring you about how to analyze the basic types and citation types in Java performance optimization. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

Definition of ★ nouns

First make clear what is the "basic type" and what is the "reference type".

To put it simply, the so-called basic types are the following eight built-in types in the Java language:

Boolean

Char

Byte

Short

Int

Long

Float

Double

Reference types are those that can be created through new (basically derived from Object).

Two types of storage methods for ★

The difference between these two types is first reflected in the storage mode.

Creation of ◇ reference type

When you create an object of reference type in a function, such as the following sentence:

StringBuffer str = new StringBuffer ()

The contents of the StringBuffer [object] are stored on the Heap and need to apply for heap memory. The variable str is just a reference (or address) to the StringBuffer object. The value of the variable str (that is, the address of the StringBuffer object) is stored on the stack.

Creation of ◇ basic types

When you create a basic type of variable in the function, such as the following sentence:

Int n = 123

The [value] of this variable n is also stored on the Stack, but this statement no longer needs to request memory from the heap.

In order to make it more vivid and easy for everyone to understand, a simple schematic diagram is drawn as follows:

Performance differences between ★ heap and stack

Some students may whisper: what's the difference between a stack and a stack?

When it comes to the difference between a stack and a stack, it's a big deal. If you still don't understand these two concepts or are often confused, it is recommended to find a book on the operating system to read it.

Since it is an introduction to performance, let's discuss the difference between heap and stack in terms of performance (which is a big difference). The heap is global relative to the process and can be accessed by all threads, while the stack is thread-local and can only be accessed by the thread. For example, the stack is like a personal vault, and the stack is like the national treasury. You take money from the personal treasury to spend, you don't need to go through any formalities, take it and spend it, but the amount of money is limited; and although there is a lot of money in the treasury, it takes time and effort to type a report, stamp, and go through multiple formalities every time you apply for money.

By the same token, because the heap is common to all threads, requesting memory from the heap requires related locking operations, so the complexity and time overhead of applying for heap memory is much higher than that of the stack; it is simple and fast to apply memory from the stack, but the size of the stack is limited, so you can't allocate too much memory.

Why did ★ design this way in the first place?

Some students may have asked why the two types are stored separately and why not put them together. That's a good question! Let's guess why Java was designed like this in the first place.

When Java's father (James Gosling) designed the language, it was a bit of a dilemma. If you put all kinds of things on the stack, it is obviously not realistic, first, the stack is thread private (not easy to share), the size of the stack is limited, and the structure of the stack indirectly limits its use. Then why not put all kinds of things in the pile? Putting them all in the heap can circumvent the above problems, but as mentioned just now, the application heap memory has to go through a lot of formalities, which is too cumbersome. If you just write a simple "int n = 0;" in the function and allocate memory to the heap, the performance will be greatly poor (you know that Java was born in 1995, when I bought a PC with [4 megabytes of memory] it was a luxury configuration).

After thinking about it, Java's father had to make a compromise by dividing types into "basic types" and "reference types", both of which were created in different ways. This difference can also be seen in Java syntax: reference types always create objects in new (as a reminder: some single-key objects / singletons do not use new on the surface, but they are also created in new inside getInstance ()), while basic types are created in new.

The disadvantages of ★ 's design like this

By the way, dare to comment on the disadvantages of Java's father's design (I hope Java Fans won't be anxious with me). I personally think that this eclectic decision has brought about many far-reaching implications. Here are a few examples:

1. Since the basic type is not derived from Object, it is not a purebred object. This led to a discount on Java's "[pure] object-oriented" brand (Sun always boasted that Java was a "pure" OO language, but Java's OO was not pure enough).

2. Because the basic type is not derived from Object, for some occasions (such as container class), we have to add the corresponding wrapper class (such as Integer, Byte, etc.) to each basic type, which makes the language a little redundant.

From the above introduction, we should understand that the overhead of creating objects using new is not small. If it can be avoided in the program, it should be avoided as much as possible. In addition, using new to create objects is not only expensive to create, but also expensive to destroy objects during garbage collection in the future.

The above is how to parse the basic types and reference types in Java performance optimization. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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