Stack and memory allocation in Java (excerpt)

Source: Internet
Author: User

In Java, there are six different places where data can be stored:

1. Register: The fastest storage area, which is allocated by the compiler according to the requirements, we can not control in the program.
2. Stack: A reference to a basic type of variable data and an object, but the object itself is not stored in the stack, but is stored in the heap (the new object) or in a constant pool (the string constant object is stored in a constant pool. )
3. Heap: Store all new objects.
4. static domain: holds static members (statically defined)
5. constant Pool: holds string constants and basic type Constants (Publicstatic final).
6. Non-RAM storage: Permanent storage space such as hard disk


Here we are primarily concerned with stacks, heaps, and objects in the stack and constant pool can be shared, and objects in the heap cannot be shared. The data size and life cycle in the stack can be determined, and the data disappears when no references point to the data. the garbage collector is responsible for the collection of objects in the heap , so the size and life cycle do not need to be determined and have great flexibility.
For strings: References to their objects are stored in the stack, and if the compilation period has been created (defined directly in double quotes), it is stored in a constant pool , which is stored in the heap if the run-time (new) is determined . For a string equal to equals, there is always only one copy in the constant pool, with multiple copies in the heap.

NewString ("China");
When a string is generated by new (assuming "China"), the constant pool is first searched for whether the "Chinese" object is already in use, and if none is created in the constant pool, then a copy object of this "China" object in the heap is created in a constant pool. This is the Youdao interview question: Strings = new String ("XYZ"); one or two, if there is no "xyz" in the constant pool, it is two.

The data for local variables exists in the stack memory. Local variables in the stack memory disappear as the method disappears. ( formal parameters are local variables)
The member variable is stored in the object in the heap and is collected by the garbage collector

stacks in Java memory allocation: Some basic types of variable data and object reference variables defined in the function are allocated in the function's stack memory. When a variable is defined in ablock of code,Java allocates memory space for the variable in the stack, and when the variable exits its scope, Java automatically frees the allocated memory space for that variable. The memory space can be used for other purposes immediately. the advantage of the stack is that the access speed is faster than the heap, after the register, the stack data can be shared . However, the disadvantage is that the size and lifetime of the data in the stack must be deterministic and inflexible.

. Suppose we define at the same time: int a =3;   int b = 3;  the compiler processes int a = 3 First, it creates a reference to the variable A in the stack, and then finds out if there is a 3 value in the stack, and if not found, stores 3 in it and then points a to 3. then the int b = 3 is processed, and after the reference variable of B is created, because there are already 3 values in the stack, B points directly to 3. In this case, A and B both point to 3. At this point, if you make a=4 again, then the compiler will re-search the stack for 4 values, if not, then store 4 in, and a point to 4; Therefore the change of a value does not affect the value of B. It is important to note that this sharing of data with two object references also points to an object where this share is different, because the modification of a does not affect B, which is done by the compiler, which facilitates space saving. An object reference variable modifies the internal state of the object, affecting another object reference variable.


Heap in Java memory allocation: heap memory is used for storage by new , even if the program runs outside the block of code that uses new to produce the array or object's statement, arrays and objects are garbage-free when no reference variable points to it, not in use, but still occupy memory space, which is then collected by the garbage collector (released) at an indeterminate time. This is the reason why the Java comparison takes up memory Lifetime also does not have to tell the compiler

In fact, the variables in the stack point to the variables in the heap memory, which is the pointer in Java!

Original address: http://blog.csdn.net/zhangjinru123/article/details/51984313

Stack and memory allocation in Java (excerpt)

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.