<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/box-to-save-memory-foxzggogq" -->

---
title: Box to save memory | daily.dev
description: A real-world Rust optimization that reduced memory usage from 895 MB to ~435 MB by boxing optional structs. The key insight: unlike Java/Python where a null...
canonical: https://daily.dev/posts/box-to-save-memory-foxzggogq
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Box to save memory | daily.dev
og:description: A real-world Rust optimization that reduced memory usage from 895 MB to ~435 MB by boxing optional structs. The key insight: unlike Java/Python where a null...
og:url: https://daily.dev/posts/box-to-save-memory-foxzggogq
og:image: https://api.daily.dev/og/posts/FOxzGgoGQ.png
og:image:alt: Box to save memory
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Box to save memory

**[Dystroy](https://daily.dev/sources/dystroy)** · 7 min read · 66 upvotes · 6 comments

## Summary

A real-world Rust optimization that reduced memory usage from 895 MB to ~435 MB by boxing optional structs. The key insight: unlike Java/Python where a null reference costs only one pointer, a Rust `Option<BigStruct>` still occupies the full size of `BigStruct` even when `None`. The fix is to use `Option<Box<BigStruct>>` so a `None` costs only one word. The post walks through the struct layout math, shows a custom Serde deserializer that discards empty structs instead of storing them, and demonstrates how to measure heap usage with jemalloc via a Cargo feature flag.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://dystroy.org/blog/box-to-save-memory/>

## Community discussion

Top comments from developers on daily.dev.

**@beefyboy** · 10 upvotes

> That is incredibly cool! I'll keep note of this optimization! It seems so simple to change the big struct into a box, but I guess I had really put in my mind to have the minimum heap allocations. I guess you could say that you thought _outside the box_ with that one! Hahahaha... yeah that wasn't that funny...

**@quetalocatl** · 7 upvotes

> Rustaceans usually prefer extracting only the necessary data from a large struct rather than passing the entire struct around. I don’t think Box is the best optimization for every case.

**@azelytof** · 3 upvotes

> I don't think that if all fields in a strict are optional, you have to move the optional on the parent
>
> Because the struct Saumya that there can be only one field filled. On the other hand, if the optional is on the parent side, all fields must be filled.
>
> Or maybe I've misunderstood something in the explanation?

**@khauma** · 2 upvotes

> Very interesting read!

## Similar posts on daily.dev

- [Where Your Bytes Live: Memory Layout in Rust and Java](https://daily.dev/posts/where-your-bytes-live-memory-layout-in-rust-and-java-tjqya8qqa) · Medium · 3 upvotes · 1 comments
- [Nobody ever got fired for using a struct](https://daily.dev/posts/nobody-ever-got-fired-for-using-a-struct-f1pngvnhw) · Hacker News · 29 upvotes · 0 comments
- [Niches for integer types in Rust](https://daily.dev/posts/niches-for-integer-types-in-rust-grufxq4r3) · Pascal’s scribbles · 3 upvotes · 0 comments

---

Tags: [#rust](https://daily.dev/tags/rust)

[View this post on daily.dev](https://daily.dev/posts/box-to-save-memory-foxzggogq)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Box to save memory","url":"https://daily.dev/posts/box-to-save-memory-foxzggogq","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/box-to-save-memory-foxzggogq"},"datePublished":"2026-04-23T16:37:54.937Z","dateModified":"2026-04-23T16:38:23.125Z","description":"A real-world Rust optimization that reduced memory usage from 895 MB to ~435 MB by boxing optional structs. The key insight: unlike Java/Python where a null...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/86c1779588c59bfe6f5411be43dd3635?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/86c1779588c59bfe6f5411be43dd3635?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Dystroy","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Dystroy","logo":"https://media.daily.dev/image/upload/s--n2ffxB54--/f_auto,q_auto/v1774960093/logos/dystroy?_a=BAMAMiWQ0","url":"https://daily.dev/sources/dystroy"},"commentCount":6,"discussionUrl":"https://daily.dev/posts/box-to-save-memory-foxzggogq","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":66},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":6}],"keywords":"rust","timeRequired":"PT7M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Dystroy","item":"https://daily.dev/sources/dystroy"},{"@type":"ListItem","position":3,"name":"Box to save memory"}]}
{"@context":"https://schema.org","@type":"WebPage","@id":"https://daily.dev/posts/box-to-save-memory-foxzggogq","comment":[{"@type":"Comment","text":"That is incredibly cool! I’ll keep note of this optimization! It seems so simple to change the big struct into a box, but I guess I had really put in my mind to have the minimum heap allocations. I guess you could say that you thought outside the box with that one! Hahahaha… yeah that wasn’t that funny…","datePublished":"2026-04-24T03:23:20.415Z","url":"https://daily.dev/posts/FOxzGgoGQ#c-vkHcn8Tag","author":{"@type":"Person","name":"BeefyBoy","url":"https://daily.dev/beefyboy","image":"https://media.daily.dev/image/upload/s--PMTQA7TD--/f_auto/v1738440409/avatars/avatar_Rjs2bk07ngT4q9dOqGvNY"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":10}},{"@type":"Comment","text":"Rustaceans usually prefer extracting only the necessary data from a large struct rather than passing the entire struct around. I don’t think Box is the best optimization for every case.","datePublished":"2026-04-24T03:43:51.229Z","url":"https://daily.dev/posts/FOxzGgoGQ#c-Q6dnyI8Z1","author":{"@type":"Person","name":"Jimmy NAGI","url":"https://daily.dev/quetalocatl","image":"https://lh3.googleusercontent.com/a/ACg8ocImdKaHdQZn6wRdVnu9Yjee969_5MvcyvPy4RWlTtQVJLMS=s96-c"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":7}},{"@type":"Comment","text":"I don’t think that if all fields in a strict are optional, you have to move the optional on the parent\nBecause the struct Saumya that there can be only one field filled. On the other hand, if the optional is on the parent side, all fields must be filled.\nOr maybe I’ve misunderstood something in the explanation?","datePublished":"2026-04-25T12:21:51.523Z","url":"https://daily.dev/posts/FOxzGgoGQ#c-JoInrWKM9","author":{"@type":"Person","name":"Azelytof","url":"https://daily.dev/azelytof","image":"https://lh3.googleusercontent.com/a/ACg8ocI6RrHgdhLNCYcIhA12ZPL-dV2oA0lQmAMXTmUgpDPCLvTK-YI=s96-c"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":3}},{"@type":"Comment","text":"Very interesting read!","datePublished":"2026-04-24T10:55:59.554Z","url":"https://daily.dev/posts/FOxzGgoGQ#c-nIiJWWho1","author":{"@type":"Person","name":"Kahlil Wallace","url":"https://daily.dev/khauma","image":"https://media.daily.dev/image/upload/s--QvppE9Ip--/f_auto/v1767378558/avatars/avatar_7eQFeSQRDx0muV3n92gxT?_a=BAMAK+ZW0"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":2}}]}
```

