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