Analysis of heap and stack in Java

Source: Internet
Author: User

1. Memory AllocationThink in Java details the specific memory allocation when the program is running. It can be divided into Register, stack, heap, static storage, constant storage, and non-ran storage. The following describes the java stack and stack. 1.1 comparison between the two1. Both stack and heap are places where Java is used to store data in Ram.
2. The advantage of stack is that the access speed is faster than the heap speed, second only to the registers directly located in the CPU. However, the disadvantage is that the data size and lifetime in the stack must be fixed, and there is a lack of flexibility. In addition, stack data can be shared. For details, refer to the 3rd point. The advantage of heap is that the memory size can be dynamically allocated, and the lifetime does not have to be told to the compiler in advance. The Java garbage collector will automatically collect the data that is no longer used. However, the slow access speed is due to the need to dynamically allocate memory during runtime.
3. There are two data types in Java. Basic Types (primitive types): int, short, long, byte, float, double, Boolean, and char. Exist in the stack. The other is the packaging class data, such as integer, String, double, and so on. All these types of data exist in the heap. string STR = "ABC"; and string STR = new string ("ABC"); and char [] C = {'A', 'B', 'C '}; string STR = new string (c); both use Heap Storage string STR = "ABC"; if the stack does not have an address with a value of "ABC", it is equivalent:
String temp = new string ("ABC ");
String STR = temp; internal work of string STR = "ABC. Java converts this statement into the following steps:
(1) first define a reference variable for the string class object named STR: String STR;
(2) check whether there is an address with the "ABC" storage value in the stack. If not, open an address with the "ABC" Storage name, create a new string class Object o, point the O string value to this address, and write down the referenced object o next to this address in the stack. If an address with a value of "ABC" already exists, search for the object O and return the o address.
(3) Point STR to the address of object o. The string STR = "ABC"; method can improve the running speed of the program to a certain extent, because the JVM automatically determines whether it is necessary to create a new object based on the actual data in the stack. For the code of string STR = new string ("ABC");, a new object is created in the heap, regardless of whether the string value is equal, whether it is necessary to create a new object, this increases the burden on the program. Char [] C = {'A', 'B', 'C'}; string STR = new string (c); equivalent:
String STR = new string ('A' + 'B' + 'C ');
========================================================== ========================================================== ====
1.
Class Test
{
Public static void main (string [] ARGs)
{
String S = "123 ";
}
} Runtime heap Summary: Test
======================================= Runtime instance list
--------------------- Package class count cumulative count memory cumulative memory
--------------------------------------------------------
Total 2 (100.0%) 2 (100.0%) 48 (100.0%) 48 (100.0%)
Java. Lang string 1 (50.0%) 1 (50.0%) 24 (50.0%) 24 (50.0%)
Char [] 1 (50.0%) 1 (50.0%) 24 (50.0%) 24 (50.0%) REPORT Date: 15:27:49
Conclusion: String S = "123" creates a "123" character array and a string object. 2.
Class Test
{
Public static void main (string [] ARGs)
{
String S = new string ("123 ");
}
} Runtime heap Summary: Test
======================================= Runtime instance list
--------------------- Package class count cumulative count memory cumulative memory
--------------------------------------------------------
Total 3 (100.0%) 3 (100.0%) 72 (100.0%) 72 (100.0%)
Java. Lang string 2 (66.7%) 2 (66.7%) 48 (66.7%) 48 (66.7%)
Char [] 1 (33.3%) 1 (33.3%) 24 (33.3%) 24 (33.3%) REPORT Date: 15:29:07
Conclusion: String S = new string ("123"); according to the test above, we can see that "123" creates an array, a string object, and new string () A New String object is generated based on the "123" object as a parameter, which is referenced by the S variable. 3.
Class Test
{
Public static void main (string [] ARGs)
{
String S1 = "123 ";
String S2 = "123 ";
If (S1 = S2 ){
System. Out. println ("S1 = S2 ");
} Else {
System. Out. println ("S1! = S2 ");
}
}
}
Output result: S1 = s24.
Class Test
{
Public static void main (string [] ARGs)
{
String S1 = new string ("123 ");
String S2 = new string ("123 ");
If (S1 = S2 ){
System. Out. println ("S1 = S2 ");
} Else {
System. Out. println ("S1! = S2 ");
}
}
}
Result: S1! = S25.
Class Test
{
Public static void main (string [] ARGs)
{
String S1 = new string ("123 ");
String S2 = new string ("123 ");
}
} Runtime heap Summary: Test
======================================= Runtime instance list
--------------------- Package class count cumulative count memory cumulative memory
--------------------------------------------------------
Total 4 (100.0%) 4 (100.0%) 96 (100.0%) 96 (100.0%)
Java. Lang string 3 (75.0%) 3 (75.0%) 72 (75.0%) 72 (75.0%)
Char [] 1 (25.0%) 1 (25.0%) 24 (25.0%) 24 (25.0%) REPORT Date: 15:41:50
Conclusion: the memory of the same String constant is shared even if it is referenced in different statements. "123" only generates one character data and one string object, two new strings () an object is generated separately. 6.
Class Test
{
Public static void main (string [] ARGs)
{
String S1 = new string ("123 ");
String S2 = new string ("1234 ");
}
} Runtime heap Summary: Test
======================================= Runtime instance list
--------------------- Package class count cumulative count memory cumulative memory
--------------------------------------------------------
Total 6 (100.0%) 6 (100.0%) 144 (100.0%) 144 (100.0%)
Java. Lang string 4 (66.7%) 4 (66.7%) 96 (66.7%) 96 (66.7%)
Char [] 2 (33.3%) 2 (33.3%) 48 (33.3%) 48 (33.3%) REPORT Date: 15:43:45
Conclusion: "123" and "1234" Generate their character arrays and string objects respectively. Create a String object for each new string. In a test, long begin = system. currenttimemillis (); For (INT I = 0; I <100000000; I ++) {// string STR = "abcdefghijklmnopqrstuvwxyz "; string str1 = new string ("abcdefghijklmnopqrstuvwxyz");} long end = system. currenttimemillis (); system. out. println (end-begin); when the execution cycle is 0.1 billion times, the first consumption time is 296 milliseconds, and the second consumption time is 3130 milliseconds. The new string () method is more efficient than direct assignment, which is 10 times slower than the first one. The above conclusion is verified. Character string connection Efficiency Comparison/*** verification + stringbuffer and stringbuilder efficiency */int count = 20000; string S = "T"; long begin = system. currenttimemillis (); For (INT I = 0; I <count; I ++) {S + = "T" + I;} long end = system. currenttimemillis (); system. out. println (end-begin); stringbuffer sb = new stringbuffer (); begin = system. currenttimemillis (); For (INT I = 0; I <count; I ++) {sb. append ("T" + I);} End = system. curren Ttimemillis (); system. out. println (end-begin); stringbuilder sbuild = new stringbuilder (); begin = system. currenttimemillis (); For (INT I = 0; I <count; I ++) {sbuild. append ("T" + I);} End = system. currenttimemillis (); system. out. println (end-begin); Count = 20000 33192320 COUNT = 50000 5565806232 You Can See That stringbuilder has the highest efficiency when the strings connected each time are different, while + has the lowest efficiency, if the strings for each connection are the same, the + operation is the most time-consuming. /*** Verification + stringbuffer and stringbuilder efficiency */int count = 20000; string S = "test"; long begin = system. currenttimemillis (); For (INT I = 0; I <count; I ++) {S + = "test" ;}long end = system. currenttimemillis (); system. out. println (end-begin); stringbuffer sb = new stringbuffer (); begin = system. currenttimemillis (); For (INT I = 0; I <count; I ++) {sb. append ("test");} End = system. currenttimemillis (); system. out. println (end-begin); stringbuilder sbuild = new stringbuilder (); begin = system. currenttimemillis (); For (INT I = 0; I <count; I ++) {sbuild. append ("test");} End = system. currenttimemillis (); system. out. println (end-begin); Count = 20000 19311160 COUNT = 500003231403215

 

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.