Skip to content

Memory Leak [DO NOT CLOSE THIS] #8

@AsakuraMizu

Description

@AsakuraMizu

We need to continuously fix memory leaks. This issue is for reporting, tracking and fixing them, and also provides some guidance on detecting the root causes.

Guidance

We have set up some tracking capabilities in the memory allocator, including simple usage statistics, and more advanced allocation stack backtraces.

Memory usage statistics

This feature is always enabled. You can read /proc/meminfo2 to get the number of bytes currently allocated, such as:

starry:~# cat /proc/meminfo2
UsageStats { Rust Heap: 59252688, User Memory: 1638400, Page Cache: 524288, Page Table: 4317184, Dma: 24576, Global: 0 }

Note: /proc/meminfo2 is a temporary path name and may be changed in the future.

Leak detect

This feature has some performance impact, so it needs to be enabled by specifying MEMTRACK=y while building. It can effectively record all newly allocated and unreclaimed memory over a period of time and record the applicants by generating a stack backtrace during allocation.

Usage:

$ make run MEMTRACK=y ...
...
starry:~# echo start > /dev/memtrack 
Memory allocation generation stamped: 0
...
starry:~# echo end > /dev/memtrack
Alive tasks: ["Task(6, \"busybox\")", "Task(8, \"busybox\")"]
===========================
Memory usage:
 72792 bytes, 3033 allocations, Layout { size: 24, align: 8 (1 << 3) }, Backtrace:
   0: <axalloc::GlobalAllocator as core::alloc::global::GlobalAlloc>::alloc::{{closure}}
            at /home/mizu/projects/os/StarryOS/arceos/modules/axalloc/src/lib.rs:416:40 with fp=0xffff000044c7f9c0, ip=0xffff0000403262e4
   1: axalloc::tracking::with_state::{{closure}}
            at /home/mizu/projects/os/StarryOS/arceos/modules/axalloc/src/lib.rs:360:30 with fp=0xffff000044c7f9c0, ip=0xffff0000403262e4
   2: axalloc::tracking::IN_GLOBAL_ALLOCATOR_WRAPPER::with_current
            at /home/mizu/projects/os/StarryOS/arceos/modules/axalloc/src/lib.rs:316:5 with fp=0xffff000044c7f9c0, ip=0xffff0000403262e4
   3: axalloc::tracking::with_state
            at /home/mizu/projects/os/StarryOS/arceos/modules/axalloc/src/lib.rs:354:9 with fp=0xffff000044c7f9c0, ip=0xffff0000403262e4
   4: <axalloc::GlobalAllocator as core::alloc::global::GlobalAlloc>::alloc
            at /home/mizu/projects/os/StarryOS/arceos/modules/axalloc/src/lib.rs:406:13 with fp=0xffff000044c7f9c0, ip=0xffff0000403262e4
   5: alloc::alloc::alloc
            at /home/mizu/.rustup/toolchains/nightly-2025-05-20-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/alloc.rs:93:9 with fp=0xffff000044c7fa20, ip=0xffff00004032b9a8
   6: alloc::alloc::Global::alloc_impl
            at /home/mizu/.rustup/toolchains/nightly-2025-05-20-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/alloc.rs:188:73 with fp=0xffff000044c7fa20, ip=0xffff00004032b9a8
   7: <alloc::alloc::Global as core::alloc::Allocator>::allocate
            at /home/mizu/.rustup/toolchains/nightly-2025-05-20-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/alloc.rs:249:9 with fp=0xffff000044c7fa20, ip=0xffff00004032b9a8
   8: alloc::alloc::exchange_malloc
            at /home/mizu/.rustup/toolchains/nightly-2025-05-20-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/alloc.rs:350:18 with fp=0xffff000044c7fa20, ip=0xffff00004032b9a8
   9: alloc::boxed::Box<T>::new
            at /home/mizu/.rustup/toolchains/nightly-2025-05-20-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/boxed.rs:261:16 with fp=0xffff000044c7fa20, ip=0xffff00004032b9a8
  10: axio::poll::PollSet::register
            at /home/mizu/.cargo/git/checkouts/axio-00998c4fb7c77d99/e11ca75/src/poll.rs:67:45 with fp=0xffff000044c7fa20, ip=0xffff00004032b9a8
  11: <starry_api::terminal::job::JobControl as axio::poll::Pollable>::register
            at /home/mizu/projects/os/StarryOS/api/src/terminal/job.rs:79:13 with fp=0xffff000044c7fab0, ip=0xffff000040245f04
  12: <starry_api::vfs::dev::tty::Tty<R,W> as axio::poll::Pollable>::register
            at /home/mizu/projects/os/StarryOS/api/src/vfs/dev/tty.rs:199:13 with fp=0xffff000044c7fab0, ip=0xffff000040245f04
  13: <starry_api::syscall::io_mpx::FdPollSet as axio::poll::Pollable>::register
            at /home/mizu/projects/os/StarryOS/api/src/syscall/io_mpx/mod.rs:21:13 with fp=0xffff000044c7faf0, ip=0xffff00004024f5dc
  14: axtask::future::Poller<P>::poll::{{closure}}
            at /home/mizu/projects/os/StarryOS/arceos/modules/axtask/src/future.rs:267:25 with fp=0xffff000044c7faf0, ip=0xffff00004024f5dc
  15: <core::future::poll_fn::PollFn<F> as core::future::future::Future>::poll
            at /home/mizu/.rustup/toolchains/nightly-2025-05-20-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/future/poll_fn.rs:151:9 with fp=0xffff000044c7faf0, ip=0xffff00004024f5dc
  16: <axtask::future::Timeout<F> as core::future::future::Future>::poll
            at /home/mizu/projects/os/StarryOS/arceos/modules/axtask/src/future.rs:214:13 with fp=0xffff000044c7fbf0, ip=0xffff000040213e68
  17: <futures_util::future::future::map::Map<Fut,F> as core::future::future::Future>::poll
            at /home/mizu/.cargo/registry/src/mirrors.cernet.edu.cn-cf4f1d0b4f95b1fb/futures-util-0.3.31/src/future/future/map.rs:55:37 with fp=0xffff000044c7fbf0, ip=0xffff000040213e68
  18: <futures_util::future::future::Map<Fut,F> as core::future::future::Future>::poll
            at /home/mizu/.cargo/registry/src/mirrors.cernet.edu.cn-cf4f1d0b4f95b1fb/futures-util-0.3.31/src/lib.rs:86:13 with fp=0xffff000044c7fbf0, ip=0xffff000040213e68
  19: axtask::future::try_block_on
            at /home/mizu/projects/os/StarryOS/arceos/modules/axtask/src/future.rs:83:15 with fp=0xffff000044c7fbf0, ip=0xffff000040213e68
  20: axtask::future::block_on_interruptible
            at /home/mizu/projects/os/StarryOS/arceos/modules/axtask/src/future.rs:111:5 with fp=0xffff000044c7fbf0, ip=0xffff000040213e68
  21: axtask::future::Poller<P>::poll
            at /home/mizu/projects/os/StarryOS/arceos/modules/axtask/src/future.rs:259:9 with fp=0xffff000044c7fbf0, ip=0xffff000040213e68
  22: starry_api::syscall::io_mpx::poll::do_poll::{{closure}}
            at /home/mizu/projects/os/StarryOS/api/src/syscall/io_mpx/poll.rs:56:15 with fp=0xffff000044c7fc70, ip=0xffff000040255a0c
  23: starry_api::signal::with_replacen_blocked
            at /home/mizu/projects/os/StarryOS/api/src/signal.rs:61:5 with fp=0xffff000044c7fc70, ip=0xffff000040255a0c
  24: starry_api::syscall::io_mpx::poll::do_poll
            at /home/mizu/projects/os/StarryOS/api/src/syscall/io_mpx/poll.rs:55:5 with fp=0xffff000044c7fd40, ip=0xffff00004026db24
  25: starry_api::syscall::handle_syscall
            at /home/mizu/projects/os/StarryOS/api/src/syscall/mod.rs:218:25 with fp=0xffff000044c7fdf0, ip=0xffff00004023096c
  26: starry_api::task::new_user_task::{{closure}}
            at /home/mizu/projects/os/StarryOS/api/src/task.rs:51:46 with fp=0xffff000044c7ff80, ip=0xffff00004023ee08
  27: core::ops::function::FnOnce::call_once{{vtable.shim}}
            at /home/mizu/.rustup/toolchains/nightly-2025-05-20-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:250:5 with fp=0xffff000044c7ff80, ip=0xffff00004023ee08
  28: <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once
            at /home/mizu/.rustup/toolchains/nightly-2025-05-20-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/boxed.rs:1966:9 with fp=0xffff000044c7ffe0, ip=0xffff000040318760
  29: axtask::task::task_entry
            at /home/mizu/projects/os/StarryOS/arceos/modules/axtask/src/task.rs:559:9 with fp=0xffff000044c7ffe0, ip=0xffff000040318760
  30: <alloc::sync::Arc<T,A> as core::clone::Clone>::clone
            at /home/mizu/.rustup/toolchains/nightly-2025-05-20-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/sync.rs:2226:13 with fp=0x0, ip=0xffff000040318704
  31: axtask::task::CurrentTask::clone
            at /home/mizu/projects/os/StarryOS/arceos/modules/axtask/src/task.rs:510:24 with fp=0x0, ip=0xffff000040318704

 25536 bytes, 12 allocations, Layout { size: 152, align: 8 (1 << 3) }, [lwext4]
 4728 bytes, 24 allocations, Layout { size: 3616, align: 8 (1 << 3) }, [cached file]
 2734 bytes, 21 allocations, Layout { size: 1224, align: 8 (1 << 3) }, [dentry]
 ...

The output may be very long, so it is recommended to use it with tee.

Also note that /dev/memtrack may be changed in the future.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingenhancementNew feature or requesthelp wantedExtra attention is needed

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions