linux 用户进程结束后 malloc申请的内存会自动释放吗,当程序退出后,动态申请的内存会自动释放吗...

news/2024/7/7 20:08:25

我们知道程序在运行的过程中是需要占用一定内存的,一般程序所需要的内存由操作系统来分配,由操作系统分配的,自然需要由操作系统回收。但是在实际开发中,用户可以通过一些函数人为地申请内存,再由用户来释放,例如通过C语言的malloc函数动态开辟内存。现在就有一个问题,如果用户没有用free函数释放内存空间,那么这块内存是不是就永远被占用了呢?当程序退出后,动态申请的内存会自动释放吗?

stackoverflow有人问了这么一个问题,下面这段程序执行完毕后,malloc的内存会释放吗

int main () {

int *p = malloc(10 * sizeof *p);

*p = 42;

return 0;  //Exiting without freeing the allocated memory

}

赞数最多的这么回答:

It depends on the operating system. The majority of modern (and all major) operating systems will free memory not freed by the program when it ends.

Relying on this is bad practice and it is better to free it explicitly. The issue isn't just that your code looks bad. You may decide you want to integrate your small program into a larger, long running one. Then a while later you have to spend hours tracking down memory leaks.

Relying on a feature of an operating system also makes the code less portable.

因此这些内存是会被大部分现代操作系统释放掉的,这些系统包括

MacOS X, Linux, all recent version of Windows, and all currently manufactured phone handsets

一些老的系统不会释放:

If you're programming on microcontrollers, on MacOS 9 or earler, DOS, or Windows 3.x, then you might need to be concerned about memory leaks making memory permenantly unavailable to the whole operating system.

解释如下:

Most modern operating systems employ a memory manager, and all userland processes only see so-called virtual memory, which is not related to actual system memory in a way that the program could inspect. This means that programs cannot simply read another process's memory or kernel memory. It also means that the memory manager will completely "free" all memory that has been assigned to a process when that process terminates, so that memory leaks within the program do not usually "affect" the rest of the system (other than perhaps forcing a huge amount of disk swapping and perhaps some "out of memory" behaviour).

This doesn't mean that it's in any way OK to treat memory leaks light-heartedly, it only means that no single program can casually corrupt other processes on modern multi-tasking operating systems (deliberate abuse of administrative privileges notwithstanding, of course).

此外The Linux Programming Interface书中有这么一段:

When a process terminates, all of its memory is returned to the system, including heap memory allocated by functions in the malloc package. In programs that allocate memory and continue using it until program termination, it is common to omit calls to free(), relying on this behavior to automatically free the memory.  This can be especially useful in programs that allocate many blocks of memory, since adding multiple calls to free() could be expensive in terms of CPU time, as well as perhaps being complicated to code.

我们在学习C语言时,老师就告诉我们,动态开辟内存之后,要及时回收,不然就会造成内存泄漏。现在想想内存泄漏是指在当前进程在堆中分配了空间后,完成了相关的操作,没有及时释放掉(不再需要此空间),并且进程没有结束!

我们在编写循环程序时要格外注意这一点,在每一次循环操作时,如果动态申请了内存,一定要及时释放,不然下一次循环又会吃掉相应的内存空间。


http://www.niftyadmin.cn/n/2001632.html

相关文章

如何做好IT运营.

定义IT管理的重点在于业务策略与 IT 部门提供的服务之间的一致性。IT 管理可建立必要的管理机制来确保可预测的 IT 服务交付,从而确保业务流程和 IT 流程之间的联系。IT 管理传统上属于CIO、CEO和一些 IT 和业务线(line of business,LOB&…

linux 对比2个目录,linux – 比较2个目录并复制第3个目录中的差异

使用–compare-dest.从手册页:–compare-destDIR –This option instructs rsync to use DIR on the destination machine as an additional hierarchy to compare destination files against doing transfers (if the files are missing in the destination direct…

Java HashMap工作原理及实现

原文出处: Yikun 1. 概述 从本文你可以学习到: 什么时候会使用HashMap?他有什么特点?你知道HashMap的工作原理吗?你知道get和put的原理吗?equals()和hashCode()的都有什么作用?你知道hash的实现…

linux c程序设计大全 下载,The C programming Language[C程序设计语言]PDF

内容简介 Presents a complete guide to ANSI standard C language programming. Written by the developers of C, this new version helps readers keep up with the finalized ANSI standard for C while showing how to take advantage of Cs rich set of operators, …

Metro Win8风格的按钮(Filp翻转)

原地址->http://www.cnblogs.com/yk250/p/5661093.html 介绍:简约而不简单....颜色可随意调制,最好用Blend工具。 效果图如下:话说这个图会不会太大了点 0_0 外观简单,制作也十分简单&#x…

linux学习密码管理,LINUX学习 usermod命令 , mkpasswd命令,用户密码管理

一、usermodusermod是更改用户属性的命令,与userid类似格式:usermod -u 123 usernameusermod -g 123 username二、mkpasswd系统默认下是没有这个工具的,我们需要手动yum安装一下mkpasswd用来生成随机密码字符串。有时我们需要生成指定的长度密…

利用 LINQ的skip和Take 方法对List实现分页效果

1 var testListnew List<string>(); 2 var resulttestList.Skip(pageSize * (pageNum - 1)).Take(pageSize); 3 //skip是跳过的条数&#xff0c;pageSize*(pageIndex-1),Take 是返回多少条数据&#xff0c;也就是pageSize! 转载于:https://www.cnblogs.com/dansediao/p/5…

linux 软件开发环境搭建,Linux Ubuntu 基本开发环境搭建及配置

本文主要说linux ubuntu 基本开发环境的搭建&#xff0c;主要有以下的软件或者环境的配置。RubyMIneChromeskypeopen-jdkgithubnode.jsyeoman在安装ubuntu系统以后首先进行的是系统软件的更新&#xff0c;以便更好的安装软件。blackblack:/$ sudo apt-get updatesudo是允许用户…