Memory allocation principles in Java

Source: Internet
Author: User

Question: How are these classes, variables, strings, and methods in Java allocated in memory?

First, the memory areas in Java are as follows:

Register: We can't control it in the program

Stack: A reference to a primitive type of data and objects, but the object itself is not stored in the stack, but is stored in the heap. When a variable is defined in a block of code, Java allocates memory space for the variable in the stack, and when the variable exits the scope, Java automatically frees the memory space allocated for the variable, and the memory space is immediately available for another use. In fact, the variables in the stack point to the variables in the heap memory, which is the pointer in Java!

Heap: Stores data generated with new. The memory allocated in the heap is managed by the automatic garbage collector of the Java Virtual machine.  After generating an array or an object in the heap, you can also define a special variable in the stack that is equal to the first address of the array or object in the heap memory, and this variable in the stack becomes the reference variable of the array or object. Arrays and objects become garbage when no reference variable points to it, cannot be used, but still occupy memory space, and are collected (released) by the garbage collector at a later indeterminate time. This is why the Java comparison accounts for memory.

Static domain: Static members that are stored in the object with static definitions

Constant pool: Storing constants;

Chang refers to some data that is determined at compile time and is saved in the compiled. class file. In addition to the constant values (final) that contain the various basic types defined in the code (such as int, long, and so on) and object types (such as strings and arrays), there are also some symbolic references that appear as text, such as:

The fully qualified name of the class and interface;

The name and descriptor of the field;

Methods and names and descriptors.

The virtual machine must maintain a constant pool for each mounted type. A constant pool is an ordered set of constants used by that type, including direct constants (String,integer and floating point constants), and symbolic references to other types, fields, and methods.

Non-RAM storage: Persistent storage space such as hard drives

Heap and Stack:

Heap is responsible for garbage collection, the advantage of the heap is the ability to dynamically allocate memory size, the lifetime does not have to tell the compiler beforehand, because it is at runtime to allocate memory dynamically, Java garbage collector will automatically take away these no longer use data. However, the disadvantage is that the access speed is slower due to the dynamic allocation of memory at run time.

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. The stack mainly contains some basic types of variable data (int, short, long, byte, float, double, Boolean, char) and object handle (reference).

Stack has a very important particularity, is that there is data in the stack can be shared. Let's say we define both:

    1. int a = 3;
    2. int b = 3;

The compiler processes int a = 3 First, it creates a reference to a variable in the stack, and then finds out if there is a value of 3 in the stack, and if it does not, it stores the 3 in 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.

    1. String str1 = "abc";
    2. String str2 = "abc";
    3. System.out.println (STR1==STR2); //true

You can see that str1 and str2 are pointing to the same object.

String str1 =new String ("abc");

String str2 =new String ("abc");

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

The new method is to generate different objects. Each time one is generated. For the code of string str = new String ("abc"), it is necessary to create new objects in the heap, regardless of whether their string values are equal or not, so that the burden of the program is compounded by the need to create new objects.

When defining a class using a format such as String str = "ABC", we always want to assume, of course, that the object that created the string class is Str. Worry about traps! The object may not have been created! Instead, it might just point to an object that was previously created. Only through the new () method can you guarantee that a new object is created each time.

About Equals () and = =:

Equals () is simple for string to compare whether the Unicode sequence of two strings is equivalent, returns True if equal, and = = is the same as the address of the two strings, that is, a reference to the same string.

Memory allocation principles in Java

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.