cornerstonejs / cornerstoneWADOImageLoader

[DEPRECATED] DICOM WADO Image Loader for the cornerstone library
MIT License
280 stars 267 forks source link

Memory leaks when loading and displaying dicoms #462

Open StevenChalle opened 1 year ago

StevenChalle commented 1 year ago

Hello everyone. Thank you for your effort and time in this awesome project.

Context

I am developping a web app that allows doctors to review medical images (dicoms) by importing them manually. I am having huge memory leaks each time a dicom is loaded and displayed, because the file is fully loaded into the memory heap, and is never freed. Dicom can be as big as 400mb, so after reviewing about 5 dicoms, the tab crashes if not the browser itself. I guess i don't discard the viewer correctly, maybe i am missing some important steps when destroying the cornerstone element. Would really like your help & tips.

Technos & Code

const imageId: string = cornerstoneWadoImageLoader.wadouri.fileManager.add(dicom.file)

void cornerstone.loadImage(imageId).then((image) => {
          const viewport = cornerstone.getDefaultViewportForImage(viewerElement, image)
          cornerstone.displayImage(viewerElement, image, viewport)
})

Observations

Reproducing

The library example for loading manually imported dicom files has this issue, you can simply use it to reproduce : example from the github repo Keep in mind, you need to import different dicoms one after the other and see the memory heap snapshot in the dev console. The biggers the dicoms, the quicker it goes. i can supply dicoms if you need.

Issues potentially related

Memory problems and long load times for multiframe ultrasound data #443 Memory Leak on loadImage() #156 Memory Leak on every image load #519


Thank you for your attention. I will provide any investigation or ressources needed.

crysta-l commented 1 year ago

So how did you end up solving the problem,I had the same problem。

StevenChalle commented 1 year ago

Ehh, I didn't. Turns out it might not be a library issue. For my app i avoided using cornerstone and rewrote what i needed. I just use dicom's pixelData field and put it on a html canvas. The memory leak is reduced, probably because cornerstone uses more memory then i need, but the problem is quite the same, i can't load an infinite number of dicom on the canvas otherwise it ends up crashing.

I can't really explain it, but one workaround would be to force reloading the page at a certain point, so that it gets properly freed. I can easily display huge 3D dicoms (1g) so there is no big limitations, its only when loading multiple dicoms on the page without refreshing that it can lead to a crash.

Hope it helps, can get in touch if you need.