Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to forget memory with the jemalloc strategy. #121

Open
fishiu opened this issue Oct 13, 2023 · 2 comments
Open

How to forget memory with the jemalloc strategy. #121

fishiu opened this issue Oct 13, 2023 · 2 comments

Comments

@fishiu
Copy link

fishiu commented Oct 13, 2023

Hello, I was using PyO3 to bind this rust code to python and is now struggling with the memory ownership problem (more specifically, double free).

When I try to pass an lmr to python, I do std::mem::forget(lmr) to make rust forget about the memory, as the memory is already somehow transfered to python by some unsafe pointer operations.

However, according to my reasoning, I think the jemalloc strategy does not give up this memory even lmr.drop() is not called. Is this expected? Is there way to forget this memory in jemalloc strategy?

P.S. raw strategy works fine and no double free occurs.

@fishiu
Copy link
Author

fishiu commented Oct 13, 2023

This is my code

fn rdma_get_item<'a>(&mut self, py: Python<'a>) -> PyResult<&'a PyArray<u8, Dim<[usize; 1]>>> {
    let mut lmr = some_function();
    let lmr_len = (&lmr).length();
    let lmr_data_ptr = *lmr.as_mut_ptr();
    let lmr_data = unsafe { Vec::from_raw_parts(lmr_data_ptr, lmr_len, lmr_len) };

    std::mem::forget(lmr);

    let py_array = lmr_data.into_pyarray(py);
    Ok(py_array)
}

@GTwhy
Copy link
Collaborator

GTwhy commented Oct 21, 2023

jemalloc strategy does not give up this memory even lmr.drop() is not called.
I don’t quite understand the meaning of this sentence.
Do you mean: Je free the memory even drop is not called?

I think lmr.drop() -> jemalloc::free() will not be called if you use forget(lmr), and the memory should keep the allocated state(is this what you mean give up?).

But, we have not conducted any analysis or testing on transferring MR between different frameworks. Welcome contributions of complete application cases and discussions on potential issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants