Note: This is a reply I posted on the school BBS, which is discussed here. Original article: Sender: true (more cruel to yourself), email area: c_and_cpp Question: will the new space system be automatically recycled? Mail site: Harbin Institute of Technology (Fri May 14 12:07:43 2004), email After the program runs, I think the system will automatically recycle it, right? Are all operating systems the same? Where can I find the theoretical basis? My first reply is: Sender: iamxiaohan (Han-System Programmer), email area: c_and_cpp Question: Re: will the new space system be automatically recycled? Mailing site: BBS Harbin Institute of Technology (Fri May 14 12:16:51 2004) This mainly depends on how the operating system operates, For general operating systems, the process forcibly recycles all allocated resources when it is destroyed, Therefore, you do not need to delete, but using Delete will allow the system to recycle resources in a timely manner. This is a good solution. Programming habits [Mentioned in the masterpiece true (more cruel to myself :] : After the program runs, I think the system will automatically recycle it, right? : Are all operating systems the same? Where can I find the theoretical basis?
The following is Sun's reply. Sender: Sun (large lightbulb), email area: c_and_cpp Question: Re: will the new space system be automatically recycled? Mail site: Harbin Institute of Technology (Fri May 14 13:08:12 2004), email [Mentioned in the masterpiece of iamxiaohan (Han-System Programmer :] : This mainly depends on how the operating system operates, : For general operating systems, the process forcibly recycles all allocated resources when it is destroyed, Is this by reference? :) : Therefore, you do not need to delete, but using Delete will allow the system to recycle resources in a timely manner. This is a good solution. :...................
The following is a reply to sun. You are welcome to continue to discuss or correct it.
|
Sender: iamxiaohan (Han-System Programmer), email area: c_and_cpp
Question: Re: will the new space system be automatically recycled?
Mailing site: BBS Harbin Institute of Technology (Sat May 15 09:25:05 2004)
According to n's book, for a modern operating system, it can be deleted. In fact, the new operation is
Finally, it will be linked to malloc by the compiler, which is called malloc, while malloc is a system call,
Used to allocate memory. Therefore, the memory allocation is ultimately completed by the operating system rather than the compiler.
It is only an action to call the system call to allocate memory. The operating system determines how to allocate memory.
.
Generally, the operating system has a large free memory zone, which is also called a heap.
This memory allocation is also called heap allocation. When you use new to generate an operating system call,
When memory allocation is required, the operating system checks the heap and retrieves a suitable size and is marked
"Unallocated" memory, return its pointer, and mark this memory as "allocable". When you call
When the delete operation is used, the operating system clears the "allocable" mark of the memory so that the memory can be allocated again.
Memory. This is one of the simplest memory allocation actions.
Yes. New is like lending a loan to a bank. Delete is like canceling a loan when you pay back the money.
I used to ask why I can still operate this memory after the delete operation without a segment error? From
The description above shows that the memory after the delete operation is still in the memory space, but the operating system is re-installed.
Marked as "allocable", so you can also operate on it, but you do not know
When it will be allocated. If it is allocated, your operations on this block of memory will be very dangerous.
.
Of course, a robust operating system can be accessed when you access the memory that does not belong to you (or the access is marked
When the memory is "allocable"), an error interruption or error trap is generated, which mainly depends on how the operating system manages
Yes. A very robust operating system may detect all accesses to heap memory.
If you access a block that is not your memory, an error will occur. This kind of detection is time-consuming, so there are
Some operating systems can choose to perform such detection only on the allocated memory.
You can continue to perform operations on the memory until it is remarked as "allocable" by Delete,
Once the memory is re-allocated, the memory is marked as "allocated", and you continue to use this
Block memory, the operating system reports an error. In this way, sometimes when you access the deleted memory
This is because the memory has not been split before, and the operating system does not detect your
Whether the access part of the memory is valid, so there is no error, but the next time you access the memory, the memory has been allocated to other
So the operating system checks the memory, and an error occurs.
For some operating systems that adopt page-based management, the underlying memory is allocated one page (4 kb) at a time. However
And then implement a layer of advanced memory management on it, so that you can allocate the specified size of memory instead of a whole
When all spaces on a page are deleted, that is, all spaces are marked as "allocable", the operating system
The page will be marked as "nonexistent", removed from the memory, and removed from the page table.
In this way, when the access is re-accessed, the CPU will produce page-missing interruptions, and the operating system will handle such interruptions.
, Processing may re-load this page, or may produce an error, because the operating system finds that you have
After deletion, you do not have the permission to access this page again. to access this page, you need to create a new one.
This is a very interesting phenomenon: You deleted a piece of memory and then kept accessing it. The first 99 times are acceptable, while
100th times won't work. This may be because there are other processes on this page
There is a delete space, and 100th times, other processes have negotiated all the delete operations, so the Operating System
The page is invalid. When you access the page again, the operating system will find that you are not authorized to access the page. Therefore
, 99 runs of the correct program. It will be correct if you do not run 100th times! The advantage of this operation is that
The system checks whether memory access is valid only when page disconnection occurs.
Annoying operations, reduce the number of detection times, of course, can increase the system efficiency.
When the operating system allocates memory, it will establish a data structure to keep the allocated memory very much.
Multiple Information, such as the first address and size of the memory. Therefore, there will be such an explicit image, when malloc
Or new memory, you need to specify the size of the memory to be allocated, but when you are free or delete, but not
You only need to specify the size, and you only need to give the first address pointer of the memory. This is mainly because the Operating System
The system only needs to re-mark this memory as "allocable", and its memory size is allocated in the operating system
The operating system automatically records it.
In the modern operating system, when memory is allocated, it also records the process in which the memory is allocated.
When the program exits, it can forcibly reclaim all space allocated to the process. Of course,
If an operating system does not record the memory and allocate it to anyone when allocating the memory
You cannot forcibly recycle a piece of memory. In this case, you must use Delete. This situation mainly occurs in ancient times.
On the Dong-level operating system, because the memory was very limited at that time, the operating system was not allowed to use it so generously.
To store the information that needs to be stored. However, for now, save the memory allocated to
The process is really a small case, so now you can not pay much attention to whether delete is used,
Using Delete will promptly release the memory space, but it will only be released when the process is finished without using Delete,
The difference between the two is naturally unnecessary.
However, I would like to remind you that the forced recovery of the operating system is not the same as what we often call the "garbage collection mechanism !",
Let's look at Java, its garbage collection mechanism. It allocates memory continuously as long as it can be allocated, such
If it cannot be allocated, it will find out which memory is no longer used by the program, and it will recycle it.
The system's forced recovery mechanism does not have this capability, and it does not know which memory is no longer used by the program
For it, as long as there is no Delete memory, it thinks that the program is still using it. Simple 1
Point. For Java or C Programs, if only new is used instead of Delete, Java can
The memory is recycled during the running process, but the C program does not work. After the program is running
Uniform one-time forced collection, Java is side-by-side, the program can run forever, and C is only used, when the most
When the program is no longer usable, it will have to be killed, and then the operating system will recycle it.
In Java, the for (;) New..., Java program can continue to run, while C is always used up
.
So can all new data be deleted? This is also not surprising, some operating systems will be in the memory
In addition to the memory allocation pointer and size, you must retain a type parameter
Mark whether the memory is used independently by the process or shared by multiple processes. If it is used separately, you can also
When the process ends, it is automatically recycled by the operating system; if it is shared by multiple processes, of course this will not work, because
, Although the process has ended, but there are still processes in use, so it cannot be recycled.
In the allocation of multi-process shared memory, this is probably achieved by reference count, each memory has
Reference Count value. The initial reference count value is 0, and the new value is + 1 at a time, and the delete value is-1 at a time. Only when
When the value is 0, it will be actually recycled by the operating system. In this case, if you are new but there is no Delete
The reference count cannot be zero, and the memory will be retained and cannot be recycled by the operating system.
In this case, you must delete it after new.
Memory Allocation is done by the operating system, rather than by the compiler.
Is a system call that calls the memory allocation function of the operating system to complete the memory allocation function.
How to allocate objects is determined by the operating system and cannot be determined by the compiler. Therefore, this is a platform
And compiler-independent.
Each operating system has different memory allocation policies. The CPU provides these functions.
In terms of functions, how can we achieve this?
There is a standard answer. Linux uses an implementation method to meet its needs, while windows
To meet its needs, another implementation method is adopted. Like pyos, my idea is to provide a more powerful feature.
Point memory management, for example, a process can apply to allocate a shared memory zone, but only apply to allocate
The memory has the write permission, while other processes only have the read permission. This is another memory allocation policy.
I think we can raise a requirement and discuss how to implement it.
On Linux or windows, rather than discussing how to implement Linux or windows.
It is to discuss how to implement it, or to discuss how to implement it in the book.
Http://purec.binghua.com/Article/ShowArticle.asp? ArticleID = 130