好的,所以,我写了一些代码来检查运行时有多少内存可用。下面是一个完整的(最小的)cpp 文件。
注意:代码并不完美,也不是最佳实践,但我希望您可以专注于内存管理而不是代码。
它的作用(第一部分):
- (1) 在一个内存中分配尽可能多的内存 堵塞。清除那段内存
- (2) 分配尽可能多的中型 block (16MB) 尽可能。清除那段内存。
--> 这很好用
它的作用(第二部分):
- (1) 在一个 block 中分配尽可能多的内存。清除那段内存
- (2) 分配尽可能多的小块 (16kb)。清除那段内存。
--> 这很奇怪!
问题是:如果我再重复一遍,我只能分配 522kb 用于继续运行的 secons ---> ?
这不会发生,如果分配的 block 有例如16MB 大小。
你有什么想法,为什么会这样?
// AvailableMemoryTest.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <vector>
#include <list>
#include <limits.h>
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
auto determineMaxAvailableMemoryBlock = []( void ) -> int
{
int nBytes = std::numeric_limits< int >::max();
while ( true )
{
try
{
std::vector< char >vec( nBytes );
break;
}
catch ( std::exception& ex )
{
nBytes = static_cast< int >( nBytes * 0.99 );
}
}
return nBytes;
};
auto determineMaxAvailableMemoryFragmented = []( int nBlockSize ) -> int
{
int nBytes = 0;
std::list< std::vector< char > > listBlocks;
while ( true )
{
try
{
listBlocks.push_back( std::vector< char >( nBlockSize ) );
nBytes += nBlockSize;
}
catch ( std::exception& ex )
{
break;
}
}
return nBytes;
};
std::cout << "Test with large memory blocks (16MB):\n";
for ( int k = 0; k < 5; k++ )
{
std::cout << "run #" << k << " max mem block = " << determineMaxAvailableMemoryBlock() / 1024.0 / 1024.0 << "MB\n";
std::cout << "run #" << k << " frag mem blocks of 16MB = " << determineMaxAvailableMemoryFragmented( 16*1024*1024 ) / 1024.0 / 1024.0 << "MB\n";
std::cout << "\n";
} // for_k
std::cout << "Test with small memory blocks (16k):\n";
for ( int k = 0; k < 5; k++ )
{
std::cout << "run #" << k << " max mem block = " << determineMaxAvailableMemoryBlock() / 1024.0 / 1024.0 << "MB\n";
std::cout << "run #" << k << " frag mem blocks of 16k = " << determineMaxAvailableMemoryFragmented( 16*1024 ) / 1024.0 / 1024.0 << "MB\n";
std::cout << "\n";
} // for_k
std::cin.get();
return 0;
}
带有大内存块的输出(这工作正常)
Test with large memory blocks (16MB):
run #0 max mem block = 1023.67MB OK
run #0 frag mem blocks of 16MB = 1952MB OK
run #1 max mem block = 1023.67MB OK
run #1 frag mem blocks of 16MB = 1952MB OK
run #2 max mem block = 1023.67MB OK
run #2 frag mem blocks of 16MB = 1952MB OK
run #3 max mem block = 1023.67MB OK
run #3 frag mem blocks of 16MB = 1952MB OK
run #4 max mem block = 1023.67MB OK
run #4 frag mem blocks of 16MB = 1952MB OK
带有小内存块的输出(从第二次运行开始内存分配很奇怪)
Test with small memory blocks (16k):
run #0 max mem block = 1023.67MB OK
run #0 frag mem blocks of 16k = 1991.06MB OK
run #1 max mem block = 0.493021MB ???
run #1 frag mem blocks of 16k = 1991.34MB OK
run #2 max mem block = 0.493021MB ???
run #2 frag mem blocks of 16k = 1991.33MB OK
run #3 max mem block = 0.493021MB ???
run #3 frag mem blocks of 16k = 1991.33MB OK
run #4 max mem block = 0.493021MB ???
run #4 frag mem blocks of 16k = 1991.33MB OK
更新:
这也适用于 new 和 delete[] 而不是 STL 的内部内存分配。
更新:
它适用于 64 位(我将两个函数允许分配的内存限制为 12GB)。真的很奇怪。这是该版本的 RAM 使用情况的图像:
更新: 它适用于 malloc 和 free,但不适用于 new 和 delete[](或如上所述的 STL)
最佳答案
正如我在上面的评论中提到的,这很可能是一个堆 fragmentation问题。堆将维护不同大小的 block 的列表,以满足不同的内存请求。较大的内存块被分解成较小的 block 用于较小的内存请求,以避免浪费 block 大小和请求大小之间的差异,从而减少了较大块的数量。因此,当请求更大的 block 时,堆可能没有足够的大块来满足请求。
碎片是堆实现的一个主要问题,因为它有效地减少了可用内存。但是,一些堆实现能够将较小的 block 合并回较大的 block ,并且即使在许多较小的请求之后也能够更好地满足大型请求。
我使用 glibc 的 malloc
(ptmalloc
) 运行了您上面的代码,稍作修改,得到以下结果...
Test with large memory blocks (16MB):
run #0 max mem block = 2048MB
run #0 frag mem blocks of 16MB = 2032MB
run #1 max mem block = 2048MB
run #1 frag mem blocks of 16MB = 2032MB
run #2 max mem block = 2048MB
run #2 frag mem blocks of 16MB = 2032MB
run #3 max mem block = 2048MB
run #3 frag mem blocks of 16MB = 2032MB
run #4 max mem block = 2048MB
run #4 frag mem blocks of 16MB = 2032MB
Test with small memory blocks (16k):
run #0 max mem block = 2048MB
run #0 frag mem blocks of 16k = 2047.98MB
run #1 max mem block = 2048MB
run #1 frag mem blocks of 16k = 2047.98MB
run #2 max mem block = 2048MB
run #2 frag mem blocks of 16k = 2047.98MB
run #3 max mem block = 2048MB
run #3 frag mem blocks of 16k = 2047.98MB
run #4 max mem block = 2048MB
run #4 frag mem blocks of 16k = 2047.98MB
因此,ptmalloc
至少似乎可以很好地处理这种特定情况下的碎片。
关于c++ - new 和 delete[] 比 malloc 和 free 差吗? (c++/VS2012),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32228952/