Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce memory usage by scanner #649

Open
wants to merge 1 commit into
base: release
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()