VTK使用越来越多的内存,变得很慢

vtk uses more and more memory and becomes slow.

本文关键字:内存 越来越多 VTK      更新时间:2023-10-16

我在应用程序中遇到了内存泄漏,并确定它是和vtk有关的东西。我在Windows 7和Visual Studio 2012上使用64bit的vtk 6.1.0。

我在使用更多的vtk算法时遇到了这种行为。当然不是操作系统占用内存,而是vtk。似乎在每次调用update vtk时,都会创建对象并将它们放入某种更新队列中,并且最终不会删除。考虑以下代码:

#include <vtkImageConstantPad.h>
#include <vtkImageData.h>
#include <ctime>
int main(int argc, char *argv[])
{
    int c=0;
    clock_t start;
    size_t iterations = 1000;
    while (c != '.'){
         c = getchar();
         start = clock();
         for(size_t i=0; i<iterations; ++i){
             vtkImageConstantPad* pad = vtkImageConstantPad::New();
             vtkImageData* test = vtkImageData::New();
             test->SetExtent(0,1,0,1,0,1);
             test->AllocateScalars(VTK_FLOAT,1);
             pad->SetInputData(test);
             pad->SetOutputWholeExtent(0,2,0,2,0,2);
             pad->Update(); //this is what makes it bad
             pad->Delete();
             test->Delete();
         }
         std::cout << "Time for " << iterations << ": " << static_cast<double>(clock() - start) / CLOCKS_PER_SEC << "sec" << std::endl;
    }
    return 0;
}

我的输出是:

Time for 1000: 0.816sec
Time for 1000: 1.879sec
Time for 1000: 3.454sec
etc.

所以每次调用update()都会消耗更多的内存和时间,尽管for循环作用域中的所有对象都会被销毁。有谁能在其他平台上证实这一点,或者知道如何解决这个问题?

经过进一步调查,我发现并不是所有的机器都出现这个问题。最后,我发现我的Bitdefender杀毒软件在某种程度上扰乱了程序,导致了这种奇怪的行为。当我关闭它时,一切都像预期的那样工作。