Created
October 21, 2025 09:47
-
-
Save bukowa/8dec0e26f922df9d3e66b00759f6f1e2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sys | |
| def fill_memory_with_strings(): | |
| """ | |
| Fills approximately 2GB of memory with unique strings. | |
| """ | |
| target_bytes = 2 * 1024 * 1024 * 1024 # 2 GB | |
| strings = [] | |
| total_size = 0 | |
| counter = 0 | |
| print(f"Starting to allocate approximately {target_bytes / (1024**3)} GB of memory with strings...") | |
| # Create a base string to append in each iteration. | |
| # A larger chunk size will be faster but less granular. | |
| chunk = "X" * (1024 * 100) # 100 KB chunk | |
| while total_size < target_bytes: | |
| # Create a unique string to ensure new memory is allocated. | |
| new_string = f"String number {counter}: {chunk}" | |
| strings.append(new_string) | |
| # Get the size of the new string and add it to the total. | |
| string_size = sys.getsizeof(new_string) | |
| total_size += string_size | |
| counter += 1 | |
| # Print progress every 1000 iterations. | |
| if counter % 1000 == 0: | |
| print(f"Allocated approximately {total_size / (1024**2):.2f} MB") | |
| print("\n----------------------------------------------------------") | |
| print(f"Successfully allocated approximately {total_size / (1024**3):.2f} GB of memory.") | |
| print(f"Total number of strings created: {len(strings)}") | |
| print("----------------------------------------------------------") | |
| # Keep the script running so the memory remains allocated for debugging. | |
| input("Press Enter to exit and release the memory...") | |
| if __name__ == "__main__": | |
| fill_memory_with_strings() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment