In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
How to deeply analyze the details of C++ resource management, many novices are not very clear about this. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.
From the point of view of the system structure, Cpicket + supports three kinds of memory management, stack-based C++ resource management, heap-based dynamic management, and global area-based static management.
The positioning of the two languages of C++ resource management is different, and they take two completely different ways in resource management: one is GC, the other is RAII. GC makes the program build on a higher level of abstraction, which makes resource management more convenient and secure, while RAII retains the underlying capabilities of C and provides a simple and effective way of resource management with the support of C++ features.
We know that the fiercest criticism of C++ often comes from the C community, and in my opinion, C programmers can not accept virtual functions, do not accept templates, but is there any reason not to accept RAII? It can be said that RAII is a significant improvement of C++ compared with C.
Let's start with GC:
C # manages managed memory through CLR and manipulates managed memory indirectly with reference abstractions instead of pointers, allowing programmers to safely use resources at a higher level. This makes C# lose the ability to manage memory directly, but in exchange for the following benefits:
1. Type safety: it is possible to convert pointers of integers or other types into pointers of a specific type through type conversion, which means that pointers are non-type safe and it is up to the programmer to ensure the legitimacy of the memory space represented by pointers. The C# reference can be seen as a type-safe pointer, and the as operator ensures the type safety of the conversion.
two。 Memory demarcation: creating objects requires dynamic allocation of continuous memory space from the heap. Because the memory sizes of different objects are different, common * * matching and * * matching heap allocation algorithms will cause memory fragmentation in the heap. The existence of fragmentation makes the actual available memory less than physical memory, so fragmentation should be reduced as much as possible.
One direction is to design better memory allocation algorithms; the other is to adjust and optimize memory periodically. In C++ resource management, there is no general memory consolidation algorithm because the pointer represents the absolute address, while C # shields the pointer and references the Operand, which makes memory consolidation possible.
PS: this does not mean that Cstroke memory allocation is weaker than Candlespace memory allocation + resource management can design special memory allocation methods for certain types of objects, or even assign objects to a certain physical address space, which C# does not have.
Managed and unmanaged resources
In C#, resources are divided into managed resources and unmanaged resources. GC can automatically reclaim managed resources (such as managed memory) when reclaiming useless object resources, but unmanaged resources (such as Socket, files, database connections) must be explicitly released in the program.
The recycling of managed resources first requires GC to identify useless objects and then recycle their resources. General useless objects are objects that are not reachable through the current system root object and the call stack object. An important feature of objects leads to the complexity of useless object judgment: mutual reference between objects! If there is no mutual reference, the judgment of useless objects and real-time recycling can be realized through the simple and efficient way of "reference counting".
It is due to the existence of mutual references that GC needs to design more complex algorithms, which leads to the problem that it loses the real-time performance of resource recovery and becomes an uncertain way. For the release of unmanaged resources, C# provides two ways:
1.Finalizer: written like the destructor of C++ resource management, it is very different in nature. Finalizer is a Terminator called before the object is reclaimed by GC. The original intention is to release unmanaged resources here, but due to the uncertainty of the running time of GC, it usually leads to untimely release of unmanaged resources.
In addition, Finalizer may have unexpected side effects, such as: the recycled object is no longer referenced by other available objects, but it is made available internally by Finalizer, which destroys the atomicity of the GC garbage collection process and increases GC overhead.
2.Dispose Pattern:C# provides the using keyword to support Dispose Pattern to release resources. This releases unmanaged resources in a determined way, and the using structure provides exception security. Therefore, it is generally recommended to use Dispose Pattern, supplemented by checking in Finalizer, and release resources in Finalizer if you forget to explicitly Dispose objects.
It can be said that GC not only brings security and convenience to the program, but also pays a large price: first, it loses the real-time performance of managed resource recovery, which is fatal in real-time systems and resource-constrained systems; second, it does not unify the management of managed resources and unmanaged resources, resulting in conceptual separation.
One of the positioning of C++ is the underlying development capabilities, so it is not difficult to understand that GC has not become a language feature of C++. Although we can see GC in Central0x and various third-party libraries, GC is not that important to C++ and is a useful complement at best. C++ Resource Management is proud of C, and its RAII competes with C # GC.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.