Java performance optimization [1]: basic type vs reference type

Source: Internet
Author: User

In the Java performance optimization series, memory management is a key factor of priority. When it comes to memory allocation, it will inevitably involve basic types and reference types. So today we will introduce the two types of their respective mysteries in terms of performance (for details about the other mysteries of the reference types, refer to "here ").

Glossary
First, identify 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 Java: Boolean, Char, byte, short, Int, long, float, and double. The reference type is the type of the object that can be created through new (basically derived from the object ).

Two types of storage
The differences between the two types are first reflected in the storage method.
◇ Create a reference type
When you areFunctionWhen creating a reference type object, for example, the following statement:
Stringbuffer STR = new stringbuffer ();
The stringbufferObjectThe content is stored in heap, and heap memory needs to be applied. The variable STR is only a reference (or address) for the stringbuffer object ). Variable StrValue(The address of the stringbuffer object) is stored on the stack.
◇ Create basic types
When you areFunctionWhen creating a variable of the basic type, such as the following statement:
Int n = 123;
This variable n'sValueIt is also stored on the stack, but this statement does not need to apply for memory from the stack.

In order to make the image more visible and easy for everyone to understand, a simple example is as follows:

Performance differences between stacks and stacks
Some may whisper: What is the difference between stack and stack? The difference between the heap and the stack is big. If you are still not familiar with these two concepts or are often confused, we recommend that you read the books in this operating system first.
Since this series introduces performance, let's discuss the performance differences between the stack and stack (this difference is a huge difference ). The heap process is global and can be accessed by all threads. the stack is local to the thread and can only be accessed by this thread. For example, a stack is like a small vault, and a stack is like a treasury. If you take the money from your small vault, you do not need to do any formalities. If you take the money, you will spend it, but the amount of money is limited. Although there is a lot of money in the Treasury, however, it takes a lot of time and effort to apply for a report, create a stamp, and complete n procedures.
Similarly, because the heap is shared by all threads, applying for memory from the heap requires related locking operations. Therefore, the complexity and time overhead of applying for heap memory are much greater than that of stack; applying for memory from the stack is simple and fast, but the stack size is limited and cannot be allocated too much memory.

Why is this design?
Some people may ask again, why do we store the two types separately? Why don't we put them together? This is a good question! Next let's try to figure out why Java was designed like this.
When Java was designed by James Gosling, this problem was a bit difficult. If you place all kinds of stuff in the stack, it is obviously unrealistic. the stack is private (not easy to share), and the stack size is limited, the third stack structure indirectly limits its usage. Why not place all kinds of stuff in the heap? The above problems can be bypassed when they are put in the heap, but as mentioned earlier, many procedures are required to apply for heap memory, which is too cumbersome. If you only write a simple "Int" in the function
N = 0; ", and we need to allocate memory in the heap, so the performance will be greatly compromised (you must know that Java was born in 1995, in those years, my PC with 4 MB of memory was a luxury configuration ).
After thinking about it, Java has to make a compromise: The types are divided into basic types and reference types, and the two use different creation methods. This difference can also be seen from the Java Syntax: The reference type can be used to create objects with new (for some single keys, new is not used on the surface, but new is still used inside getinstance ); the basic type does not need to be created using new.

Disadvantages of this design
By the way, I am afraid to comment on the disadvantages of Java's design (I hope Java fans will not rush with me ). I personally think that the decision-making in this compromise has brought many profound influences. I would like to give you a few examples:
1. Because the basic type is not derived from the object, it cannot be a pure object. This causesPureThe object-oriented sign made a discount (Sun always boasted that Java wasPureOO language ).
2. Because the basic type is not derived from the object, you have to add the corresponding packaging class (such as integer and byte) to each basic type for some occasions (such as the container class ), this makes the language a little redundant.

Conclusion
From the above introduction, we should understand that the overhead of using new to create an object isNot small. Avoid it in the program as much as possible. In addition, using new to create objects will not only cause high overhead during creation, but also overhead when garbage collection is performed in the future (we will discuss GC overhead in The Post below ). Next post, let's look for an example for practical use.

Copyright Notice
All original articles in this blog are copyrighted by the author. This statement must be reprinted to keep this article complete, and the author's programming preferences and original addresses in the form of hyperlinks should be noted:

Http://program-think.blogspot.com/2009/03/java-performance-tuning-1-two-types.html

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.