From ec4bbf35a159b5d019d6acd84aa5753b794bb7ee Mon Sep 17 00:00:00 2001 From: Benjamin Schubert Date: Mon, 11 Feb 2019 15:20:00 +0000 Subject: Cleanup internal Loader cache after loading elements --- buildstream/_loader/loader.py | 19 +++++++++++++++++++ buildstream/_project.py | 2 ++ buildstream/element.py | 15 +++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/buildstream/_loader/loader.py b/buildstream/_loader/loader.py index d0cfa8522..17d7c63fb 100644 --- a/buildstream/_loader/loader.py +++ b/buildstream/_loader/loader.py @@ -152,8 +152,27 @@ class Loader(): # ret.append(loader._collect_element(element)) + self._clean_caches() + return ret + # clean_caches() + # + # Clean internal loader caches, recursively + # + # When loading the elements, the loaders use caches in order to not load the + # same element twice. These are kept after loading and prevent garbage + # collection. Cleaning them explicitely is required. + # + def _clean_caches(self): + for loader in self._loaders.values(): + # value may be None with nested junctions without overrides + if loader is not None: + loader._clean_caches() + + self._meta_elements = {} + self._elements = {} + ########################################### # Private Methods # ########################################### diff --git a/buildstream/_project.py b/buildstream/_project.py index 028bdcc9f..92ac8cab5 100644 --- a/buildstream/_project.py +++ b/buildstream/_project.py @@ -358,6 +358,8 @@ class Project(): for meta in meta_elements ] + Element._clear_meta_elements_cache() + # Now warn about any redundant source references which may have # been discovered in the resolve() phase. redundant_refs = Element._get_redundant_source_refs() diff --git a/buildstream/element.py b/buildstream/element.py index a3bf59dc6..5ecc25ab2 100644 --- a/buildstream/element.py +++ b/buildstream/element.py @@ -966,6 +966,21 @@ class Element(Plugin): return element + # _clear_meta_elements_cache() + # + # Clear the internal meta elements cache. + # + # When loading elements from meta, we cache already instantiated elements + # in order to not have to load the same elements twice. + # This clears the cache. + # + # It should be called whenever we are done loading all elements in order + # to save memory. + # + @classmethod + def _clear_meta_elements_cache(cls): + cls.__instantiated_elements = {} + # _get_redundant_source_refs() # # Fetches a list of (Source, ref) tuples of all the Sources -- cgit v1.2.1