cornerstonejs / cornerstone3D

Cornerstone is a set of JavaScript libraries that can be used to build web-based medical imaging applications. It provides a framework to build radiology applications such as the OHIF Viewer.
https://cornerstonejs.org
MIT License
575 stars 296 forks source link

[Bug] Memory leak with MPR loading #846

Open HemantPatel30 opened 1 year ago

HemantPatel30 commented 1 year ago

Describe the Bug

I was working on memory optimization for MPR loading of the study & with current example code base made below changes to reduce memory foot print on MPR,

Changes,

  1. useNorm16Texture set to true
  2. Clear the worker thread after volume loading is finished, as discussed in OHIF community hours

TestData, Two different study being used to capture this observation

  1. study contain single series and having 649 images with each image size 512 * 512
  2. study contain single series and having 1203 images with each image size 512 * 512

OS, macos vertura chrome browser

Observation is that, Once volume is loaded and workers threads are terminated & app is stable --> at this point if we switch to new tab memory consumption in chrome task manager reduces by exactly size of the MPR volume.

in attached recording,

Example 1 : with study 1 (649 images) here is the break down of memory consumption in chrome,

https://github.com/cornerstonejs/cornerstone3D/assets/115255387/bc0d1db4-bf37-40bc-8f10-0c6f7c575980

Volume load in progress : 1020 MB After workers are killed : 815 - 804 MB When click on new tab : 462 MB ( Reduced by ~330 MB)


Example 2 : with study 2 (1203 images) here is the break down of memory consumption in chrome,

https://github.com/cornerstonejs/cornerstone3D/assets/115255387/37731f33-a7cd-4c17-a0e2-a0743254937b

Volume load in progress : 1.6 GB After workers are killed : 1.3 GB When click on new tab : 760 MB ( Reduced by ~550 MB)

Chrome hold/releasing memory when you switch to new tab but in that case it may be some fix memory, here it release the memory in proportion to volume size only. I have tried even smaller study contain 400 images & in this case it release the memory ~200 MB.

Steps to Reproduce

  1. yarn run example volumeAPI
  2. To clean the worker once volume load is finished, make change in BaseStreamingImageVolume.ts, on top,
    import { webWorkerManager } from '@cornerstonejs/dicom-image-loader'; in method, callLoadStatusCallback at end below code, if (webWorkerManager) { webWorkerManager.terminate(); }
  3. init.ts - useNorm16Texture set to true

The current behavior

it occupies lot more memory especially with MPR loading

The expected behavior

it should occupied memory as per the image size

OS

MAC OS 13.5.2

Node version

18.12.1

Browser

Chrome Version 118.0.5993.96

HemantPatel30 commented 1 year ago

@sedghi , Here is the comparison between firefox and chrome on active tab, for study contains 1203 image

Chrome memory : 1.3 GB Firefox memory : 842 MB Once new tab is opened, chrome memory reduces at reached near to 720 -750 MB , which is in-line with firefox.

https://github.com/cornerstonejs/cornerstone3D/assets/115255387/8edd6066-8c2a-4560-a964-aa8277bce7da

sedghi commented 1 year ago

Could you kindly provide the data if it has been anonymized and you can confirm that there is no patient health information present in any of the headers or embedded within the pixel data?

HemantPatel30 commented 1 year ago

@sedghi , I will try to get anonymized data but this happened with v3-demo server as well, below is recording for the same and study and series UID

const imageIds = await createImageIdsAndCacheMetaData({ StudyInstanceUID: '2.25.232704420736447710317909004159492840763', SeriesInstanceUID: '2.25.36253944465418474135239716841130807555', wadoRsRoot: 'https://d33do7qe4w26qo.cloudfront.net/dicomweb', });

https://github.com/cornerstonejs/cornerstone3D/assets/115255387/ae3730fa-890d-4004-9d8e-af8dd2e3ea7c