cornerstone3D icon indicating copy to clipboard operation
cornerstone3D copied to clipboard

can't identify what's causing memory leak

Open IbrahimCSAE opened this issue 3 years ago • 4 comments
trafficstars

Hello,

I'm using cornerstone3D for a project I'm working on, and I have a viewer component that uses it, I'm trying to clear memory when I unmount the component, here's the unmount function I use:


  unmounted() {
    // delete tool group
    cornerstone3DTools.ToolGroupManager.destroyToolGroup(this.toolGroupId);
    // remove Tools
    cornerstone3DTools.removeTool(cornerstone3DTools.WindowLevelTool);
    cornerstone3DTools.removeTool(cornerstone3DTools.PanTool);
    cornerstone3DTools.removeTool(cornerstone3DTools.ZoomTool);
    cornerstone3DTools.removeTool(cornerstone3DTools.StackScrollMouseWheelTool);
    cornerstone3DTools.removeTool(cornerstone3DTools.CrosshairsTool);
    // purge cache
    cornerstone3D.cache.purgeCache();
    // destory render engine
    this.renderingEngine.destroy();
    // register regular cornerstone to the image loader again
    cornerstoneWADOImageLoader.external.cornerstone = cornerstone;
  },

However, even with this function running when the component is unmounted, there's massive memory leak. Is there something I'm missing? am I not clearing memory the right way? if there's a function I should call that I'm not, please point that out to me as I can't figure out what to do.

I'm using old cornerstone in other components, and the cornerstone.imageCache.purgeCache(); does the job there, I don't know why it's not doing it's job in cornerstone3D

Screenshot_5 Screenshot_6 Screenshot_7

IbrahimCSAE avatar Aug 25 '22 11:08 IbrahimCSAE

There is a destory exported for CornerstoneTools3D, use that

sedghi avatar Aug 25 '22 13:08 sedghi

There is a destory exported for CornerstoneTools3D, use that

It still doesn't fix the huge leaks happening, I think it's from Cornerstone3D itself, not the tools.

The leaks are usually huge like 400MB+ , and If I keep mounting and unmounting the component the leaks keep getting bigger and bigger until it crashes.

IbrahimCSAE avatar Aug 25 '22 17:08 IbrahimCSAE

    createVolume: async function (series) {
      let volumeName = series.seriesInstanceUID;
      let volumeLoaderScheme = "cornerstoneStreamingImageVolume";
      let volumeId = `${volumeLoaderScheme}:${volumeName}`;
      this.volumeId = volumeId;
      const ctVolume = await cornerstone3D.volumeLoader.createAndCacheVolume(
        volumeId,
        { imageIds: series.imageIds }
      );
      return // i returned here and the memory problem doesn't happen anymore
      // load the volume
      ctVolume.load();
    },

it seems to me that ctVolume.load() is what causes the problem, whatever resources that allocates, don't seem to get cleaned up.

Is there something like ctVolume.unload() that I can use? if not then how do I go about clearing this manually?

IbrahimCSAE avatar Aug 25 '22 17:08 IbrahimCSAE

Yes it seems like there is a leak. the load just start the loading, createAndCacheVolume does not load the images in yet

sedghi avatar Aug 31 '22 14:08 sedghi

This might be related

https://github.com/OHIF/Viewers/issues/3207

sedghi avatar Oct 13 '23 20:10 sedghi