-
Notifications
You must be signed in to change notification settings - Fork 42
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
Memory Leak #160
Comments
I don't think it's a leak, it's just surprising memory allocator behavior in a threaded process. Try (on Linux) with: [dependencies]
tikv-jemalloc-ctl = { version = "0.6.0", features = ["stats", "use_std"] }
tikv-jemallocator = "0.6.0" use tikv_jemalloc_ctl::{epoch, stats};
use tikv_jemallocator::Jemalloc;
#[global_allocator]
static GLOBAL: Jemalloc = Jemalloc;
#[get("/")]
fn index() -> &'static str {
{
let _: Vec<geos::Geometry> = (0..2_000_000)
.map(|_| geos::Geometry::create_empty_point().unwrap())
.collect();
}
epoch::advance().unwrap();
let allocated = stats::allocated::read().unwrap();
let resident = stats::resident::read().unwrap();
println!("{} bytes allocated/{} bytes resident", allocated, resident);
"finished"
} I get:
While |
I see. |
I'm not sure. jemalloc reports it as "retained", which should show up as VIRT, but not RES. And Valgrind says there's no leak. |
Ah, so GEOS won't use my custom allocator. Try |
Thank you! |
Also, is there a way to make GEOS use tikv_jemallocator? Is that the case for all external bindings? |
Yeah, that's not ideal, see also #141. I don't think the Rust allocator mechanism can handle C libraries. The second option from https://github.com/jemalloc/jemalloc/wiki/getting-started might work, but I'm not sure. |
Ah, easy-peasy: |
A dumb question, why should Rust be using the Re-entrant API, if we can wrap the regular API with |
It's a bit weird, TBH. On the other hand,
Depending on your needs, you could take a look at the |
I see, weird.
I'm already using gdal extensively. I need You still didn't answer my question 😅. If a any non thread-safe function can be wrapped with Arc to share it among threads, and Mutex to mutate it among threads, can't the same logic be applied to C functions as well? |
What about BufferEx?
It can't. If
A single context and
I'm not sure I understand, but it's probably not easy. Ideally, you want as few contexts as possible, to save memory and so on. But on the other hand, you might want to process geometries on multiple threads, and you can't do that within a single context. PS: |
Not present in georust/gdal :')
No I don't mean that
I'm talking about the use of the normal C API of libgeos that does NOT have context whatsoever. See C API and reentrant C API |
File a PR, an issue, or use
The "normal" API uses a global context, that's not going to be thread-safe. |
I'll try, but I also want geos directly because
I see.
I see, I need to learn Rust more. Why are you recommending other tools than this project? |
You can, but are you sure it's correct? I don't know which context GEOS uses for operations that involve two geometries, but you can see how that might be problematic, right? At least you have a different context for each geometry, that's good (helps with thread-safety).
They don't allow as much thread-unsafety, and don't allocate a huge context per geometry? |
So it's dangerous to use this crate xD. I mean about the maintenance of this project. Is it maintained? |
I simplified it as much as possible. Here's my Cargo.toml
main.rs:
GEOS version:
3.12.1-CAPI-1.18.1
Every time I request
curl http://127.0.0.1:8000/
, I get a +3GB spike in memory. It never frees it. It continues allocating more and more until my OS runs out of memory and crashes. I'm on Ubuntu 24.04.If you think this is related to rocket, I don't think it is. Here's a code where I only used
Vec<i64>
:This code spikes memory to about 7GB, but it instantly frees after the request is completed.
Here's the geos-sys version:
Interesting, but why
geos::Geometry::create_empty_point()
creates a separate context for each new geometry? According to GEOS_init_r(), the context should be passed to other_r
functions. I think that's using a lot of memory.In this geos-sys code, I was able to create 10 million points and only take up 1GB. The memory usage spiked from 1GB, 2GB, 3GB, and then it stayed on 3GB.
The text was updated successfully, but these errors were encountered: