- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以,我有一些问题,我怀疑内存泄漏,为了测试我写了这段小代码。通过注释以下行:
printf("Calc index: %d\n", ArrLength);
代码运行良好。但是当我取消注释时,程序在几千个线程后崩溃了。当我使用 try/catch 时,程序只是在 try 函数内崩溃。谁能帮帮我?
#include "stdafx.h"
#include <process.h>
#include <iostream>
#include <mutex>
#include <windows.h>
using namespace std;
typedef struct {
int StartNode;
int EndNode;
int GangID;
int MemberID;
int ArrLength;
int arr[10000];
}t;
t *arg;
mutex m;
void myFunc(void *param) {
m.lock();
printf("Calculate thread started\n");
t *args = (t*)param;
int StartNode = args->StartNode;
int EndNode = args->EndNode;
int GangID = args->GangID;
int MemberID = args->MemberID;
int ArrLength = args->ArrLength;
printf("Calc index: %d\n", ArrLength);
free(args);
m.unlock();
}
int main()
{
for (int i = 0; i < 1000000; i++)
{
HANDLE handle;
arg = (t *)malloc(sizeof(t));
arg->StartNode = 2;
arg->EndNode = 1;
arg->GangID = 1;
arg->MemberID = 1;
arg->ArrLength = 5;
for (int j = 0; j < 10000; j++)
{
arg->arr[j] = j;
}
handle = (HANDLE)_beginthread(myFunc, 0, (void*)arg);
}
cin.get();
return 0;
}
最佳答案
好吧,让我们做一些计算。您的 t
结构每个实例有 40020 个字节。您确实分配了 1M 次,导致总共分配了大约 40 Gb。而且这还不是所需的全部内存,因为每个线程都不是免费的。默认情况下,Windows 为每个线程分配 1Mb 堆栈,这为您提供了 1 Tb(1 TB)的内存,仅供线程运行。
因此,总内存量约为 1040 Gb。你真的打算那样吗?
关于c++ - _beginthread 疑似内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49561787/
我正在创建一个用于跟踪网络上机器配置的应用程序。 “配置”广泛定义了系统上安装的产品/服务/操作系统/应用程序。其中任何一个的变化都会导致配置发生变化。信息是使用扫描仪获取的。数据已经解析。我需要找出
好的,所以,我已经使用自动加载器一段时间了,变得相当习惯于 MVC,并决定尝试处理命名空间。嗯,这没有按预期工作。我设法以一种非常简单的方式重现了该错误,因此我的 php 文件的内容如下:
我正在尝试确定我构建的 JMS 队列中发生可疑内存/资源泄漏的位置。我是 JMS 队列的新手,因此我使用了许多标准 JMS 类对象来确保稳定性。但是在我的代码或配置中的某个地方我做错了一些事情,并且我
我正在编写一个小型单 Activity android 应用程序并遇到内存泄漏错误,因为我无法找到起源。首先,该应用程序计算基本内容并以结构化方式显示结果。计算很简单,虽然有一些图像,但它们大约有 5
我是一名优秀的程序员,十分优秀!