简体   繁体   English

当我使用new运算符分配大小为1k字节的内存时,操作系统在Windows中会做什么?

[英]what will OS do in Windows when I use the operator new to allocate memory with the size of 1k bytes?

It seems that memory leak occurs in my code, so I try to locate the place in my code which causes the memory leak. 似乎我的代码中发生了内存泄漏,因此我尝试在代码中找到导致内存泄漏的位置。

In the post Can't obtain accurate information of available memory in the heap I was told that OS may allocate large memory when a small memory is request to reduce the system call. 在“ 无法获取堆中可用内存的准确信息”一文中,我被告知,当请求较小的内存以减少系统调用时,OS可能会分配较大的内存。

Is it correct in Windows? 在Windows中正确吗?

What's relevant here, after seeing your other question, is not what happens when you allocate memory. 看到另一个问题后,这里相关的不是分配内存时会发生什么。 What matters is what happens when you release it. 重要的是释放它时会发生什么。 In particular a 1 KB allocation will never be released back to the OS, it is too small. 特别是1 KB的分配将永远不会释放回操作系统,这太小了。 It gets added to a list of free blocks, ready to be used by the next allocation of (about) the same size. 它被添加到空闲块列表中,准备供(大约)相同大小的下一次分配使用。

You cannot reliably detect memory leaks with VirtualQuery(). 您无法使用VirtualQuery()可靠地检测到内存泄漏。

If you use Visual Studio then use its built-in leak detection feature . 如果使用Visual Studio,则使用其内置的泄漏检测功能 There are plenty of other tools. 还有很多其他工具。

On most systems (including most recent compilers on Windows), the heap manager will allocate relatively large "chunks" of memory from the OS, then divide that up into pieces for use by the program. 在大多数系统上(包括Windows上最新的编译器),堆管理器将从OS分配相对较大的“块”内存,然后将其分成几部分以供程序使用。 That allocation from the OS will typically be at least tens of kilobytes. 来自OS的分配通常至少为几十KB。

Those large chunks of memory will be returned to the OS when the program ends execution. 当程序结束执行时,那些大块的内存将返回给OS。 It can happen sooner than that, but end of execution is the most common. 可以比这更早发生,但是执行结束是最常见的。

Each of those large chunks will be tracked by the OS as a single allocation (even though the heap manager will then break it up into smaller pieces for use by your code). 操作系统会将这些大块中的每一个作为单个分配进行跟踪(即使堆管理器随后会将其分解成较小的块,以供您的代码使用)。 Any that have been released back to the OS will show up as free memory blocks. 任何释放回操作系统的内容都将显示为空闲内存块。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我们使用new来分配内存时,操作系统会执行哪种记账? - What kind of book keeping does the OS do when we use new to allocate memory? 为什么运算符new []为数组的大小分配内存? - Why does operator new[] allocate memory for the size of the array? C ++在Windows XP中使用VS2005,可以使用new运算符动态分配的最大字节数是多少? - C++ What's the max number of bytes you can dynamically allocate using the new operator in Windows XP using VS2005? 新操作员无法分配内存 - new operator unable to allocate memory 当可以使用=运算符为其分配字符串时,为什么在分配之前使用new运算符为字符串动态分配内存? - why use the new operator to dynamically allocate memory for a string before assigning, when we can use the = operator to assign a string to it? 当使用new()运算符分配内存时,mudflap会抛出核心转储 - mudflap throws core dump when using new() operator to allocate memory 重载新运算符时,它不会为char指针分配内存 - when overloading new operator it doesnt allocate memory for char pointer 分配受控内存 - 以字节为单位的数组大小 - allocate controlled memory - array size in bytes C ++新运算符分配新内存 - C++ new operator allocate new memory 使用哪个 - “operator new”或“operator new []” - 在C ++中分配一块原始内存? - Which to use - “operator new” or “operator new[]” - to allocate a block of raw memory in C++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM