Thinking in Java---initialization and cleanup

Source: Internet
Author: User
Tags compact switches

1. Order of Initialization

1) When the first time a new object is created, either a static method or a property is accessed for the first time, the Java interpreter attempts to locate the corresponding. class file.

2) load the. class file, and the static initialization action will be performed at this stage. But static initialization is done only once when the object is first loaded.

3) When you create a new object with new, the Java virtual machine allocates enough space on the heap for the object, and the allocated space is cleared by 0. And initializes all properties to the default values (numeric types are initialized to 0, and object types are initialized to null).

4) Executes all initialization data that appears at the property definition.

5) Execute the construction method.


2. Java garbage collection mechanism

1) Garbage collection has obvious effect on improving the speed of object creation.

2) to better understand the garbage collector (GC) mode of operation in order to understand why Java uses the memory allocated to the object on the heap.

Reference counting is a simple, but slow , garbage collection technique. Each object has a reference counter, and when a reference is linked to an object, the corresponding counter is incremented by 1. Similarly, when the reference leaves the scope or the reference is set to 0, the corresponding counter is reduced by 1. The garbage collector iterates over the list of all objects and frees up space when it finds that the reference count for an object is 0.

However, because the reference count is very slow and there is a "should be recycled, but the reference count is not 0" flaw, it has never been used in any kind of Java Virtual machine implementation. The practical technical idea is that for any "live" object, it can be traced back to the reference that survived on the stack or in static storage. So, if you start with a stack or a static store, you can find all the "live" objects by traversing through all the references. Java uses an "adaptive" algorithm that implements garbage collection. How to find the surviving objects and deal with them depends on how the Java virtual machine is implemented. There are two ways to use it: Stop and copy (stop-copy) and Mark and sweep (Mark-sweep);

Stop and copy apparently said that to halt the program's run (so it is not in background recycle mode), and then copy all the surviving objects to another heap, not being copied will be treated as garbage. When objects are copied to the new heap, they are one next to each other, so the new heap is also compact. But this creates two new problems: the first problem is that you replicate between the two heaps, which is more than the actual maintenance of space. The second problem is that replication, when the program enters a stable state, only produces very little debris. This replication-type garbage collector still needs to copy objects from one location in memory to another, which is a waste of time. To avoid this waste, the mark and Sweep mode is created. It checks the heap and automatically switches to another mode "adaptive" If no garbage is generated. "Mark-sweep" is based on the idea is to go from the heap or static storage, traverse all objects, and then find the surviving object, whenever it finds a surviving object, give him a mark. But this time, no garbage collection. Clean-up is a formal start only when all the marked work is done. During the cleanup process, objects that are not marked will be cleaned out. As a result, the remaining areas of memory are not contiguous and must be handled in a compact way.

In Summary, the Java Virtual Opportunity monitors memory, and when the objects are found to be stable, they switch to "mark and sweep mode, the same Java virtual opportunity monitors the "mark-sweep" result, and automatically switches to the "Stop and copy" mode if there is a lot of fragmentation in the heap space. The combination of these two is an "adaptive", generational, stop-copy, tag-sweep garbage collector.


In addition, there are many additional technologies in the Java Virtual machine to speed up. Especially the technology associated with loading, known as the "Just-in-time,jit" compiler.


Thinking in Java---initialization and cleanup

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.