Hi,
I have a little piece of code that causes a huge mem use which isn't freed later:
rayon::scope(|scope| {
for (tree1, record1) in records {
for (tree2, record2) in records {
for fun in TWO_ARG_FUNCTIONS.iter() {
let thread_outcomes = outcomes.clone();
scope.spawn(move |_| {
let guesses = (fun.fun)(record1.clone(), record2);
if guesses.iter().find(|&v| !v.is_finite()).is_none() {
let predictions = sort_guesses(guesses, thread_outcomes);
}
});
}
}
}
});
Without call to sort_guesses there is no high mem use. However the same code without rayon:
// rayon::scope(|scope| {
for (tree1, record1) in records {
for (tree2, record2) in records {
for fun in TWO_ARG_FUNCTIONS.iter() {
let thread_outcomes = outcomes.clone();
// scope.spawn(move |_| {
let guesses = (fun.fun)(record1.clone(), record2);
if guesses.iter().find(|&v| !v.is_finite()).is_none() {
let predictions = sort_guesses(guesses, thread_outcomes);
}
// });
}
}
}
// });
runs fine, i.e. no high mem use. There is no unsafe in my code.
The function sort_guesses is rather simple:
pub fn sort_guesses(guesses: Vec<f64>, outcomes: Vec<&Prediction>) -> Vec<(f64, &Prediction)> {
let mut predictions = guesses.into_iter().zip(outcomes).collect::<Vec<(f64, &Prediction)>>();
predictions.sort_unstable_by(|(first, _), (second, _)| first.partial_cmp(second).unwrap());
predictions
}
What is extra surprising is that even when I add drop(predictions) right after sort_guesses call, memory used is still increasing with each invocation (this code is run in a loop).
Valgrind shows about 120kB still reachable and 2kB possibly lost at the end of my program. However mem use goes by about 2GB with every loop.
Does anyone have any suggestion what to try next?
Thanks
edit: formatting