What is the need for freeing memory in C?

Perhaps I was wrong, but as far as I know, the OS clears the memory after exiting the program or crashes.

If so, how useful is freeing up memory at the end of the program? I understand that if a program is launched and memory deallocation is ignored, then the memory may become โ€œfullโ€, but if the program has already completed and the OS will free all the memory used by the program, what is the manual release point for this memory?

+6
source share
6 answers

Quote from Problems with freeing memory in C :

The operating system is responsible for maintaining application resources, including its memory. When the application terminates, the operating system is responsible for reallocating this memory to other applications. The memory status of completed applications, garbled or undamaged, is not a problem. In fact, one of the reasons an application may terminate is because the memory is corrupted. If the program terminates abnormally, cleaning may not be possible.

With this in mind, there may be other reasons why the memory should be freed when the program usually ends:

  • A conscientious programmer may want to free up memory as a quality problem. It is always a good habit to free memory after it is no longer needed, even if the application terminates.
  • If you use the tool to detect memory leaks or similar problems, then freeing up memory will clear the output of such tools.
  • On some less complex operating systems, the operating system cannot automatically recover memory, and the responsibility for recovering memory may depend on the programs.
  • In addition, a later version of the application may add code to the end of the program. If the previous memory was not freed, problems may occur.
+4
source

Purity.

You could, of course, not worry about going through the cleanup and letting the system handle it. However, if you do this, in essence, it is impossible for you to track memory leaks in your program, since you cannot run it and see if there is anything left in the end. If, on the other hand, you provide a clean shutdown, you can find out if there are any leaks by running it and seeing if there is anything left at the end. Since for any non-trivial program that is likely to work for a while, memory leaks are something that should be avoided, doing it in a clean way leads to advantages.

In addition, it is also part of ensuring that your program shuts down completely with any persistent state remaining in the correct state and any external resources freed up (although most modern OSs will clean it these days) because you are going through an orderly and not just cut and work.

+4
source

Another perspective:

At the end of your program, there is usually no specific practical reason for clearing memory. But this is not how you usually develop non-trivial programs that do not fit on any page! Parts of a program should usually be designed to work well, regardless of when they are launched during program execution, and usually without knowledge of the rest of the program. They cannot constantly allocate and load memory, because in general they do not know how often they will be called, or how much code will follow them, or for how long. After all, many end-user applications are designed to run a potentially endless โ€œmain loopโ€.

So, a program that completely cleans itself is not a goal, it is one of the smaller consequences (reward for obsessive!) From developing your entire program from scratch. It can also serve as a warning flag that at some stage of the design process was not planned if at the end there are resources left at the end.

+1
source

You can not always depend on the system, although most modern do it. AFAIK Windows 95 did not do this, so perhaps some wild embedded systems also do not clean up after processes.

Other than that, it's a good habit. Sometimes you reorganize things from main into some modules and try to reuse them. Itโ€™s good not to forget to loose things then.

0
source

Yeah. This is true. If there is any object that will live throughout the entire life of the application, it is reasonable to select it once at startup and never delete / delete the object. However, if you isolate something as part of a calculation where there is no need to save the object throughout the entire life cycle of the program, then it is important to free the object when it is no longer needed; otherwise, your program will slowly exit the available memory as objects leak out over the entire life cycle of the program.

It should be noted that even if you free up your memory, there is always a chance that your program will be forcibly terminated (for example, by a signal) before you have a chance to free up memory. Therefore, this is one reason not to be super paranoid about it. At the same time, for consistency and maintaining good habits, both for regular distributions / deallocations and for distributing / releasing long-lived objects, I usually prefer free / cleaned long-living objects similar to ordinary objects. In this case, you must balance the desire to be clean with a quick shutdown and the ability to create the correct dumps / logs in the event of a forced shutdown. For example, the time taken to free large long-term data structures can be due to the time required to write a core dump, in which case it is probably better to write a kernel.

0
source

In the general case, if you do not free memory when it is no longer needed, you can run out of system memory. This is what is known as a memory leak.

This is most obvious in programs that run for a long period of time ... when you start and allocate an increasing amount of memory (for example, call malloc), more and more memory resources are consumed by the program. If they do not return to the system, the operating system may start working from memory to provide it to applications when "malloc" is called.

Some operating systems and programming languages โ€‹โ€‹have functions that can either mitigate or prevent this.

Java, for example, does not allow you to directly call malloc for free. Instead, when you create objects, memory is allocated for them in the java runtime. When you are done with variables (that is, when they go out of scope), they are marked as garbage collection. Typically, a task or garbage collection function is run periodically and will be freed from all memory associated with variables that are no longer in use (ie. "Cleans garbage").

Some operating systems also support run-time limits on processes and / or threads that prevent them from consuming "all" system resources. If any of these restrictions is affected, the operating system may stop the program.

Hope this helps, - J.

0
source

Source: https://habr.com/ru/post/970863/


All Articles