From d0cc6f115b82d66cbd2b88c7a9ba3d8ef264af2b Mon Sep 17 00:00:00 2001 From: Maxime Beauchemin Date: Tue, 16 Sep 2025 09:23:39 -0700 Subject: [PATCH] feat: add optional garbage collection after requests (#35061) Co-authored-by: Claude --- superset/config.py | 2 ++ superset/initialization/__init__.py | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/superset/config.py b/superset/config.py index 856b60401af..662e79576e1 100644 --- a/superset/config.py +++ b/superset/config.py @@ -626,6 +626,8 @@ DEFAULT_FEATURE_FLAGS: dict[str, bool] = { "DATE_RANGE_TIMESHIFTS_ENABLED": False, # Enable Matrixify feature for matrix-style chart layouts "MATRIXIFY": False, + # Force garbage collection after every request + "FORCE_GARBAGE_COLLECTION_AFTER_EVERY_REQUEST": False, } # ------------------------------ diff --git a/superset/initialization/__init__.py b/superset/initialization/__init__.py index 82296e0e37f..133456f35a6 100644 --- a/superset/initialization/__init__.py +++ b/superset/initialization/__init__.py @@ -638,6 +638,17 @@ class SupersetAppInitializer: # pylint: disable=too-many-public-methods response.headers[k] = v return response + @self.superset_app.after_request + def cleanup_analytics_memory(response: Response) -> Response: + """Force garbage collection after each request if feature flag enabled""" + if feature_flag_manager.is_feature_enabled( + "FORCE_GARBAGE_COLLECTION_AFTER_EVERY_REQUEST" + ): + import gc + + gc.collect() + return response + @self.superset_app.context_processor def get_common_bootstrap_data() -> dict[str, Any]: # Import here to avoid circular imports