Memory leak of rustc?

It is my first non-small project. about 6000 lines.

I fixed various errors and finally the compiler goes on, but never end.
rustc consumes more than 40GB memory.

How can I avoid it? Are there any statements that you need care?
Any suggestions are welcome.

My environment is,
OS X 10.11.1
rustc 1.4.0 (8ab8581f6 2015-10-27)

1 Like

Are you sure it's "40" GB?

It could also be some infinite recursion/iteration going on instead of a memleak. Have you tried splitting your project into multiple crates? a 6k loc crate is somewhat large for a crate imo.

Are you sure it's "40" GB?

Sure.

It could also be some infinite recursion/iteration going on instead of a memleak. Have you tried splitting your project into multiple crates? a 6k loc crate is somewhat large for a crate imo.

What is "imo"?

According your adivce, I will separate my crate into several ones.
Thank you for your comment!

Sorry for the internet slang. "imo" is "in my opinion".

If the issue still occurs, I'll be in a smaller crate, so you can keep splitting it up until you find the exact issue. If it's a Rust-issue, please post a bug report.

Splitting a project into a few crates did not help me.
I splitted 6000 lines into 3800, 2100, 37, and 7 lines.

(I needed to change code non-trivially in a few places when splitting.
In order to avoid inter-dependencies of structs, I had to add traits.)

An interesting thing is that each lib crate was compiled well, but main crate with 7 lines was not compiled. It never end.

The main crate just calls a startup function from main function.

Linker problem?

Is your project hosted anywhere?

Are you compiling with cargo build or cargo build --release? If the latter, it might be some bad llvm interaction.

Sorry, I told wrong report.
The problem occurs on one of lib crates.
I will split it more.

Thank you for your collaboration.

Such catastrophic memory use is very atypical e.g. libcore is 17K lines of actual code (32K including comments and blanks) and only requires 300MB of RAM to compile in debug mode. It will likely be caused by one specific function or module which means that splitting a crate is unlikely to solve the problem, other than narrowing down what is causing it. The latter can probably be done better by just commenting/#[cfg]ing off parts of the problematic crate until it is clear what makes the memory use explode.

The only time I've seen this is with macros: Compiling ambiguous macro invocation hangs forever · Issue #5067 · rust-lang/rust · GitHub

I finally extracted the code of cause

const KSIZE: i32 = 25;
const LENGTH: usize = 1 << KSIZE;

#[derive(Clone, Copy)]
struct LargePat {
    key: u64,
    id: usize,
    prob: f32,
}

static mut patterns: [LargePat; LENGTH] = [LargePat { key: 0, id: 0, prob: 0.0, }; LENGTH];

fn main() {

}

This code tries to obtain 670MB bss memory.

Kind of crazy, so I changed my code using lazy_static, and Mutex<Vec>, and compiled it successfully!

Thank you, guys!

2 Likes

Filed this issue here. Thanks for letting us know!

https://github.com/rust-lang/rust/issues/30083