代码如下:
#include <iostream>
#include <vector>
#include <chrono>
#include <thread>
#include <cuda_runtime.h>
// CUDA错误检查宏
void checkCudaError(cudaError_t err, const char* msg) {
if (err != cudaSuccess) {
std::cerr << "CUDA Error: " << msg << " - " << cudaGetErrorString(err) << std::endl;
exit(EXIT_FAILURE);
}
}
int main() {
std::cout << "=== MetaX C500 VRAM Leak Simulation ===" << std::endl;
std::cout << "WARNING: This program will intentionally leak GPU memory!" << std::endl;
std::cout << "Press Ctrl+C to stop the test." << std::endl << std::endl;
// 获取设备信息
int deviceCount;
checkCudaError(cudaGetDeviceCount(&deviceCount), "cudaGetDeviceCount");
if (deviceCount == 0) {
std::cerr << "Error: No CUDA-compatible GPU found!" << std::endl;
return 1;
}
// 选择GPU设备(可根据需要修改设备ID)
int deviceId = 0;
checkCudaError(cudaSetDevice(deviceId), "cudaSetDevice");
cudaDeviceProp prop;
checkCudaError(cudaGetDeviceProperties(&prop, deviceId), "cudaGetDeviceProperties");
std::cout << "Target Device: " << prop.name << std::endl;
std::cout << "Total VRAM: " << (prop.totalGlobalMem / 1024 / 1024) << " MB" << std::endl;
std::cout << std::endl;
// 配置参数
const size_t ALLOCATION_SIZE = 50 * 1024 * 1024; // 每次分配50MB
const int ALLOCATIONS_PER_BATCH = 10; // 每批分配次数
const int BATCH_DELAY_MS = 500; // 批次间延迟(毫秒)
std::vector<void*> allocations;
size_t totalAllocated = 0;
int allocationCount = 0;
try {
while (true) {
// 批量分配显存
for (int i = 0; i < ALLOCATIONS_PER_BATCH; ++i) {
void* ptr = nullptr;
cudaError_t err = cudaMalloc(&ptr, ALLOCATION_SIZE);
if (err != cudaSuccess) {
std::cerr << "\n[ERROR] Allocation failed!" << std::endl;
std::cerr << " Allocation number: " << allocationCount << std::endl;
std::cerr << " Total allocated: " << (totalAllocated / 1024 / 1024) << " MB" << std::endl;
std::cerr << " CUDA Error: " << cudaGetErrorString(err) << std::endl;
// 显示近似剩余显存
size_t free, total;
cudaMemGetInfo(&free, &total);
std::cerr << " Approximate free VRAM: " << (free / 1024 / 1024) << " MB" << std::endl;
throw std::runtime_error("Out of memory");
}
allocations.push_back(ptr);
totalAllocated += ALLOCATION_SIZE;
allocationCount++;
}
// 打印进度
std::cout << "[LEAK] Allocations: " << allocationCount
<< " | Total: " << (totalAllocated / 1024 / 1024) << " MB"
<< std::endl;
// 延迟一段时间,便于观察
std::this_thread::sleep_for(std::chrono::milliseconds(BATCH_DELAY_MS));
}
}
catch (const std::exception& e) {
std::cerr << "\nTest completed: " << e.what() << std::endl;
}
// 说明:程序故意不释放内存以模拟泄漏
// 问题:进程退出后GPU驱动会自动回收显存么?
std::cout << "\n=== Test Finished ===" << std::endl;
std::cout << "To clean up: Terminate this process (Ctrl+C) or let it crash." << std::endl;
std::cout << "Driver will automatically free leaked memory on process exit." << std::endl;
return 0;
}