October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

What `std::mem::forget` Actually Does to Heap Allocations

Rust’s std::mem::forget does not free memory. It consumes a value without running its destructor, so destructor-managed allocations and resources may remain unreleased.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

std::mem::forget(value) consumes value and skips its destructor; it does not itself free heap memory. If that destructor would have released a heap allocation or another resource, skipping it can leave the resource unreleased. That is allowed in safe Rust, though it can still be a memory or resource-management problem.

Does std::mem::forget free heap memory?

No. It prevents the value’s destructor from running. It is not an allocator operation that frees or reallocates memory. Whether heap memory remains allocated depends on what the value owns and what its destructor would normally do.

As an Amazon Associate I earn from qualifying purchases.

For example, a Vec owns a heap allocation for its elements. When the vector is dropped, its destructor releases that backing allocation. Forgetting the vector skips that cleanup:

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
let data = vec![1, 2, 3];
std::mem::forget(data);

After the call, the local binding no longer owns the vector, but the vector’s destructor did not run to release its allocation. This example illustrates the mechanism; not every value passed to forget owns heap memory.

What happens to the value and its resources?

The function takes ownership of its argument. Because that ownership has been consumed, the value will not later be dropped as the local binding goes out of scope. Any cleanup normally performed by its destructor is skipped.

  • Heap-backed value: A forgotten Vec can leave its backing allocation unreleased because the vector’s destructor does not run.
  • Other managed resource: A forgotten File is not closed by its destructor. The standard-library documentation uses this as an example of a resource leak.
  • Value with no relevant destructor cleanup: Forgetting it does not necessarily create a heap leak; the effect depends on the value’s ownership and destructor behavior.

Rust’s core documentation describes forget as circumventing a value’s destructor. The Rust core source documentation explains the function and its ownership-transfer example. The standard-library Vec documentation describes the vector’s heap allocation.

Is using mem::forget unsafe or undefined behavior?

Calling mem::forget is safe; it is not, by itself, undefined behavior. The Rust core documentation explains: “forget is not marked as unsafe, because Rust’s safety guarantees do not include a guarantee that destructors will always run.”

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

That rule matters for unsafe abstractions: they must not rely on callers always dropping returned values. Destructors may fail to run for reasons beyond an explicit call to forget, including reference cycles or termination through process::exit. The Rust Reference’s destructor rules likewise say types cannot generally rely on destructor execution for soundness except where the Reference guarantees it.

Safe does not mean harmless. A skipped destructor can retain memory, leave a file descriptor open, or otherwise prevent expected cleanup. The Rustonomicon’s discussion of leaks treats memory leaks as safe in Rust’s model while noting that they can still make a program incorrect.

When is mem::forget used?

Its specialized use is suppressing cleanup when ownership of an underlying resource has been transferred elsewhere. The Rust core documentation describes a File whose raw descriptor has been handed to code outside Rust: forgetting the File prevents its destructor from closing a descriptor now owned by that external code.

This is not a general memory-management technique. If the goal is simply to let Rust release a vector, string, box, or file normally, do not forget it; ordinary ownership and scope cleanup provide that behavior.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Should you use ManuallyDrop instead?

For carefully controlled destruction or ownership-transfer code, ManuallyDrop<T> is generally the more appropriate tool. It inhibits automatic destruction while keeping the wrapped value available for a deliberate operation. The Rust core documentation says it is typically preferred for specialized memory-ownership transfer.

Question mem::forget(value) ManuallyDrop<T>
What it does Consumes the value and skips its destructor. Wraps a value to prevent automatic destructor execution.
Typical role Suppresses cleanup, including after a resource has been transferred outside Rust. Allows destruction to be managed deliberately while retaining access to the wrapped value.
Main risk Cleanup is skipped; using it in an ownership transfer can be error-prone. Manual destruction requires care: exposing or dropping a value after it has already been dropped can violate safety requirements.

The sequencing can be important in unsafe transfer code. With ManuallyDrop, automatic destruction is disabled before extracting raw parts. With forget, the value is consumed only after extraction, leaving a window in which a panic could trigger an unwanted drop or double-free. The documented ManuallyDrop pattern favors a leak over a double-drop in that scenario.

ManuallyDrop<T> is not an uninitialized-memory wrapper such as MaybeUninit<T>; it has the same layout and bit validity as T. Its wrapper behavior and safety considerations are documented in the standard-library ManuallyDrop documentation. Most Rust code does not need either API for ordinary ownership.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.