binaryninja-api: Possible memory leak in Binary Ninja python API unrelated to "active objects"
Version and Platform (required):
- Binary Ninja Version: 4.2.6455 (02c8da1e)
- OS: Windows
- OS Version: 11
- CPU Architecture: x64
Bug Description: It appears that type object accumulate even after the BinaryView object is closed, resulting in a memory leak. After opening and closing 150 binaries, it appears that the amount of type objects grow linearly.
Steps To Reproduce: Run the following script with an argument of a folder the contains at least 150 binaries:
import os
import psutil
import matplotlib.pyplot as plt
from binaryninja import load, BinaryView, set_worker_thread_count, get_memory_usage_info
from pathlib import Path
BINARY_NINJA_LOAD_OPTIONS = {
"analysis.limits.cacheSize": 64 * 8,
"analysis.limits.maxFunctionAnalysisTime": 20_000 * 3,
"analysis.limits.maxFunctionSize": 0,
"analysis.limits.maxFunctionUpdateCount": 100 * 10,
"analysis.mode": "full",
"rendering.strings.maxAnnotationLength": 256,
}
def get_memory_usage():
process = psutil.Process(os.getpid())
mem = process.memory_info().rss / (1024 * 1024) # Convert to MB
return mem
def load_binary(
path: str | os.PathLike,
) -> BinaryView:
path = Path(path)
set_worker_thread_count(8)
bv = load(path, options=BINARY_NINJA_LOAD_OPTIONS)
return bv
def plot_list_to_file(filename: str, data: list, title: str, xlabel: str, ylabel: str) -> None:
plt.figure(figsize=(10, 5))
plt.plot(data, marker='o')
plt.title(title)
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.grid()
plt.savefig(filename)
plt.close()
def test_binja(folder: Path):
limit = 150
i = 0
mem_usage = []
basic_blocks = []
bv_objects = []
function_objects = []
llil_objects = []
mlil_objects = []
type_objects = []
for file in folder.iterdir():
if i >= limit:
break
i += 1
with load_binary(file) as bv:
print(f"Loaded {file.name} with {bv.arch.name} architecture and {bv.platform.name} platform")
print(f"[{i}/{limit}] Memory usage after loading {file.name}: {get_memory_usage()} MB")
mem_usage_info = get_memory_usage_info()
print(f"[{i}/{limit}] Memory usage info: {mem_usage_info}")
mem_usage.append(get_memory_usage())
basic_blocks.append(mem_usage_info["Active BasicBlock objects"])
bv_objects.append(mem_usage_info["Active BinaryView objects"])
function_objects.append(mem_usage_info["Active Function objects"])
llil_objects.append(mem_usage_info["Active LowLevelILFunction objects"])
mlil_objects.append(mem_usage_info["Active MediumLevelILFunction objects"])
type_objects.append(mem_usage_info["Active Type objects"])
plot_list_to_file(
"memory_usage.png",
mem_usage,
"Memory Usage",
"Binary Number",
"Memory Usage (MB)",
)
plot_list_to_file(
"basic_blocks.png",
basic_blocks,
"Basic Blocks",
"Binary Number",
"Basic Blocks",
)
plot_list_to_file(
"bv_objects.png",
bv_objects,
"BinaryView Objects",
"Binary Number",
"BinaryView Objects",
)
plot_list_to_file(
"function_objects.png",
function_objects,
"Function Objects",
"Binary Number",
"Function Objects",
)
plot_list_to_file(
"llil_objects.png",
llil_objects,
"LowLevelILFunction Objects",
"Binary Number",
"LowLevelILFunction Objects",
)
plot_list_to_file(
"mlil_objects.png",
mlil_objects,
"MediumLevelILFunction Objects",
"Binary Number",
"MediumLevelILFunction Objects",
)
plot_list_to_file(
"type_objects.png",
type_objects,
"Type Objects",
"Binary Number",
"Type Objects",
)
def main():
import argparse
parser = argparse.ArgumentParser(description="Test Binary Ninja loading")
parser.add_argument("folder", type=Path, help="Path to the folder containing binaries")
args = parser.parse_args()
test_binja(args.folder)
if __name__ == "__main__":
main()
This should produce several plots with type_objects.png being the interesting one that shows the issue.
Expected Behavior: It’s expected that the type objects would be released when closing the BinaryView, but instead they are accumulated.
Screenshots/Video Recording:
Binary:
Any binary collection would do. I used a binaries produced by BinKit project.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 18 (8 by maintainers)
I’ve verified that we have a type leak when DebugInfo is being applied. Same file loaded and closed 3 times first two times with debuginfo disabled third time with it enabled:
@empezarcero We’ve fixed the Type leak on dev please let us know if you run into any other type leaks. I’ve run some additional tests to determine if there are more leaks and it does look like we’re leaking some additional memory some where but I haven’t been able to tell it is just heap fragmentation or not. It kind seems like the memory usage is going asymptotic.
Please re-run your tests and verify our fix!
@plafosse Here’s the binary:
hello-2.12.1_clang-10.0_arm_32_O0_hello.zip
@plafosse tried with

5.0.7194-dev (fe6fcbf9)Results:Looks like the leak is still there.
You can go to your Update Settings and switch to the Development channel.