UNIX vs Windows内存释放 [英] UNIX vs Windows memory deallocation

查看:75
本文介绍了UNIX vs Windows内存释放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的理解是,在unix中,释放内存时,内存不会返回到操作系统,而是停留在进程中,以再次用于下一次对malloc的调用.

My understanding is that in unix, when memory is freed, the memory doesn't get returned back to the operating system, it stays in the process to be used again for the next call to malloc.

在Windows上,我了解到内存实际上已返回操作系统.

On windows, I understand that the memory actually gets returned to the operating system.

这两种做事方式之间是否有很大的不同,或者只是两种做同一件事情的不同方式?如果这两种方法都有优点/缺点,那么它们是什么?

Is there any big difference between these two ways of doing things or are they just two different ways of doing the same thing? And if there are any pros/cons to these two methods, what are they?

编辑:感谢您的澄清.我一直以为这是OS的事情(因为在类似UNIX的系统中进程似乎从未减小,但是在Windows中却是如此).

Thanks for the clarification. I had always thought this was an OS thing (since processes never seem to decrease in size in UNIX-like systems, but do in windows).

推荐答案

Windows和Unix在这方面没有太大区别.

There isn't much difference between Windows and Unix with respect to that.

在这两种情况下,都有两个分配级别.操作系统以较大的块(一页或更多;在x86上,页面大小通常为4096字节)为进程分配内存.在流程中运行的运行时库将细分该空间,并将其一部分分配给您的代码.

In both, there are two levels of allocation. The operating system allocates memory to the process in large chunks (one page or more; on x86, the page size is usually 4096 bytes). The runtime libraries, running within the process, subdivide this space and allocate parts of it to your code.

要将内存返回操作系统,首先必须将从这些大块之一分配的所有内存释放到运行时库.然后,运行时库可以根据需要告诉操作系统释放该内存块.

To return the memory to the operating system, first all the memory allocated from one of these large chunks has to be released to the runtime library. The runtime library then can, if it wants, tell the operating system to release that chunk of memory.

在Linux上,您具有brkmmap. brk控制分配给您的进程的大块内存的大小;您可以扩大或缩小,但只能在一端.传统上,malloc在需要分配更多内存时会扩展此内存块,并在可能的情况下进行收缩.但是,收缩并不容易.最后,它只需要一个一字节的不适当的时间分配就可以使它无法收缩,即使该分配之前的所有内容都已释放.这是"Unix不释放内存"模因的来源.

On Linux, you have brk and mmap. brk controls the size of of a large chunk of memory allocated to your process; you can expand or shrink it, but only at one end. malloc traditionally expands this chunk of memory when it needs more memory to allocate from, and shrinks it when possible. However, shrinking is not easy; it takes a single one-byte ill-timed allocation at the end to make it unable to shrink even if everything before that allocation has been freed. This is the source of the "Unix doesn't release memory back" meme.

但是,还有匿名mmap.匿名mmap向操作系统请求一块内存,该内存可以放在进程内存空间中的任何位置.即使不再需要以后的分配,也可以在不再需要时轻松返回该块. malloc也使用mmap(尤其是对于较大的分配,整个内存块在释放后可以很容易地返回).

However, there's also anonymous mmap. Anonymous mmap requests a chunk of memory from the operating system, which can be placed anywhere in the process memory space. This chunk can be returned easily when it's not needed anymore, even if there are later allocations which weren't released yet. malloc uses also mmap (particularly for large allocations, where a whole chunk of memory can be easily returned after being freed).

当然,在Windows和Linux上,如果您不喜欢运行时库中的内存分配器(或多个分配器)的行为,则可以使用自己的内存,从操作系统中查询内存,然后按所需方式对其进行细分. (或有时从另一个分配器中查询内存,但以更大的块为单位).一种有趣的用途是为与任务相关联的所有内存(例如,Web服务器请求)分配一个分配器,该分配器在任务结束时被完全丢弃(无需单独释放所有块);另一个有趣的用途是为固定大小的对象(例如,五字节对象)分配器,它避免了内存碎片.

Of course, on both Windows and Linux if you do not like the behavior of the memory allocator (or allocators) from the runtime libraries, you can use your own, asking memory from the operating system and subdividing it the way you want (or sometimes asking memory from another allocator, but in larger blocks). One interesting use is to have an allocator for all the memory associated with a task (for instance, a web server request), which is completely discarded at the end of the task (with no need to free all the pieces individually); another interesting use is an allocator for fixed-size objects (for instance, five-byte objects), which avoids memory fragmentation.

这篇关于UNIX vs Windows内存释放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆