Skip to content

Commit

Permalink
Release memory after Scanner.get_results
Browse files Browse the repository at this point in the history
  • Loading branch information
yaroslav-dudar committed Apr 30, 2024
1 parent 761892b commit 681060d
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions sslyze/scanner/scanner.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import ctypes
import gc
import queue
from traceback import TracebackException
from typing import List, Optional, Generator, Sequence
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -140,3 +155,5 @@ def server_connectivity_test_error_callback(

for observer in self._observers:
observer.all_server_scans_completed()

self._free_memory()

0 comments on commit 681060d

Please sign in to comment.