Last active
February 16, 2024 10:13
-
-
Save T1T4N/6ac1d1073e15353f23578e1a50e52908 to your computer and use it in GitHub Desktop.
A snippet to leak as much of the available virtual memory in Swift as possible
This file contains 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
// Modified from: https://stackoverflow.com/a/45882589 | |
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(3)) { | |
var freeMem = Int.max | |
let reserved = 605696 | |
let chunks = 12000 | |
repeat { | |
freeMem = os_proc_available_memory() | |
let toAlloc = freeMem | |
let mallocSize = toAlloc / chunks | |
var p: [UnsafeMutableRawPointer] = [] | |
var allocatedMB = 0 | |
let buf = malloc(mallocSize) | |
assert(buf != nil) | |
p.append(buf!) | |
memset(p[allocatedMB], 0, mallocSize) | |
allocatedMB += 1 | |
} while (freeMem > reserved) | |
let remainingMemory = os_proc_available_memory() | |
print("Available memory after alloc: \(remainingMemory)") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment