Created
March 25, 2024 15:20
-
-
Save a10y/a48eaacece0ba1f440930326c6298929 to your computer and use it in GitHub Desktop.
Exploring memory leaks with Rust
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
use std::hint::black_box; | |
fn main() { | |
// Leak the reference in a loop. | |
println!("begin leaking memory"); | |
for _ in 0..10 { | |
let data = Box::new("hello world".to_string()); | |
let _ = black_box(Box::leak(data)); // Needed to avoid allocs being optimized away by the compiler. | |
} | |
println!("done leaking memory"); | |
} |
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
use std::hint::black_box; | |
fn main() { | |
// Leak the reference in a loop. | |
println!("begin leaking memory"); | |
for _ in 0..10 { | |
let data = Box::new("hello world".to_string()); | |
let _ = black_box(data); // Needed to avoid allocs being optimized away by the compiler. | |
} | |
println!("done leaking memory"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output of the leaky.rs version with 10 explicit leaks snuck in there: