Java variables and memory allocations

Source: Internet
Author: User

Java variables and memory allocations (very important)

Stack

Static Storage Area

The memory used by a program compiled by C + + is divided into the following sections

1. Stack (stack)-Automatically allocated by the compiler to release, store the function parameter value, local variable value and so on. It operates in a manner similar to a stack in a data structure.

2. Heap area-allocated by the programmer to release, if the programmer does not release, the end of the program may be recycled by the OS. Note that it is not the same as the heap in the data structure, but the distribution is similar to the linked list.

3. Global zone (static zone)-the storage of global variables and static variables is placed in a block, initialized global variables and static variables in an area, uninitialized global variables and uninitialized static variables in another contiguous area. Released by the system after the program is finished.

4, literal constant area-the constant string is placed here, the program is released after the end of the system.

5. Program code area-binary code that holds the function body.

Save address in Java:

Register Register This is the fastest place where the data is located in a place that's different from all other ways. The number of registers is very limited so the register is assigned by the compiler as needed we have no direct control over it. It is also impossible to find any sign of register presence in your own program.

The registers of the JVM are used to hold the current system state. However, based on the portability requirements, the JVM cannot have too many registers. Otherwise, it is more difficult to simulate high-speed registers with conventional storage for any of its own registers that are smaller than the JVM's porting target machine. The JVM is also stack-based, which makes it less of a register.

The JVM registers include the following four:

(1) PC Program Count Register (2) optop operand stack top address register. (3) Frame current execution Environment address register. (4) VARs local variable header address register.

These register lengths are 32 bits. The PC is used to record the program execution steps, and the rest of the Optop,frame,vars store the corresponding address in the JVM stack for quick access to the information required for the current execution.

stack stack stacks are located in regular RAM random access memory but can get the processor's direct support stack pointer through its stack pointer if moving down creates new memory if you move up, it will release those memory. This is a particularly fast and particularly efficient way to save data only after the Register creation program The Java compiler must know exactly the length of all the data stored in the stack and the time it exists because it must generate the appropriate code to move the pointer up and down, which undoubtedly affects the flexibility of the program, although some Java data is stored in the stack, especially object references, but Java objects Doesn't fit in .

some basic types of variables and object reference variables defined in the function are allocated in the function's stack.

When a variable is defined in a block of code, Java allocates a memory space for the variable in the stack, and when the scope of the variable is exceeded, Java automatically frees the memory space allocated for that variable, which can be used immediately by another. so try to use basic types of variables.

heap (or memory heap heap) A general purpose memory pool is also in RAM all Java objects are stored inside and stacks of different memory heaps or heap heaps are most attractive because the compiler does not have to know how much storage space to allocate from the heap. How long does the stored data stay in the heap so that you can save data in heaps with greater flexibility when you create an object, you only need to code the code with the new command to execute it, and the data is saved automatically in the heap . We have to pay a price for this flexibility. If you allocate storage space in the memory heap and allocate the size of the storage space compared to the former to spend a longer time and C + + Java is actually not allowed to create objects in the stack that is just for theoretical comparison

heap memory is used to hold objects and arrays created by new. managed by the Java Virtual machine's automatic garbage collector.

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. A reference variable is the name of an array or object, and you can use reference variables from the stack in your program to access the arrays or objects in the heap.

static storage static statics means that data that is stored statically while still in RAM while the program is running will be ready to call the available static keyword to indicate that a particular element of an object is static, but the Java object itself never It's not going to be in a static storage space.

constant storage constant storage constant values are usually placed directly inside the program code. It's safe to do this because they never change. Constants need to be strictly protected so consider placing them in a read-only memory ROM

non-RAM storage If the data is completely independent of a program then even if the program is not running, they can still exist and be outside the control of the program. Two of the most important examples of this are streaming objects and persistent objects becoming bytes for streaming object objects Regular circulation to another machine and for persistent objects we can save them on disk or tape even if the program aborts, they can still keep their state intact. One of the main considerations for designing these types of data stores is to turn objects into forms that can exist on other media You can also change back to an ordinary object that exists in RAM. Currently Java provides only limited persistence object support in future versions of Java that are expected to provide more complete support for durability.

Reprint others summed up: red for Self-supplement (http://blog.csdn.net/gaowenming/archive/2010/02/22/5316423.aspx)

Stacks and heaps are places that Java uses to store data in RAM.       Unlike C + +, Java automatically manages stacks and heaps, and programmers cannot directly set up stacks or heaps. Java'sheap, or heap of memory.is a run-time data area in which the object allocates space. These objects are established through directives such as new, NewArray, Anewarray, and Multianewarray, and they do not require program code to be explicitly released. 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. But the downside is that because you want to run theDynamically allocating memory, the access speed is slow.StackThe advantage is that the access speed is faster than the heap, second only to register, the stack data can be shared. But the downside is thatthe data size and lifetime of the existing stack must be deterministic and inflexible. The stack mainly contains some basic types of variables (, int, short, long, byte, float, double, Boolean, char) and object handle. Stack has a very important particularity, is that there is data in the stack can be shared. Suppose we define both: int a = 3; int b = 3; the compiler processes int a = 3 First, it creates a reference to a variable in the stack, and then finds the value of 3 in the stack, and if not found, 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.string is a special wrapper class data. Can be used: string str = new String ("abc"); String str = "ABC"; Two forms, the first is to create new objects with new (), which is stored in the heap. A new object is created each time the call is made.

-> String str = new String ("abc"): It should be said that there will be two objects, one entity object for the new String ("ABC") into the memory heap, and a reference object for the Stack object str that is the class instance object.

and the second kind(String str = "ABC";)is to first create a String class object reference variable str in the stack, and then find if there is no "ABC" in the stack, if not, put "ABC" in the Stack, and make str point to "ABC", if there is already "ABC" directly to the STR point to "ABC".when comparing values within a class, use the Equals () method; whenTo test whether a reference to two wrapper classes is pointing to the same object, use = =,The above theory is illustrated in the following example. String str1 = "abc"; String str2 = "abc"; System.out.println (STR1==STR2); Trueyou 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); Falsethe new method is to generate different objects. Each time you generate a。 So in the second way(String str = "ABC";)Create multiple "ABC" strings, only one object exists in memory. This writing is advantageous and saves memory space. At the same time it can improve the speed of the program to some extent, because the JVM will automatically determine whether it is necessary to create new objects based on the actual data in the stack.    In the case of string str = new String ("abc"), the code creates a new object in the heap, regardless of whether the string value is equal or not, and it is necessary to create a new object, thereby aggravating the burden of the program. On the other hand, it is important to note that when you define a class using a format such as String str = "ABC", you 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. Because of the immutable nature of the string class, you should consider using the StringBuffer class to improve program efficiency when a string variable needs to change its value frequently.

Primitive type--"On the stack, you can refer to the above description. (It's strange how bigdecimal and date types are not in the table below, and stirng = "abc" is not also put on the stack, so is string also possible to say?) )

Java variables and memory allocations

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.