If you do not free memory that you malloc'd in a C program under Linux, when is it released? After the program terminates? Or is the memory still locked up until an unforseen time (probably at reboot)?
如果你没有释放你在Linux下的C程序中使用malloc的内存,它什么时候发布?程序终止后?或者内存是否仍然被锁定,直到不可预见的时间(可能是重启)?
7 个解决方案
#1
Memory allocated by malloc()
is freed when the process ends. However memory allocated using shmget()
is not freed when the process ends. Be careful with that.
当进程结束时,malloc()分配的内存被释放。但是,当进程结束时,不会释放使用shmget()分配的内存。小心一点。
Edit:
mmap() is not shmget() read about the difference here: http://www.opengroup.org/onlinepubs/000095399/functions/mmap.html http://www.opengroup.org/onlinepubs/009695399/functions/shmget.html
mmap()不是shmget()在这里读到的差异:http://www.opengroup.org/onlinepubs/000095399/functions/mmap.html http://www.opengroup.org/onlinepubs/009695399/functions/shmget html的
They are different system calls, which do very different things.
它们是不同的系统调用,它们执行非常不同的操作。
#2
Yes, the memory is freed when the process terminates.
是的,当进程终止时释放内存。
#3
malloc()
ed memory is definitely freed when the process ends, but not by calling free()
. it's the whole memory mapping (presented to the process as linear RAM) that is just deleted from the MMU tables.
malloc()ed内存在进程结束时肯定会释放,但不能通过调用free()来释放。它是从MMU表中删除的整个内存映射(呈现为线性RAM的过程)。
#4
In Linux (and most other Unixes) when you invoke a program from a command shell, it creates a new process to run that program in. All resources a process reserves, including heap memory, should get released back to the OS when your process terminates.
在Linux(以及大多数其他Unix)中,当您从命令shell调用程序时,它会创建一个新进程来运行该程序。当进程终止时,进程保留的所有资源(包括堆内存)都应该释放回操作系统。
#5
Memory 'allocation' has two distinct meanings that are commonly overlapped in questions like this.
记忆'分配'有两个不同的含义,通常在这样的问题中重叠。
- memory is made available to the process by the sbrk system call
- memory is assigned to some purpose within the context of the program by malloc()
sbrk系统调用使进程可以使用内存
通过malloc()将内存分配给程序上下文中的某些用途
the sbrk system call tell the kernel to get some more memory ready in case the process needs it. the memory is not actually mapped into the processes address space until immediately before it is written to. This is called demand paging. Typically the right to access this memory is never actually revoked by the operating system until the process exits. If memory becomes scarce then the kswapd (part of the kernel) will shuffle the least used parts off to disk to make room. The kernel can enforce a hard limit on the ammount of memory in a processes working set if you would like :)
sbrk系统调用告诉内核在进程需要时准备更多内存。直到写入之前,内存实际上并未映射到进程地址空间。这称为请求分页。通常,在进程退出之前,操作系统实际上不会实际访问此内存的权限。如果内存变得稀缺,那么kswapd(内核的一部分)会将最少使用的部分移到磁盘上以腾出空间。如果你愿意,内核可以对进程工作集中的内存量强制执行硬限制:)
the second context is what you are talking about when you call malloc/free. malloc keeps a list of available memory and hands chunks out to your functions when requested. if it detects that it doesnt have enough memory on hand to meet a request it will call sbrk to allow the process to access more.
当您调用malloc / free时,第二个上下文就是您所说的内容。 malloc在请求时保留可用内存列表和手动块。如果它检测到它没有足够的内存来满足请求,它将调用sbrk以允许进程访问更多。
when you look at the output of top you will see two numbers for a processes memory usage. One for the 'virtual size' and 'resident size', virtual size is the total amount that the process has requested access to. resident size is the amount that is actively being used by the process at the moment.
当你查看top的输出时,你会看到两个进程内存使用数。一个用于“虚拟大小”和“常驻大小”,虚拟大小是进程请求访问的总量。驻留大小是此过程主动使用的数量。
#6
The memory is released from the point of the program when it exits. It is not tied up in any way after that and can be reused by other processes.
存储器在退出时从程序的位置释放。在此之后它不会以任何方式捆绑,并且可以被其他进程重用。
#7
modern operating systems will release all memory allocated by a process when that process is terminated. The only situations where that might not be true, would probably be in very old operating systems (perhaps DOS?) and some embedded systems.
当该进程终止时,现代操作系统将释放进程分配的所有内存。唯一可能不是真的情况可能是在非常老的操作系统(可能是DOS?)和一些嵌入式系统中。
#1
Memory allocated by malloc()
is freed when the process ends. However memory allocated using shmget()
is not freed when the process ends. Be careful with that.
当进程结束时,malloc()分配的内存被释放。但是,当进程结束时,不会释放使用shmget()分配的内存。小心一点。
Edit:
mmap() is not shmget() read about the difference here: http://www.opengroup.org/onlinepubs/000095399/functions/mmap.html http://www.opengroup.org/onlinepubs/009695399/functions/shmget.html
mmap()不是shmget()在这里读到的差异:http://www.opengroup.org/onlinepubs/000095399/functions/mmap.html http://www.opengroup.org/onlinepubs/009695399/functions/shmget html的
They are different system calls, which do very different things.
它们是不同的系统调用,它们执行非常不同的操作。
#2
Yes, the memory is freed when the process terminates.
是的,当进程终止时释放内存。
#3
malloc()
ed memory is definitely freed when the process ends, but not by calling free()
. it's the whole memory mapping (presented to the process as linear RAM) that is just deleted from the MMU tables.
malloc()ed内存在进程结束时肯定会释放,但不能通过调用free()来释放。它是从MMU表中删除的整个内存映射(呈现为线性RAM的过程)。
#4
In Linux (and most other Unixes) when you invoke a program from a command shell, it creates a new process to run that program in. All resources a process reserves, including heap memory, should get released back to the OS when your process terminates.
在Linux(以及大多数其他Unix)中,当您从命令shell调用程序时,它会创建一个新进程来运行该程序。当进程终止时,进程保留的所有资源(包括堆内存)都应该释放回操作系统。
#5
Memory 'allocation' has two distinct meanings that are commonly overlapped in questions like this.
记忆'分配'有两个不同的含义,通常在这样的问题中重叠。
- memory is made available to the process by the sbrk system call
- memory is assigned to some purpose within the context of the program by malloc()
sbrk系统调用使进程可以使用内存
通过malloc()将内存分配给程序上下文中的某些用途
the sbrk system call tell the kernel to get some more memory ready in case the process needs it. the memory is not actually mapped into the processes address space until immediately before it is written to. This is called demand paging. Typically the right to access this memory is never actually revoked by the operating system until the process exits. If memory becomes scarce then the kswapd (part of the kernel) will shuffle the least used parts off to disk to make room. The kernel can enforce a hard limit on the ammount of memory in a processes working set if you would like :)
sbrk系统调用告诉内核在进程需要时准备更多内存。直到写入之前,内存实际上并未映射到进程地址空间。这称为请求分页。通常,在进程退出之前,操作系统实际上不会实际访问此内存的权限。如果内存变得稀缺,那么kswapd(内核的一部分)会将最少使用的部分移到磁盘上以腾出空间。如果你愿意,内核可以对进程工作集中的内存量强制执行硬限制:)
the second context is what you are talking about when you call malloc/free. malloc keeps a list of available memory and hands chunks out to your functions when requested. if it detects that it doesnt have enough memory on hand to meet a request it will call sbrk to allow the process to access more.
当您调用malloc / free时,第二个上下文就是您所说的内容。 malloc在请求时保留可用内存列表和手动块。如果它检测到它没有足够的内存来满足请求,它将调用sbrk以允许进程访问更多。
when you look at the output of top you will see two numbers for a processes memory usage. One for the 'virtual size' and 'resident size', virtual size is the total amount that the process has requested access to. resident size is the amount that is actively being used by the process at the moment.
当你查看top的输出时,你会看到两个进程内存使用数。一个用于“虚拟大小”和“常驻大小”,虚拟大小是进程请求访问的总量。驻留大小是此过程主动使用的数量。
#6
The memory is released from the point of the program when it exits. It is not tied up in any way after that and can be reused by other processes.
存储器在退出时从程序的位置释放。在此之后它不会以任何方式捆绑,并且可以被其他进程重用。
#7
modern operating systems will release all memory allocated by a process when that process is terminated. The only situations where that might not be true, would probably be in very old operating systems (perhaps DOS?) and some embedded systems.
当该进程终止时,现代操作系统将释放进程分配的所有内存。唯一可能不是真的情况可能是在非常老的操作系统(可能是DOS?)和一些嵌入式系统中。