为什么 Valgrind 没有检测到使用 nightly 1.29.0 的 Rust 程序中的内存泄漏?
Why does Valgrind not detect a memory leak in a Rust program using nightly 1.29.0?
我正在尝试在 this blog post 之后使用 Valgrind 检测 Rust 程序中的内存泄漏。我的源代码很简单:
#![feature(alloc_system)]
extern crate alloc_system;
use std::mem;
fn allocate() {
let bad_vec = vec![0u8; 1024*1024];
mem::forget(bad_vec);
}
fn main() {
allocate();
}
我希望对 mem::forget()
的调用会产生内存泄漏,而 Valgrind 能够发现这一点。但是,当我 运行 Valgrind 时,它报告说不可能有泄漏:
[memtest]> cargo run
Compiling memtest v0.1.0 (file:///home/icarruthers/memtest)
Finished dev [unoptimized + debuginfo] target(s) in 0.28s
Running `target/debug/memtest`
[memtest]> valgrind target/debug/memtest
==18808== Memcheck, a memory error detector
==18808== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==18808== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==18808== Command: target/debug/memtest
==18808==
==18808==
==18808== HEAP SUMMARY:
==18808== in use at exit: 0 bytes in 0 blocks
==18808== total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==18808==
==18808== All heap blocks were freed -- no leaks are possible
==18808==
==18808== For counts of detected and suppressed errors, rerun with: -v
==18808== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
我升级到最新的nightly(1.29.0-nightly (6a1c0637c 2018-07-23))。
我错过了什么?
生锈 1.32
从 Rust 1.32 开始,可执行文件的 默认分配器 是 now the system allocator,所以默认情况下您不需要设置任何东西。
以前的版本
您没有正确使用全局分配器设置。这是一个 nightly 功能,这意味着它随时可能更改。您的博客 post 已过时。
检查 module docs for std::alloc
以查看正确的用法:
#![feature(alloc_system)]
extern crate alloc_system;
#[global_allocator]
static GLOBAL: alloc_system::System = alloc_system::System;
use std::mem;
fn allocate() {
let bad_vec = vec![0u8; 1024*1024];
mem::forget(bad_vec);
}
fn main() {
allocate();
}
root@3fb431791293:/tmp/vg# valgrind target/debug/vg
==6326== Memcheck, a memory error detector
==6326== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==6326== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==6326== Command: target/debug/vg
==6326==
==6326==
==6326== HEAP SUMMARY:
==6326== in use at exit: 1,048,576 bytes in 1 blocks
==6326== total heap usage: 12 allocs, 11 frees, 1,050,753 bytes allocated
==6326==
==6326== LEAK SUMMARY:
==6326== definitely lost: 1,048,576 bytes in 1 blocks
==6326== indirectly lost: 0 bytes in 0 blocks
==6326== possibly lost: 0 bytes in 0 blocks
==6326== still reachable: 0 bytes in 0 blocks
==6326== suppressed: 0 bytes in 0 blocks
==6326== Rerun with --leak-check=full to see details of leaked memory
==6326==
==6326== For counts of detected and suppressed errors, rerun with: -v
==6326== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
我正在尝试在 this blog post 之后使用 Valgrind 检测 Rust 程序中的内存泄漏。我的源代码很简单:
#![feature(alloc_system)]
extern crate alloc_system;
use std::mem;
fn allocate() {
let bad_vec = vec![0u8; 1024*1024];
mem::forget(bad_vec);
}
fn main() {
allocate();
}
我希望对 mem::forget()
的调用会产生内存泄漏,而 Valgrind 能够发现这一点。但是,当我 运行 Valgrind 时,它报告说不可能有泄漏:
[memtest]> cargo run
Compiling memtest v0.1.0 (file:///home/icarruthers/memtest)
Finished dev [unoptimized + debuginfo] target(s) in 0.28s
Running `target/debug/memtest`
[memtest]> valgrind target/debug/memtest
==18808== Memcheck, a memory error detector
==18808== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==18808== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==18808== Command: target/debug/memtest
==18808==
==18808==
==18808== HEAP SUMMARY:
==18808== in use at exit: 0 bytes in 0 blocks
==18808== total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==18808==
==18808== All heap blocks were freed -- no leaks are possible
==18808==
==18808== For counts of detected and suppressed errors, rerun with: -v
==18808== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
我升级到最新的nightly(1.29.0-nightly (6a1c0637c 2018-07-23))。
我错过了什么?
生锈 1.32
从 Rust 1.32 开始,可执行文件的 默认分配器 是 now the system allocator,所以默认情况下您不需要设置任何东西。
以前的版本
您没有正确使用全局分配器设置。这是一个 nightly 功能,这意味着它随时可能更改。您的博客 post 已过时。
检查 module docs for std::alloc
以查看正确的用法:
#![feature(alloc_system)]
extern crate alloc_system;
#[global_allocator]
static GLOBAL: alloc_system::System = alloc_system::System;
use std::mem;
fn allocate() {
let bad_vec = vec![0u8; 1024*1024];
mem::forget(bad_vec);
}
fn main() {
allocate();
}
root@3fb431791293:/tmp/vg# valgrind target/debug/vg
==6326== Memcheck, a memory error detector
==6326== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==6326== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==6326== Command: target/debug/vg
==6326==
==6326==
==6326== HEAP SUMMARY:
==6326== in use at exit: 1,048,576 bytes in 1 blocks
==6326== total heap usage: 12 allocs, 11 frees, 1,050,753 bytes allocated
==6326==
==6326== LEAK SUMMARY:
==6326== definitely lost: 1,048,576 bytes in 1 blocks
==6326== indirectly lost: 0 bytes in 0 blocks
==6326== possibly lost: 0 bytes in 0 blocks
==6326== still reachable: 0 bytes in 0 blocks
==6326== suppressed: 0 bytes in 0 blocks
==6326== Rerun with --leak-check=full to see details of leaked memory
==6326==
==6326== For counts of detected and suppressed errors, rerun with: -v
==6326== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)