Skip to content

Instantly share code, notes, and snippets.

@T1T4N
Last active February 16, 2024 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save T1T4N/6ac1d1073e15353f23578e1a50e52908 to your computer and use it in GitHub Desktop.
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
// 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