阅读Martin Sustrick's blog关于在C ++中对防止“未定义行为”的挑战,特别是由于内存耗尽导致malloc()失败的问题,我被提醒了很多次,我有在这种情况下知道该怎么做是很沮丧的。
对于虚拟系统来说,这种情况很少见,但是在嵌入式平台上,或者在击中虚拟系统时性能下降等同于失败的情况下,正如Martin对ZeroMQ的情况一样,我决定找到一个可行的解决方案,并且做到了。
我想向StackOverflow的读者询问他们是否尝试过这种方法,以及他们对此的体验。
解决方案是在程序开始时调用malloc()从堆中分配一块备用内存,然后使用该备用内存池来避免内存耗尽。我的想法是防止投降有利于有序撤退(我昨晚阅读了Kesselring's defense of Italy的帐户),其中错误消息和IP套接字等工作时间足够长(希望)至少告诉用户发生了什么。
#define SPARE_MEM_SIZE (1<<20) // reserve a megabyte
static void *gSpareMem;
// ------------------------------------------------------------------------------------------------
void *tenacious_malloc(int requested_allocation_size) {
static int remaining_spare_size = 0; // SPARE_MEM_SIZE;
char err_msg[512];
void *rtn = NULL;
// attempt to re-establish the full size of spare memory, if it needs it
if (SPARE_MEM_SIZE != remaining_spare_size) {
if(NULL != (gSpareMem = realloc(gSpareMem, SPARE_MEM_SIZE))) {
remaining_spare_size = SPARE_MEM_SIZE;
// "touch" the memory so O/S will allocate physical memory
meset(gSpareMem, 0, SPARE_MEM_SIZE);
printf("\nSize of spare memory pool restored successfully in %s:%s at line %i :)\n",
__FILE__, __FUNCTION__, __LINE__);
} else {
printf("\nUnable to restore size of spare memory buffer.\n");
}
}
// attempt a plain, old vanilla malloc() and test for failure
if(NULL != (rtn = malloc(requested_allocation_size))) {
return rtn;
} else {
sprintf(err_msg, "\nInitial call to malloc() failed in %s:%s at line %i",
__FILE__, __FUNCTION__, __LINE__);
if(remaining_spare_size < requested_allocation_size) {
// not enough spare storage to satisfy the request, so no point in trying
printf("%s\nRequested allocaton larger than remaining pool. :(\n\t --- ABORTING --- \n", err_msg);
return NULL;
} else {
// take the needed storage from spare memory
printf("%s\nRetrying memory allocation....\n", err_msg);
remaining_spare_size -= requested_allocation_size;
if(NULL != (gSpareMem = realloc(gSpareMem, remaining_spare_size))) {
// return malloc(requested_allocation_size);
if(NULL != (rtn = malloc(requested_allocation_size))) {
printf("Allocation from spare pool succeeded in %s:%s at line %i :)\n",
__FILE__, __FUNCTION__, __LINE__);
return rtn;
} else {
remaining_spare_size += requested_allocation_size;
sprintf(err_msg, "\nRetry of malloc() after realloc() of spare memory pool "
"failed in %s:%s at line %i :(\n", __FILE__, __FUNCTION__, __LINE__);
return NULL;
}
} else {
printf("\nRetry failed.\nUnable to allocate requested memory from spare pool. :(\n");
return NULL;
}
}
}
}
// ------------------------------------------------------------------------------------------------
int _tmain(int argc, _TCHAR* argv[]) {
int *IntVec = NULL;
double *DblVec = NULL;
char *pString = NULL;
char String[] = "Every good boy does fine!";
IntVec = (int *) tenacious_malloc(100 * sizeof(int));
DblVec = (double *) tenacious_malloc(100 * sizeof(double));
pString = (char *)tenacious_malloc(100 * sizeof(String));
strcpy(pString, String);
printf("\n%s", pString);
printf("\nHit Enter to end program.");
getchar();
return 0;
}
答案 0 :(得分:1)
最好的策略是瞄准无需分配的代码。特别是,对于正确,健壮的程序,所有故障路径必须是无故障情况,这意味着您不能在故障路径中使用分配。
我的偏好,只要有可能,就是在操作开始后避免任何分配,而是确定所需的存储并在操作开始之前全部分配。这可以大大简化程序逻辑并使测试更容易(因为您必须测试可能的单点故障)。当然,在其他方面它也可能更贵;例如,您可能必须对输入数据进行两次传递,以确定您需要多少存储空间,然后使用存储进行处理。
关于在malloc
失败后预分配一些紧急存储的解决方案,基本上有两个版本:
free
,然后希望malloc
再次发挥作用。第一种方法的优点是,即使标准库和第三方库代码也可以利用紧急空间,但它的缺点是,释放的存储可能被其他进程或您自己进程中的线程窃取,争夺它。如果您确定内存耗尽将来自耗尽虚拟地址空间(或进程资源限制)而不是系统资源,并且您的进程是单线程的,那么您不必担心竞争,并且您可以相当安全假设这种方法可行。但是,一般来说,第二种方法更安全,因为您有绝对的保证,可以获得所需的紧急存储量。
我真的不喜欢这些方法中的任何一种,但它们可能是你能做到的最好的方法。
答案 1 :(得分:0)
在现代的64位计算机上,你可以使用大量内存,而不是RAM。实际上,malloc并没有失败。在实践中发生的事情是你的应用程序开始颠簸,一旦你说4GB的RAM并且你的分配超过了这个,你的性能将降到零,因为你像疯了一样交换。你的性能下降太多,以至于你永远无法达到malloc无法返回内存的程度。