Java programming Idea: The fifth chapter, initialization and cleanup

Source: Internet
Author: User

Knowledge Point collation:

1. Conceptually, ' Initialize ' and ' create ' are independent of each other, but in Java, the two are bundled together and inseparable.

2. Ways to differentiate overloads: each overloaded method has a unique list of parameter types. Even different parameter sequences can differentiate between two methods, but this is not recommended, which makes the code difficult to maintain.

Note: Overloading cannot be distinguished by the return value, because sometimes the return value of a method is required, but sometimes it is not required, for example, two methods, Piblic String func () {sysout ("xxxx")} with public void Func () {Sysout ("xxxx ")}, when only want to output XXXX, directly a func () is sufficient, but at this time the compiler does not know exactly which method to call.

3. If a constructor is not defined, the compiler automatically creates a parameterless constructor, but if the constructor is already defined (with or without parameters), the constructor will no longer create a default constructor.

4. A constructor can be called with the This keyword, but only one is called, and the constructor must be placed at the very beginning, or the compiler will make an error.

5. Cleaning up

The garbage collector only knows to release the memory allocated through new, and if an object is not using new to acquire a particular area of memory, the collector cannot dispose of the object, so Java allows a method named Finalize () to be defined in the class.

About garbage collection, keep in mind three points: 1. Objects may not be garbage collected; 2. Garbage collection is not the same as "destruction". ; 3. Garbage collection is only related to memory.

Neither "garbage collection" nor "termination" is guaranteed to occur, and if the JVM does not face memory exhaustion, it will not waste time performing garbage collection to recover memory.

6. How the garbage collector works

Today's JVMs typically employ an "adaptive" garbage collection technique, which is automatic switching between stop-copy and Mark-sweep.

Stop-copy: Pauses the program's run, then copies all the surviving objects from the current heap to the other heap, all of which are garbage-free. After the objects are copied to the new heap, they are arranged in a compact order and then the new space can be allocated directly. The problem with this pattern: first, it takes two heaps and then daoteng back and forth between two separate heaps to maintain a much larger space than it actually is. Some JVMs are handled on an as-needed basis by allocating a few large chunks of memory from the heap, where the copy action occurs between the blocks, and secondly, when the program is stable, it only produces very little rubbish, not even garbage, but the copying is still happening, wasteful, and unnecessary. At this point, a new pattern of "tag-sweep" is introduced.

"Tag-sweep": All references are traversed at this time, and only a tag is made for the surviving objects, and when all the marked work is done, the process is slow and the heap space after cleanup is discontinuous and the object needs to be re-organized to get continuous space.

Java programming Idea: The fifth chapter, initialization and cleanup

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.