Creation, memory allocation, and destruction of objects in Java __java

Source: Internet
Author: User
Java back-end technology 2017-03-22 23:52

First, the creation of objects

The creation object is implemented with the new keyword, and there is a lot more detail behind the New keyword for the JVM. When an object is created, for example: Object = new Object (), its corresponding bytecode instruction is new (object array and base type array is not), and when executing to the new instruction, the virtual opportunity first checks whether the corresponding class has been loaded, and if it is not loaded, Then the process of performing class loading. After loading, you need to allocate memory space for the object. The allocation algorithm mainly divides into two kinds, one is the pointer collision, if the heap memory is neat, uses the memory and the unused memory divides the dividing point, then allocates the memory for the newborn object is the boundary point to move the object equal size position to be possible. Looking at the JVM source, you can tell that the virtual machine uses CAs and failed retries to ensure that the memory allocation is thread safe. If the heap memory is not neat, there is no clear dividing point between the memory used and unused memory, so the virtual machine must record which memory is in use and which memory is unused, and when allocating memory, it needs to find a memory space greater than or equal to the size of the current object. This allocation algorithm is called an idle list. The way a virtual machine allocates memory for an object depends on whether the garbage collector you use has collation capabilities. After the memory is allocated, initialization is required, such as initializing space to 0 values, calling constructors, and so on.

Ii. which objects need to be collected by GC

There are two main ways to determine that an object needs to be recycled:

1, reference counting method, when there is a place to refer to the object, the Counter plus 1, do not reference the minus 1. At any point, when the object's reference counter is 0 o'clock, it indicates that the object has not been referenced, so the object can be reclaimed. The biggest drawback of this algorithm is that it can't solve the problem of mutual reference, for example: Class A refers to the Class B example, Class B refers to the Class A instance, but A and B are not referenced anywhere, when a and B reference count is not 0, then garbage collection can not reclaim such objects and lead to memory leaks. Interested readers may not believe that trying to write an example, the conclusion can be recycled, because the Java virtual machine does not adopt this algorithm.

2, accessibility analysis algorithm, this algorithm is based on some called GC roots objects as the starting point, from the beginning of the enumeration, if the GC roots to the object is unreachable, that is, starting from the GC roots, no path can reach the object in memory, then the object has been detached from the organization ", the garbage collector can recycle the object." The objects that are roots as GC are: objects referenced in the virtual machine stack, objects referenced by class static properties, objects referenced by constants, objects referenced in the native method.

Third, the object of the Finalize method

When the GC roots to the object, the object is tagged and filtered once, and if the object implements the Finalize method, the object is placed in a referencequeue queue, This queue is called Queue.remove () by a daemon called finalizer, which is started by the virtual machine, and this method blocks until an element returns, and the returned element calls its Finalize method. A lot of information says finalizer thread priority is very low, actually its priority is not low: Thread.max_priority-2 = 8, and thread's default priority is 5. By looking at the source code, you can see that the thread's priority is 8 by jstack the thread stack.

It is not an easy thing to know more about the bottom floor, and a lot of information needs to be consulted. Although you can see OPENJDK source code to learn more, but I believe many friends of OpenJDK source code or confused, do not know where to look. Friends with a good way to welcome the message to discuss together, learn together, progress together.

Related Article

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.