In Motoko you can use Prim.rts_memory_size()
and in Rust it seems to be memory_size in core::arch::wasm32 - Rust .
Have a look at this thread for more details: Motoko, Array, Memory
This repo does this in Rust:
and there might be other examples in the following thread: ICDevs.org Bounty #20 - QuickStart Dapp - Scaling With Canisters - 200 ICP, 100 ICP, 50 ICP - Multiple winners