Memory management issue with image upload for dash web app

Hi there,

I built a computer vision web app and deployed it on Heroku.
It processes picture uploaded by a user (through dcc.upload) and should return the processed picture and additional information computed by a PyTorch back-end.

After a few picture uploads, I bumped into memory issue and the app crashed. My understanding is that all pictures passed through dcc.upload are accumulated into memory (see here) and causes the crash.

Here an overview of the code:

from dash.dependencies import Input, Output
import dash_core_components as dcc
import dash_html_components as html

app = dash.Dash(__name__)

app.layout = html.Div([
    
    ...
    
        dcc.Upload(
        id='upload-image',
        children=html.Div(['Drag and Drop or ',
                           html.A('Select Files')]),
        ...
        
        ),
    ...
    
    ])

@app.callback(
    [Output('output-image-1', 'children'),
      Output('output-image-2', 'children'),
      Output('output-graph', 'children')],
    [Input('upload-image', 'contents')])
def update_output(content):
    
    if content is not None:
        
        # Deep Learning
        raw_img, grad_img, d_probs = process(content)
        return parse_image(str_raw_img), parse_image(str_grad_img), parse_graph(d_probs)
    
    else:
        return None, None, None

if __name__ == '__main__':
    app.run_server()

How could I improve memory management? Ideally, pictures could be deleted whenever a new one is uploaded. I understand I could use dcc.Store but I’m not sure how to proceed. Thanks for any guidance.