Benchmarksgame -nbody memory usage

Dear Rustaceans,
I have been taking a look at the nbody benchmark results from Benchmarksgame,
and I'm really curious about how Fortran and C implementations manage to use so little memory..

The fastest Rust implementation of nbody uses much more memory than the C implementation, but if I examine the assembly code output of the two different implementations, I can't really find the big difference in memory usage..

Could it be an issue with the auto-import of Rust Std ?? C++ also uses much more memory than C.
I'm looking to see if it is possible to reduce memory usage of the Rust implementation of the nbody benchmark. Any advice would be appreciated! Thank you :slight_smile:

1 Like

What units are memory in? If it's bytes, I don't much care about <1k on a machine with 4GB RAM. Also, note that the fastest Rust one (#7) is a port of "C gcc #4", which actually uses more memory than the Rust port.

I suspect that the "it's from std" is probably a good hypothesis. Why are you looking to reduce its memory usage? Have you tried just doing a direct port of "gcc #8" to see how it compares?

1 Like

It uses GLIBTOP_PROC_MEM_RESIDENT on Linux which is measured in pages according to https://developer.gnome.org/libgtop/stable/libgtop-procmem.html.

The program doesn't seem to allocate memory dynamically. The only suspect I see is a predefined array for 1000 elements, but that should be in kilobytes range.

The memory usage is in units of KB. Sorry for not mentioning that in the post.

Blockquote Have you tried just doing a direct port of "gcc #8" to see how it compares?

Wow, I should definitely try doing a direct port of "gcc#8" to Rust. I only attempted to compare the source code and the generated assembly from "Rust #7" and "gcc #8"..

The reason I'm looking to reduce its memory usage is to identify whether the memory usage difference comes from the language implementation difference or just difference in optimizations.

Thanks for your suggestion! :slight_smile:

Since most (all?) of the rust versions take 872k, it's probably not anything specific to #6. Might also be interesting to use the system allocator, which C will be using.

Thanks for your input. Since the nbody benchmark doesn't use any heap memory, I think trying the system allocator would probably not make much of a change in this case(plz correct me if I'm wrong). But it would definitely be interesting to try different system allocators in rust programs using heaps!

@scottmcm @bjorn3 @kornel @cbiffle

Just a brief update FYI, I did a direct port of C gcc #8 (which uses only 8KB memory) to Rust, and the ported version(Rust) still uses much more memory in my local machine (868 KB).

I also executed C gcc #4 (which is showed on the Benchmarksgame webpage that it uses 1108 KB) in my local machine, but it only uses 4 KB memory.

My current best guess to explain the relatively low memory usage of C implementations is...

  1. Rust std is imported by default (causing more memory usage?)
  2. C apps can use std libraries with little overhead (libc)

p.s. I checked the compiler flags used for both C and Rust programs at Benchmarksgame, and all the programs were compiled with static linking turned on.

1 Like

Simply importing std shouldn't affect memory usage. If you can trick the program into running for a long time (perhaps by adding an infinite loop toward the end), it would be interesting to inspect /proc/{pid}/maps and see where the memory is actually being used. (Assuming you're on Linux.)

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.