Memory won't release when loading images in Python

I’m fairly new to the SimpleITK API and I’m using it right now to load a CT and PET image, and resample the PET to the same coordinate system and size as the CT (for a deep learning algorithm).

Right now my code is:

reader = sitk.ImageSeriesReader()
# loading CT
dicom_names = reader.GetGDCMSeriesFileNames(pathToCTDicomFiles)
reader.SetFileNames(dicom_names)
imageCT = reader.Execute()
# Loading PET
dicom_names = reader.GetGDCMSeriesFileNames(pathToPETDicomFiles)
reader.SetFileNames(dicom_names)
imagePET = reader.Execute()
# Resampling the PET image
pet_resampled = sitk.GetArrayFromImage(sitk.Resample(imagePET, imageCT))

The problem is that each time I loop over this part for a new image, the memory usage increases until the program crashes after 2 or 3 iterations. I run the code in Spyder 4.1 with python 3.7 on Ubuntu 18.04 with 32 GB of ram.

I have tried using del and gc.collect(), but it does nothing in this case.

Any suggestions for better use of the API are also very welcome, e.g. only load the the metadata for the CT or something like this.

Hello @malteekj,

Welcome to SimpleITK!

Generally speaking your code looks fine and you should not be running out of memory. As you separated the looping from the example, I ran the following and did not experience the behavior you describe (increase in memory as number of iterations increases):

reader = sitk.ImageSeriesReader()

for i in range(10):
    dicom_names = reader.GetGDCMSeriesFileNames(pathToCTDicomFiles)
    reader.SetFileNames(dicom_names)
    imageCT = reader.Execute()

    dicom_names = reader.GetGDCMSeriesFileNames(pathToPETDicomFiles)
    reader.SetFileNames(dicom_names)
    imagePET = reader.Execute()

    pet_resampled = sitk.GetArrayFromImage(sitk.Resample(imagePET, imageCT))

Are you writing the pet_resampled numpy array to disk after each iteration or are you adding them into a list or some other data structure that is constantly growing? I suspect the issue is there and not in the resampling code itself.

One other observation, you are assuming that the PET and CT files are each in their own directory and that there is a single image series in each of these directories, otherwise you would have had to select a specific series from the directory (right now you are using the first series found in each directory as you did not specify a series ID).

1 Like

Hi,
Thanks for the answer!

I just overwrite all the variables each iteration, so nothing should be stored. Can I provide anything that might help indicate where the problem is? I don’t run into these problems of releasing memory when using e.g. pydicom.

Hello @malteekj,

Did you experience the memory issue running the code I provided? If not, then please provide a more complete version of your code so that we can identify why that code exhibits memory issues and the code above doesn’t. Once we identify the difference that will likely be the culprit.