liveBook · Manning

Explicit memory management

As explained in Refactoring to Rust

Manual memory management is a memory-management approach in which program developers explicitly control when memory is allocated and released.

Explicit memory management requires developers to request memory and mark when it may be released. The approach makes allocation and deallocation manual, especially for heap memory, rather than delegating cleanup to garbage collection. section 2.2

Manual control introduces practical trade-offs:

  • Requesting too much memory can slow allocation or waste memory. section 2.2
  • Requesting too little can cause programs to use memory outside the allocated block. section 2.2
  • Stack values are commonly released when their function returns, while heap memory requires greater management attention. section 2.2
Listing 2.6 The welcome program written with K
def welcome(name):
  print('Welcome ' + name)

name = input('Please enter your name: ')
welcome(name)
free(name)

The welcome program places an explicit free operation after the value is no longer needed, making the cleanup point visible. section 2.2

How Refactoring to Rust covers it

How deep it goes
Defines allocation and deallocation, explains stack and heap differences, and stops short of detailed implementation mechanics.
Examples
Uses a welcome program with explicit free operations to show where cleanup occurs.
What it assumes
Assumes basic familiarity with functions and values while defining allocation, deallocation, stack memory, and heap memory.
What you can do after
Evaluate allocation choices by weighing memory use, allocation cost, and risks from insufficient memory.
How it connects
Links manual memory management to heap memory, stack memory, allocation, deallocation, and garbage collection.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Refactoring to Rust. The section links point to the parts of the book it draws on.