How Do You Manage Memory?
We haven't found a job yet. We've met a few families. I'm a little thick, I'm a little bit short on my blog.
Interviews such as the title have been encountered more than once or twice these days. The first time I was a little confused, it was a long time since I was not seen, I wasn't prepared, so I didn't give a full answer. However, even now, I have not been able to answer this question well in the interview. Perhaps this is no longer a problem for me, not a lot of thought, and I can solve it when I'm here. Here, take notes, and I can do better with this.
First of all, the question is why the management of memory needs to be considered, as C/C++ is mainly a very primitive method of manual release of memory. People make mistakes, and if they apply for memory and eventually forget to release, it can cause leakage. In addition, it is a question of how the memory that is shared between several modules can be correctly and reasonably released. If one of these modules is used for reference, and other modules are specially notified, then the actual release of the memory is determined by the circumstances, and there must be a close connection between the modules. Therefore, memory management is not handled with caution, and good memory management must be supported by appropriate methods and tools.
The first tool I have to talk about here is the memory pool. The memory pool is in fact the first to apply for a large memory, which is then divided into small memory units of several sizes according to certain rules. A small memory of the same size is tied up with something called FreeList, and several of the sizes make FreeList. When we need memory, we find a suitable FreeList, depending on the size required (e.g., 8 Bytes and 16 Bytes in the system, we need 9 Bytes memory, then pick 16 Bytes FreeList) and take a small piece of it. When the memory is no longer in use, the process of release is the process of returning it to FreeList. There are, of course, other ways to achieve it, but it's the same thing, the same thing.
The memory pool technology does not relieve programmers of memory releases, and what benefits does it bring? As the system it was to consider had fewer distributors, it would generally be more efficient. At the same time, some RAM alignment was ensured, not to the extent that it was more efficient to locate CPUs, but to a certain extent to avoid RAM debris. If the memory is not all-inclusive, the number of consecutive empty blocks in the final memory becomes smaller, and when large pieces of memory are allocated, they fail because they do not have enough continuous free memory size. Memory pool technology for the matching of memory distributions can significantly alleviate this problem.
In fact, memory pool technology is present in many open source memory distributors, even in the operating system. There is no need to self-regulate the memory pool unless it is being used at extremes. I think it would be better if there were a situation like this, which would probably not be solved by memory pool technology, with more static, fixed distribution options.
If you have a homemade memory pool, you can integrate a memory tracker at the same time. The memory tracker helps programmers to find the memory leak. As already mentioned, the leakage was due to the failure to release the memory in a timely manner. The tracker Hook is on the dispenser and registers the allocation for each memory. Meanwhile, Hook cancelled its pre-release registration on each release operation. When the process or module exits, it is possible to view items in the tracker that are not registered and, if so, to indicate a leak in the memory. According to the registration information, we can know where the memory of the application was not finally released, and we can then find a place to place the release code. The memory tracker is a tool that is independent of the memory pool and does not depend on its existence.
For many applications, we can use the linkage structure of their data to alleviate the pain of the release of the manual memory. In these systems, the data tend to be parent-son linkages: for example, in the GUI system, the button is a sub-object of the form (not a sub-category); Also, as in the game scene tree, the object entity is placed on the root of the scene as a sub-object, and the object entity can be placed on the sub-object. For this structure, the release of a child object can be done by handing it to the parent. In this way, as long as a node in a tree-shaped structure is no longer in use (i.e. needs to be removed and released), it, together with its descendants, can be released in a way that runs through the trees.
In many cases, the relationship between the object does not present a structure of rules like a tree. These objects are held in different modules, and the relationship between the modules is often parallel. How, then, can memory release be reduced without creating a coupling? This allows for the use of a reference counting technique: the memory count is marked as 1 per application for a single memory; Add 1 to each reference transfer for this memory; (a) The counter is reduced by 1 each time the memory is dequoted; When the counter is down to zero, the memory can really be released. Thus, the need for release was achieved within the reference counter, independently of the module of the specific reference object, and was direct, and there was no problem of adding inter-module coupling.
For the simplest reference counter to achieve it, one or less operations often require us to do it manually. Even if one or less are to be hidden in the construction, resolution, grant operators, we still need a manual release operation to trigger a further reduction mechanism. In order to solve this problem, we can introduce smart pointer technology: the ink object automatically triggers the resolution when it's in the action area, and the ingenuity refers to the like as a kind of object, which automatically triggers the reduction of an operation. In fact, the new C++1x std::shared_ptr is an advanced intelligence pointer that combines the reference count.
One problem with the reference to the counting technique is that once there is a circular reference relationship between objects, there is a problem and there is no normal release. The object cannot be reduced to zero because of the number of references to the object and because of the existence of a loop reference. This will require manual disruption of the loop reference to allow for normal release. And in the real code, we will try to avoid the emergence of circular references, relying on a good program structure.
For the management of the memory of C/C++, there is generally no discussion of GC waste recycling. I do not classify the reference count as GC, which here refers to the type of GC that uses tagging and so on. They are generally less effective than the quotations, and the C/C++ language, which is more of an implementation-efficient language, is less used with GC. While the GC library is also available, once the GC library is used, the memory of the GC library allocation must be managed by the GC library and supported by a lack of grammatical language. As for details, the different GC libraries use different algorithms, and the language supporting GC is not so fundamentally different as it is and its characteristics can be referred to in other languages. One advantage of the GC comparison of the reference to the counting mechanism is that there is no fear of circular reference.
There is no generalization of what means are used to address memory management issues, and there is no best approach. Optimizing the program structure, which allows for control of memory by manual application and release at large structural nodes; A structured target relationship can be designed in the system logic backbone and the release of the object can be carried out systematically. However, it is designed to reduce unnecessary mechanisms, such as not to abuse the initialization of Lazy, to fix the order of the life cycle of the object and to reduce the likelihood of revolving references. Where complex codes are needed, they should be where performance is strictly required. But even when the game is programmed, not all places have a strong demand for performance. Where there is a real need, we take special measures to manage our memory, and the cost is not just to achieve coding, but to have enough tests, all on the basis of what is actually going on. And in most places, we're going to use a ready-made, price-for-money approach.