Memory Allocation [closed]
I used the memory allocation code in a C file and I didn't free it, then the file was deleted. My question is: does a memory leak occur even after the file is deleted without freeing, knowing that the memory of the partition that contains the OP and the programs keeps running out of memory and I have already used "Disk Cleanup"?
Note: Someone told me that once you I restart the computer, everything in the dynamic memory will vanish, is that true?
A memory leak will only continue as long as the program is running. Once the program exits, the operating system should free up the memory for you, even if you didn't. There is no need to delete any files from your haddisk.
I believe you are confused about the differences between storing something on disk and in memory.
For example, when you have int x = 4;
, x
is stored in RAM. This is not the same place your files, such as word documents, are stored. If you reboot your computer, this x
is lost. Also, when your program stops running, the operating system cleans up the space that x
was taking up.
When a program is leaking memory, it is basically taking up more RAM than it actually needs, and as time goes on it will bloat in size. However, once the program stops running, the operating system cleans it all up since it is no longer being used. There are no permanent effects.
When a program is terminated, the OS will free up all the resources it allocated the program. In your case, the memory leak would be within the program's allocated memory, and after it's finished, the OS will reclaim all that memory that was "leaked".
Also you should note here that when you allocate memory in a multi-process environment and don't free the memory and continue execution, it results in serious performance degradation of the system. This leaves other process less memory for allocation as your process will have eaten up the memory and not freed it. Hence its always advised to free the memory after use. We should not wait till the process has finished execution.
Yes, deleting the file from the FS has nothing to do with what your C program will see. And any malloc that isn't freed is freed at program termination. FYI, it's kinda dangerous to be writing C code without understanding those basics, you may want to do some reading or try something like python or even java that will hide those responsibilities from you first.
But you should understand stack, heap, virtual memory, and i/o, and pass by value vs pass by reference (pointer) to start being effective in C. Understand what a buffer overflow is. There's a lot to learn.
精彩评论