diff --git a/sslyze/scanner/scanner.py b/sslyze/scanner/scanner.py index 125b563d..e73bfb2b 100644 --- a/sslyze/scanner/scanner.py +++ b/sslyze/scanner/scanner.py @@ -1,3 +1,5 @@ +import ctypes +import gc import queue from traceback import TracebackException from typing import List, Optional, Generator, Sequence @@ -64,6 +66,19 @@ def queue_scans(self, server_scan_requests: List[ServerScanRequest]) -> None: def _has_started_work(self) -> bool: return self._connectivity_tester.has_started_work + def _free_memory(self): + """ + Manually trigger a garbage collection and release memory from the heap to OS kernel + """ + try: + # Forcing garbage collection to resolve circular references + gc.collect() + # Call glibc's malloc_trim() to make an attempt to release memory back to OS + ctypes.CDLL(None).malloc_trim(0) + except Exception: + # Could not release memory + pass + def get_results(self) -> Generator[ServerScanResult, None, None]: if not self._has_started_work: raise ValueError("No scan requests have been submitted") @@ -140,3 +155,5 @@ def server_connectivity_test_error_callback( for observer in self._observers: observer.all_server_scans_completed() + + self._free_memory()