Java memory allocation and java allocation

Source: Internet
Author: User

Java memory allocation and java allocation

Store basic local variable data and object references in the stack.
Java generally involves the following areas in memory allocation:
◆ Register: we cannot control it in the program
◆ Stack: stores basic data and object references, but the object itself is not stored in the stack, but in the heap (new object)
◆ Heap: stores data generated with new
◆ Static domain: static members defined by static in objects
◆ Constant pool: stores constants.
◆ Non-RAM storage: Permanent storage space such as hard disk

STACK:Some basic types of variable data defined in the method and referenced variables of objects (and form parameters) are allocated in the stack memory, and local variables in the stack memory disappear as the method disappears. Data in the stack can be shared.

Heap:Stores the objects and arrays created by new, which are managed by GC. Defines the reference variable in the stack. The value is equal to the first address of the array or object in the heap memory. The referenced variable is released after the program runs out of its scope. Arrays and objects do not. GC recycles the object only when no referenced variable points to it.

class BirthDate {      private int day;      private int month;      private int year;          public BirthDate(int d, int m, int y) {          day = d;           month = m;           year = y;      }  }    public class Test{      public static void main(String args[]){  int date = 9;          Test test = new Test();                test.change(date);           BirthDate d1= new BirthDate(7,7,1970);             }          public void change1(int i){          i = 1234;      }}

The following describes the changes in code execution:
1. Run the main method: int date = 9; date local variable, basic type, reference value, and so on exist in the stack.
2. Test test = new Test (); test is an object reference. It exists in the stack and the object (new Test () exists in the heap.
3. test. change (date); I is a local variable, and the reference and value exist in the stack. After the method change is executed, I disappears from the stack.
4. birthDate d1 = new BirthDate (1970,); d1 is an object reference, which exists in the stack. The object (new BirthDate () exists in the heap, where d, m, y is a local variable stored in the stack, and their type is the basic type, so their data is also stored in the stack. Day, month, and year are member variables, which are stored in the heap (new BirthDate ). After the BirthDate constructor is executed, d, m, and y will disappear from the stack.
5. After the main method is executed, the date variables, test, and d1 references will disappear from the stack. new Test () and new BirthDate () will wait for garbage collection.

Constant pool:Constant pool refers to the data that is determined during the compilation period and saved in the compiled. class file.
In addition to the various basic types defined in the Code and the object type constant value (final), it also contains some symbolic references that appear in the form of text, such:
◆ Full qualified names of classes and interfaces;
◆ Field name and descriptor;
◆ Method name and descriptor;
The String type that has been created during the compilation period (defined using double quotation marks) is stored in the constant pool. If it is determined during the runtime (new), it is stored in the heap.

Reference: http://blog.csdn.net/truelove12358/article/details/38139843

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.