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

MemoryUsage of Arc forgets to account for ret-counts #17

Open
matklad opened this issue Jun 11, 2021 · 3 comments
Open

MemoryUsage of Arc forgets to account for ret-counts #17

matklad opened this issue Jun 11, 2021 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@matklad
Copy link

matklad commented Jun 11, 2021

mem::size_of_val(self)
+ if tracker.track(Arc::as_ptr(self) as *const ()) {
self.as_ref().size_of_val(tracker)
} else {
0
}
}

This accounts for sizeof T stored inside the Arc, as well as sizeof of the Arc itself (fat or thin pointer). However, it misses the two counters that are stored in the ArcInner. In parcicular this currently returns 8: crate::size_of_val(&Arc::new(())). I'd expect 24 instead -- one usize for the pointer + 2 * usize for strong and weak count.

@Hywan Hywan self-assigned this Jun 15, 2021
@Hywan Hywan added the bug Something isn't working label Jun 15, 2021
@Hywan
Copy link
Contributor

Hywan commented Jun 17, 2021

I'm surprised that mem::size_of_val misses the size of the ArcInner. I need to take a look at it.

@matklad
Copy link
Author

matklad commented Jun 17, 2021

Yeah, it's a bit tricky. The only difference between size_of and size_of_val is in handling DST.

For Arc, the size_of is just a single pointer to the ArcInner heap allocated box. There isn't a function which can tell you the size of this box itself, you'll have to hard-code it.

Well, I guess there is a way to get this size without hard-coding, by asking the allocator about it. This is what servo's analogue dose:

https://github.com/servo/servo/blob/94414dfb9aa8d29318e4825a36e4fdd188fbb892/components/malloc_size_of/lib.rs

But that obviously requires support from the allocator.

@matklad
Copy link
Author

matklad commented Jun 17, 2021

That's completly unrelated to the issue, but, JFYI, rust-lang/rust-analyzer#9309

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants