该函数将在字节对象列表中分配内存。列表中的每个项目实际上是独特的且长度相同。该函数还记录其分配情况。我已经测试过它可以分配高达3.7 TiB的内存。它使用
humanfriendly
包,但如果您不需要它,可以删除其使用。
它确实使用了循环,但至少它允许您自选每次分配多少内存。例如,您可以为
multiplier_per_allocation
使用8倍的值。
import logging
import secrets
from typing import Optional
from humanfriendly import format_size
log = logging.getLogger(__name__)
def fill_memory(*, num_unique_bytes_per_allocation: int = 1024, multiplier_per_allocation: int = 1024 ** 2, max_allocations: Optional[int] = None) -> None:
"""Allocate available memory into a list of effectively unique bytes objects.
This function is for diagnostic purposes.
:param num_unique_bytes_per_allocation: Each allocation is created by multiplying a random sequence of bytes of this length.
:param multiplier_per_allocation: Each allocation is created by multiplying the random sequence of bytes by this number.
:param max_allocations: Optional number of max allocations.
"""
num_allocation_bytes = num_unique_bytes_per_allocation * multiplier_per_allocation
log.info(
f"Allocating cumulative instances of {num_allocation_bytes:,} bytes ({format_size(num_allocation_bytes)}) each. "
f"Each allocation uses {num_unique_bytes_per_allocation:,} unique bytes ({format_size(num_unique_bytes_per_allocation)}) "
f"with a multiplier of {multiplier_per_allocation:,} ({format_size(multiplier_per_allocation)})."
)
allocated = []
num_allocation = 1
while True:
unique_bytes_for_allocation = secrets.token_bytes(num_unique_bytes_per_allocation)
allocated.append(unique_bytes_for_allocation * multiplier_per_allocation)
num_total_bytes_allocated = num_allocation * num_allocation_bytes
log.info(f"Used a total of {num_total_bytes_allocated:,} bytes ({format_size(num_total_bytes_allocated)}) via {num_allocation:,} allocations.")
if max_allocations and (max_allocations == num_allocation):
break
num_allocation += 1
示例输出:
>>> import logging
>>> logging.basicConfig(level=logging.INFO)
>>> fill_memory()
INFO:Allocating cumulative instances of 1,073,741,824 bytes (1 GiB) each. Each allocation uses 1,024 unique bytes (1 KiB) with a multiplier of 1,048,576 (1 MiB).
INFO:Used a total of 1,073,741,824 bytes (1 GiB) via 1 allocations.
INFO:Used a total of 2,147,483,648 bytes (2 GiB) via 2 allocations.
INFO:Used a total of 3,221,225,472 bytes (3 GiB) via 3 allocations.
INFO:Used a total of 4,294,967,296 bytes (4 GiB) via 4 allocations.