Java Memory Management

Source: Internet
Author: User

Java Memory Management

Some time ago, I gave a rough look at "deep into the second edition of Java Virtual Machine". It may be because I only work for one year, but it looks very hard. After all, if the details are specific, the Java Virtual Machine involves too much content. It may take another year or two to see if it is appropriate.

But after reading "Deep Java Virtual Machine", it will be much better to understand Java memory management. Next, let's take a look at Java memory management.

Note the following:

Programming can also reduce the chance of making mistakes. With garbage collection, programmers can avoid some bugs related to pointer and Memory leakage (this type of bug is usually concealed ). However, garbage collection requires more computing time. Garbage collection actually transfers the responsibilities of programmers to computers. It takes longer to run the garbage collection program.

In Java, the object is used by reference (the object is like a fatal poison, and the reference is like a tweezers used to extract the poison ). If there is no reference pointing to an object, we will no longer be able to call or process the object. Such an object will not be accessible (unreachable ). Garbage collection is used to release the memory occupied by inaccessible objects. This is the basic principle of garbage collection.

Early garbage collection adopted the reference counting mechanism. Each object contains a counter. When there is a new reference pointing to this object, add 1 to the counter. When the reference is removed, the counter minus 1. When the counter is 0, the object can be recycled.

However, one possible problem is that if there are two cyclic reference objects, for example, two objects reference each other, and there are no other references (pointing to A or B) at this time, in fact, we cannot reach these two objects by referencing them.

Therefore, we use the stack and static data as the root, starting from the root, and following all references, We can find all the reachable objects. That is to say, a reachable object must be referenced by the root or another reachable object.

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.