About why Java requires garbage collection

Source: Internet
Author: User
Tags java reference

Why Java uses garbage collection and C + + does not, because in Java, all object variables are references, when a reference is overwritten by a new object, there is no reference to the original object, the object is "out of control."

In C + +, unless you use a special symbol & indicate that the variable name is a reference, the variable refers to the object, the object has a life cycle, and at the end of the lifecycle, the destructor is called to complete the memory release, whereas the C + + Reference object must point to the object, and it is clear that it should be referenced before it is declared Even if the reference later points to another variable, it does not cause the original object reference number to be 0, so that its destructor can still be called (through a variable pointing to the object), and the Java reference already points to other objects, so the address of the original object cannot be obtained, which is why Java has put the data in the heap.

There is no consideration of the c++new situation.

RAII Transcendental function declaration period:

About why Java requires garbage collection

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.