Java Heap and Stack Understanding summary __java

Source: Internet
Author: User

Java divides memory into two types: one is stack memory and the other is heap memory.

1. Stacks (stack) and heap (heap) are places where Java is used to store data in RAM (random access memory). Unlike C + +, Java automatically manages stacks and heaps, and programmers cannot directly set stacks or heaps.

2, the advantage of the stack is that access faster than the heap, second only to the register directly in the CPU, stack data can be shared. The disadvantage is that the data size and lifetime in the stack must be fixed and inflexible. There are some basic types of variables in the stack (int, short, long, byte, float, double, Boolean, char) and object handles. The JVM specification allows each Java thread to have its own independent JVM stack, the call stack for Java methods (multiple threads sharing heaps). Some of the basic types of variables and reference variables defined in the function are allocated in the stack memory of the function. When a variable is defined in a block of code, Java allocates memory space for the variable in the stack, and when the scope of the variable is exceeded, Java automatically releases the memory space allocated for the variable, which can be used as an immediate alternative.

The advantage of the heap is that the memory size can be dynamically allocated, and the lifetime does not have to tell the compiler beforehand, as it is dynamically allocating memory at runtime, and the Java garbage collector automatically collects the data that is no longer in use. The disadvantage is that the access rate is slow due to the dynamic allocation of memory at run time.

3, stored in the stack of data can be shared.

Assumption definition: int a = 3,B = 3.

The ① compiler handles A = 3 first. First it creates a reference to a variable in the stack, and then looks for an address that has a literal value of 3, and if not, opens an address that holds the face value of 3 and then points a to 3.

② order down, followed by the processing of B = 3. After you create the reference variable for B, you point B directly to the 3 address because you already have 3 of the literal value on the stack. This shows the situation where A and B colleagues point to 3.

③ is particularly concerned that this type of literal reference to a class object is different, and that the value is modified by reference to a literal, and that the value of another reference to this literal is not changed. If we let A = 4 at this point, it will search the stack for a 4 literal value, and if not, reopen the address to store the value of 4, and if so, point A to the address directly.

In addition, string is a special wrapper class data. Can be used:

String str = new String ("abc");

String str = "ABC";

In two forms, the first is to create a new object with new (), which is stored in the heap. Each time a call is made, a new object is created.

And the second is to create an object reference variable str in the stack on a string class, then find out if there is any "ABC" in the stack, and if not, store "ABC" in the Stack and make str point to "ABC", and If "ABC" is already there, direct STR to "ABC".

So:

Situation One:

String str1 = "abc";

String str2 = "abc";

System.out.println (STR1==STR2); True

Situation Two:

String str1 =new string ("abc");

String str2 =new string ("abc");

System.out.println (STR1==STR2); False

Situation Three:

String S1 = "Ja";

String s2 = "va";

String s3 = "Java";

String S4 = s1 + s2;

System.out.println (s3 = = S4);//false

System.out.println (S3.equals (S4));//true

4, reference variables are ordinary variables, when defined in the stack allocated memory, reference variables in the program run to the role of the extraterritorial release. and the array and the object itself are allocated in the heap, even if the program runs to a block of code that uses new to generate arrays and objects, the heap memory occupied by the array and the object itself is not freed, and the array and object are turned into garbage and can no longer be used without reference to the variable, but still occupy memory, is released by the garbage collector at a later uncertain time. This is also the Java comparison of the main reason for memory.

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.