Object cleanup Dilemma: Who is responsible for cleaning up?

Source: Internet
Author: User

Each object requires a resource to "survive," where the most compelling resource is memory. If you no longer need to use an object, you must clear it so that the resources can be freed for use by other objects. If there is a very simple problem to solve, how to clear the object does not appear to be prominent: we create the object, call it when it is needed, and then clear it or "destroy" it. But on the other hand, the problems we usually encounter are much more complicated than that.
For example, suppose we were to design a system to manage air traffic at an airport (the same model might also be good for managing a warehouse container, or a set of video rental systems, or pet rooms for a pet shop.) The initial look seemed simple: construct a set to hold the plane, then create a new aircraft and place it in a collection. This is the case with all aircraft entering the air traffic control area. As for removal, simply delete it when a plane leaves the area.
But it's not that simple, and it may require a different system to record data about airplanes. Of course, unlike the main functions of the controller, the importance of the data may not be apparent at first. For example, this record may reflect the flight plans of all small aircraft leaving the airport. So we got another set of small airplanes. Once a plane is created, if it is a small plane, it must also be placed into the set. Then in the system idle time, the object in this collection needs to do some background processing.
The problem now seems more complicated: how do you know when to delete objects? After you run out of objects, some of the rest of the system may still be functioning. The same problem can occur in a number of other situations, and in a programming system (such as C + +), you must explicitly delete an object after it has been used, so the problem becomes extraordinarily complex (note ⑥).

⑥: Note that this is only true for objects created in the memory heap (created with the new command). On the other hand, for the problems described here and all the other common programming problems, the object is created in the memory heap.

In Java, the garbage collector has designed to take into account the release of memory (although this does not include removing other aspects of an object). The garbage collector "knows" when an object is no longer in use, and then automatically frees the memory space occupied by that object. In this way, plus the fact that all objects inherit from a single root class object, and since we can only create objects in one way in the memory heap, Java programming is much simpler than C + + programming. We only need to make a few choices to overcome the many obstacles that existed.

1. The impact of garbage collectors on efficiency and flexibility
Since this is such a good means, why in C + + has not been fully played? Of course we have to pay a certain price for the convenience of this programming, the cost is the cost of the running period. As mentioned earlier, in C + +, we can create objects in the stack. In this case, the object is automatically purged (but does not have the flexibility to create the object as it pleases during run time). creating objects in the stack is one of the most efficient ways to allocate storage space for an object, and is the most efficient way to release those spaces. Creating objects in the memory heap (HEAP) can be a much more costly effort. If you always inherit from the same underlying class and make all function calls have the "homogeneous polymorphism" feature, you will inevitably have to pay a certain price. But the garbage collector is a special problem because we can never be sure when it starts or how long it will take. This means that there is an incoherent element during the execution of Java programs. So on some special occasions, we have to avoid it--for example, when the execution of a program has to be stable and coherent (usually called "real time", although not all real-time programming problems require this--annotation ⑦).

⑦: According to some of the technical readers of this book, there is a ready-made real-time Java system (www.newmonics.com) that can actually guarantee the efficiency of the garbage collector.

The designer of the C + + language once made a request to C programmers (and was very successful) and did not want to add any features that might affect the speed or use of C + + anywhere in the language where C can be used. The goal is achieved, but the cost is that C + + programming is inevitably complicated. Java is simpler than C + +, but the cost is efficiency and a certain degree of flexibility. But for most programming problems, Java is certainly the first choice for us.

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.