Skip to content

Memory leaks when building models from custom templates  #212

@salaza82

Description

@salaza82

Spectral model classes inheriting the FunctionMeta class AND loading data leak memory. Memory leaks immediately on calling the constructor and when class object goes out of scope.

Class defintion does the following

from astromodels.functions.function import Function1D, FunctionMeta
import numpy as np

class CustomSpec(Function1D, metaclass=FunctionMeta):

    def _setup(self):
        # Does nothing

    def _load_spec_from_params(self):
        self._data = np.load(<path_to_data_file>, allow_pickle=True).item()

    def set_params(self, params):
        self_attributes = params

        self._load_spec_from_params()

The following will leak slowly

import psutil as ps
from threeML import *
# Custom Spectral class library
import CustomSpec

p = ps.Process()

for i in range(500):
    spectrum = CustomSpec() # instantiate spectral model

    if i%50 == 0: # Print memory use every 50 iters
        print(f'Real Usage: {p.memory_info().rss * 1e-6:4.1f} MB')

And the following leaks substantially.

spectrum = CustomSpec()

for i in range(500):

    # Define Spectral model Parameters
    spectrum.set_params("your params here")

    # Instantiate spatial template
    myDwarf = PointSource('name', 0.0, 30.0, spectral_shape=spectrum)
    model = Model(myDwarf)

    if i%50 == 0 or i == 499: # Print memory use every 50 steps
        print(f'Real Usage: {p.memory_info().rss * 1e-6:4.1f} MB')

In the second block, the jump in memory use is exactly the size of the data table loaded in the custom spectral class.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions