[{"data":1,"prerenderedAt":1430},["ShallowReactive",2],{"navigation_docs":3,"-advanced-go-memory-and-garbage-collection":377,"-advanced-go-memory-and-garbage-collection-surround":1425},[4,39,114,144,184,244,293,362],{"title":5,"icon":6,"path":7,"stem":8,"children":9,"page":6},"Getting Started",false,"\u002Fgetting-started","1.getting-started",[10,15,20,25,30,35],{"title":11,"path":12,"stem":13,"icon":14},"Learning Path","\u002Fgetting-started\u002Flearning-path","1.getting-started\u002F1.learning-path","i-lucide-map",{"title":16,"path":17,"stem":18,"icon":19},"Introduction","\u002Fgetting-started\u002Fintroduction","1.getting-started\u002F2.introduction","i-lucide-house",{"title":21,"path":22,"stem":23,"icon":24},"Installation","\u002Fgetting-started\u002Finstallation","1.getting-started\u002F3.installation","i-lucide-download",{"title":26,"path":27,"stem":28,"icon":29},"Your First Program","\u002Fgetting-started\u002Ffirst-program","1.getting-started\u002F4.first-program","i-lucide-play",{"title":31,"path":32,"stem":33,"icon":34},"Modules and Dependencies","\u002Fgetting-started\u002Fmodules-and-dependencies","1.getting-started\u002F5.modules-and-dependencies","i-lucide-package-open",{"title":36,"path":37,"stem":38,"icon":34},"Advanced Modules","\u002Fgetting-started\u002Fadvanced-modules","1.getting-started\u002F6.advanced-modules",{"title":40,"path":41,"stem":42,"children":43,"page":6},"Fundamentals","\u002Ffundamentals","2.fundamentals",[44,49,54,59,64,69,74,79,84,89,94,99,104,109],{"title":45,"path":46,"stem":47,"icon":48},"Program Structure","\u002Ffundamentals\u002Fprogram-structure","2.fundamentals\u002F1.program-structure","i-lucide-workflow",{"title":50,"path":51,"stem":52,"icon":53},"Structs and Methods","\u002Ffundamentals\u002Fstructs-and-methods","2.fundamentals\u002F10.structs-and-methods","i-lucide-box",{"title":55,"path":56,"stem":57,"icon":58},"Interfaces","\u002Ffundamentals\u002Finterfaces","2.fundamentals\u002F11.interfaces","i-lucide-plug",{"title":60,"path":61,"stem":62,"icon":63},"Error Handling","\u002Ffundamentals\u002Ferrors","2.fundamentals\u002F12.errors","i-lucide-triangle-alert",{"title":65,"path":66,"stem":67,"icon":68},"Defined Types and Type Aliases","\u002Ffundamentals\u002Fdefined-types","2.fundamentals\u002F13.defined-types","i-lucide-tags",{"title":70,"path":71,"stem":72,"icon":73},"Generics","\u002Ffundamentals\u002Fgenerics","2.fundamentals\u002F14.generics","i-lucide-blocks",{"title":75,"path":76,"stem":77,"icon":78},"Project Structure","\u002Ffundamentals\u002Fproject-structure","2.fundamentals\u002F2.project-structure","i-lucide-folder-tree",{"title":80,"path":81,"stem":82,"icon":83},"Project Layout","\u002Ffundamentals\u002Fproject-layout","2.fundamentals\u002F3.project-layout","i-lucide-layout",{"title":85,"path":86,"stem":87,"icon":88},"Variables and Data Types","\u002Ffundamentals\u002Fvariables-and-types","2.fundamentals\u002F4.variables-and-types","i-lucide-variable",{"title":90,"path":91,"stem":92,"icon":93},"Functions","\u002Ffundamentals\u002Ffunctions","2.fundamentals\u002F5.functions","i-lucide-braces",{"title":95,"path":96,"stem":97,"icon":98},"Control Statements","\u002Ffundamentals\u002Fcontrol-statements","2.fundamentals\u002F6.control-statements","i-lucide-git-branch",{"title":100,"path":101,"stem":102,"icon":103},"Pointers","\u002Ffundamentals\u002Fpointers","2.fundamentals\u002F7.pointers","i-lucide-mouse-pointer-2",{"title":105,"path":106,"stem":107,"icon":108},"Arrays and Slices","\u002Ffundamentals\u002Farrays-and-slices","2.fundamentals\u002F8.arrays-and-slices","i-lucide-list",{"title":110,"path":111,"stem":112,"icon":113},"Maps","\u002Ffundamentals\u002Fmaps","2.fundamentals\u002F9.maps","i-lucide-table-properties",{"title":115,"path":116,"stem":117,"children":118,"page":6},"Concurrency","\u002Fconcurrency","3.concurrency",[119,124,129,134,139],{"title":120,"path":121,"stem":122,"icon":123},"Concurrency Model","\u002Fconcurrency\u002Fconcurrency-model","3.concurrency\u002F1.concurrency-model","i-lucide-network",{"title":125,"path":126,"stem":127,"icon":128},"Goroutines","\u002Fconcurrency\u002Fgoroutines","3.concurrency\u002F2.goroutines","i-lucide-git-fork",{"title":130,"path":131,"stem":132,"icon":133},"Channels","\u002Fconcurrency\u002Fchannels","3.concurrency\u002F3.channels","i-lucide-arrow-left-right",{"title":135,"path":136,"stem":137,"icon":138},"Select and Cancellation","\u002Fconcurrency\u002Fselect-and-cancellation","3.concurrency\u002F4.select-and-cancellation","i-lucide-circle-stop",{"title":140,"path":141,"stem":142,"icon":143},"Synchronization","\u002Fconcurrency\u002Fsynchronization","3.concurrency\u002F5.synchronization","i-lucide-lock-keyhole",{"title":145,"path":146,"stem":147,"children":148,"page":6},"Standard Library","\u002Fstandard-library","4.standard-library",[149,154,159,164,169,174,179],{"title":150,"path":151,"stem":152,"icon":153},"Standard Library Overview","\u002Fstandard-library\u002Foverview","4.standard-library\u002F1.overview","i-lucide-library",{"title":155,"path":156,"stem":157,"icon":158},"Text and Formatting","\u002Fstandard-library\u002Ftext-and-formatting","4.standard-library\u002F2.text-and-formatting","i-lucide-text",{"title":160,"path":161,"stem":162,"icon":163},"Files and JSON","\u002Fstandard-library\u002Ffiles-and-json","4.standard-library\u002F3.files-and-json","i-lucide-file-json",{"title":165,"path":166,"stem":167,"icon":168},"HTTP Clients and Servers","\u002Fstandard-library\u002Fhttp","4.standard-library\u002F4.http","i-lucide-server",{"title":170,"path":171,"stem":172,"icon":173},"Testing","\u002Fstandard-library\u002Ftesting","4.standard-library\u002F5.testing","i-lucide-flask-conical",{"title":175,"path":176,"stem":177,"icon":178},"Command-Line Tools and Configuration","\u002Fstandard-library\u002Fcommand-line-and-configuration","4.standard-library\u002F6.command-line-and-configuration","i-lucide-terminal-square",{"title":180,"path":181,"stem":182,"icon":183},"Structured Logging","\u002Fstandard-library\u002Fstructured-logging","4.standard-library\u002F7.structured-logging","i-lucide-scroll-text",{"title":185,"path":186,"stem":187,"children":188,"page":6},"Backend Development","\u002Fbackend-development","5.backend-development",[189,194,199,204,209,214,219,224,229,234,239],{"title":190,"path":191,"stem":192,"icon":193},"Backend Roadmap","\u002Fbackend-development\u002Fbackend-roadmap","5.backend-development\u002F1.backend-roadmap","i-lucide-route",{"title":195,"path":196,"stem":197,"icon":198},"Graceful Shutdown","\u002Fbackend-development\u002Fgraceful-shutdown","5.backend-development\u002F10.graceful-shutdown","i-lucide-power",{"title":200,"path":201,"stem":202,"icon":203},"Production Checklist","\u002Fbackend-development\u002Fproduction-checklist","5.backend-development\u002F11.production-checklist","i-lucide-clipboard-check",{"title":205,"path":206,"stem":207,"icon":208},"HTTP API Design","\u002Fbackend-development\u002Fhttp-api-design","5.backend-development\u002F2.http-api-design","i-lucide-waypoints",{"title":210,"path":211,"stem":212,"icon":213},"Middleware","\u002Fbackend-development\u002Fmiddleware","5.backend-development\u002F3.middleware","i-lucide-layers-3",{"title":215,"path":216,"stem":217,"icon":218},"Validation","\u002Fbackend-development\u002Fvalidation","5.backend-development\u002F4.validation","i-lucide-list-checks",{"title":220,"path":221,"stem":222,"icon":223},"Application Wiring","\u002Fbackend-development\u002Fapplication-wiring","5.backend-development\u002F5.application-wiring","i-lucide-cable",{"title":225,"path":226,"stem":227,"icon":228},"Observability","\u002Fbackend-development\u002Fobservability","5.backend-development\u002F6.observability","i-lucide-activity",{"title":230,"path":231,"stem":232,"icon":233},"Database SQL","\u002Fbackend-development\u002Fdatabase-sql","5.backend-development\u002F7.database-sql","i-lucide-database",{"title":235,"path":236,"stem":237,"icon":238},"Transactions","\u002Fbackend-development\u002Ftransactions","5.backend-development\u002F8.transactions","i-lucide-repeat-2",{"title":240,"path":241,"stem":242,"icon":243},"Authentication and Authorization","\u002Fbackend-development\u002Fauthentication","5.backend-development\u002F9.authentication","i-lucide-shield-check",{"title":245,"path":246,"stem":247,"children":248,"page":6},"Advanced Go","\u002Fadvanced-go","6.advanced-go",[249,254,259,264,269,274,278,283,288],{"title":250,"path":251,"stem":252,"icon":253},"Advanced Go Roadmap","\u002Fadvanced-go\u002Fadvanced-roadmap","6.advanced-go\u002F1.advanced-roadmap","i-lucide-brain-circuit",{"title":255,"path":256,"stem":257,"icon":258},"Reflection","\u002Fadvanced-go\u002Freflection","6.advanced-go\u002F2.reflection","i-lucide-scan-search",{"title":260,"path":261,"stem":262,"icon":263},"Memory and Garbage Collection","\u002Fadvanced-go\u002Fmemory-and-garbage-collection","6.advanced-go\u002F3.memory-and-garbage-collection","i-lucide-memory-stick",{"title":265,"path":266,"stem":267,"icon":268},"Benchmarking and Profiling","\u002Fadvanced-go\u002Fbenchmarking-and-profiling","6.advanced-go\u002F4.benchmarking-and-profiling","i-lucide-gauge",{"title":270,"path":271,"stem":272,"icon":273},"Advanced Testing and Fuzzing","\u002Fadvanced-go\u002Fadvanced-testing-and-fuzzing","6.advanced-go\u002F5.advanced-testing-and-fuzzing","i-lucide-microscope",{"title":275,"path":276,"stem":277,"icon":48},"Advanced Concurrency","\u002Fadvanced-go\u002Fadvanced-concurrency","6.advanced-go\u002F6.advanced-concurrency",{"title":279,"path":280,"stem":281,"icon":282},"Unsafe and C Boundaries","\u002Fadvanced-go\u002Funsafe-and-cgo","6.advanced-go\u002F7.unsafe-and-cgo","i-lucide-radiation",{"title":284,"path":285,"stem":286,"icon":287},"Package and API Design","\u002Fadvanced-go\u002Fpackage-and-api-design","6.advanced-go\u002F8.package-and-api-design","i-lucide-boxes",{"title":289,"path":290,"stem":291,"icon":292},"Distributed Reliability","\u002Fadvanced-go\u002Fdistributed-reliability","6.advanced-go\u002F9.distributed-reliability","i-lucide-cloud-cog",{"title":294,"path":295,"stem":296,"children":297,"page":6},"Projects","\u002Fprojects","7.projects",[298,316,332,347],{"title":299,"path":300,"stem":301,"children":302,"icon":305},"CLI Task Manager","\u002Fprojects\u002Fcli-task-manager","7.projects\u002F1.cli-task-manager\u002Findex",[303,306,311],{"title":304,"path":300,"stem":301,"icon":305},"Project — CLI Task Manager","i-lucide-list-todo",{"title":307,"path":308,"stem":309,"icon":310},"CLI — Domain and Storage","\u002Fprojects\u002Fcli-task-manager\u002Fdomain-and-storage","7.projects\u002F1.cli-task-manager\u002F1.domain-and-storage","i-lucide-save",{"title":312,"path":313,"stem":314,"icon":315},"CLI — Commands and Tests","\u002Fprojects\u002Fcli-task-manager\u002Fcommands-and-tests","7.projects\u002F1.cli-task-manager\u002F2.commands-and-tests","i-lucide-terminal",{"title":317,"path":318,"stem":319,"children":320,"icon":323},"Concurrent URL Checker","\u002Fprojects\u002Fconcurrent-url-checker","7.projects\u002F2.concurrent-url-checker\u002Findex",[321,324,328],{"title":322,"path":318,"stem":319,"icon":323},"Project — Concurrent URL Checker","i-lucide-radar",{"title":325,"path":326,"stem":327,"icon":128},"URL Checker — Worker Pipeline","\u002Fprojects\u002Fconcurrent-url-checker\u002Fworker-pipeline","7.projects\u002F2.concurrent-url-checker\u002F1.worker-pipeline",{"title":329,"path":330,"stem":331,"icon":173},"URL Checker — Cancellation and Tests","\u002Fprojects\u002Fconcurrent-url-checker\u002Fcancellation-and-tests","7.projects\u002F2.concurrent-url-checker\u002F2.cancellation-and-tests",{"title":333,"path":334,"stem":335,"children":336},"File-backed API","\u002Fprojects\u002Ffile-backed-api","7.projects\u002F3.file-backed-api\u002Findex",[337,339,343],{"title":338,"path":334,"stem":335},"File-backed notes API",{"title":340,"path":341,"stem":342},"File store and concurrency","\u002Fprojects\u002Ffile-backed-api\u002Ffile-store-and-concurrency","7.projects\u002F3.file-backed-api\u002F1.file-store-and-concurrency",{"title":344,"path":345,"stem":346},"HTTP layer and tests","\u002Fprojects\u002Ffile-backed-api\u002Fhttp-and-tests","7.projects\u002F3.file-backed-api\u002F2.http-and-tests",{"title":348,"path":349,"stem":350,"children":351},"Production REST API","\u002Fprojects\u002Fproduction-rest-api","7.projects\u002F4.production-rest-api\u002Findex",[352,354,358],{"title":353,"path":349,"stem":350},"Production REST API capstone",{"title":355,"path":356,"stem":357},"Persistence and authentication","\u002Fprojects\u002Fproduction-rest-api\u002Fpersistence-and-auth","7.projects\u002F4.production-rest-api\u002F1.persistence-and-auth",{"title":359,"path":360,"stem":361},"Operations and release","\u002Fprojects\u002Fproduction-rest-api\u002Foperations-and-release","7.projects\u002F4.production-rest-api\u002F2.operations-and-release",{"title":363,"path":364,"stem":365,"children":366,"page":6},"Meta","\u002Fmeta","99.meta",[367,372],{"title":368,"path":369,"stem":370,"icon":371},"About","\u002Fmeta\u002Fabout","99.meta\u002F1.about","i-lucide-info",{"title":373,"path":374,"stem":375,"icon":376},"Acknowledgements","\u002Fmeta\u002Facknowledgements","99.meta\u002F2.acknowledgements","i-lucide-heart-handshake",{"id":378,"title":260,"body":379,"description":1417,"extension":1418,"links":1419,"meta":1420,"navigation":1421,"path":261,"seo":1422,"stem":262,"__hash__":1424},"docs\u002F6.advanced-go\u002F3.memory-and-garbage-collection.md",{"type":380,"value":381,"toc":1402},"minimark",[382,388,393,397,416,420,423,426,497,500,504,507,510,513,517,520,543,546,549,553,556,559,563,566,620,626,629,742,745,749,752,755,762,767,770,774,781,784,788,831,835,838,1361,1365,1382,1392,1395,1398],[383,384],"learning-lesson-meta",{"level":385,"prerequisites":386,"time":387},"Advanced","Pointers, slices, interfaces, concurrency, benchmarks, and profiling basics","65–80 minutes",[389,390,392],"h2",{"id":391},"what-you-will-learn","What you will learn",[394,395,396],"p",{},"By the end of this lesson, you will be able to:",[398,399,400,404,407,410,413],"ul",{},[401,402,403],"li",{},"distinguish language semantics from compiler allocation decisions;",[401,405,406],{},"explain reachability-based garbage collection;",[401,408,409],{},"inspect escape-analysis diagnostics without treating them as permanent guarantees;",[401,411,412],{},"find common sources of accidental retention;",[401,414,415],{},"decide when reducing allocations is worth the complexity.",[389,417,419],{"id":418},"values-first-storage-second","Values first, storage second",[394,421,422],{},"Write correct code using Go's value, pointer, and ownership semantics. The compiler decides whether storage can remain associated with a goroutine stack or must be allocated on the heap.",[394,424,425],{},"Returning a pointer to a local value is safe:",[427,428,433],"pre",{"className":429,"code":430,"language":431,"meta":432,"style":432},"language-go shiki shiki-themes solarized-light material-theme one-dark-pro","func newCounter() *int {\n    value := 0\n    return &value\n}\n","go","",[434,435,436,463,478,491],"code",{"__ignoreMap":432},[437,438,441,445,449,453,456,460],"span",{"class":439,"line":440},"line",1,[437,442,444],{"class":443},"sEOdo","func",[437,446,448],{"class":447},"sisdC"," newCounter",[437,450,452],{"class":451},"shFME","()",[437,454,455],{"class":443}," *",[437,457,459],{"class":458},"sF6sA","int",[437,461,462],{"class":451}," {\n",[437,464,466,470,474],{"class":439,"line":465},2,[437,467,469],{"class":468},"s45-6","    value",[437,471,473],{"class":472},"su2KV"," :=",[437,475,477],{"class":476},"sAJzr"," 0\n",[437,479,481,485,488],{"class":439,"line":480},3,[437,482,484],{"class":483},"s7Bcg","    return",[437,486,487],{"class":443}," &",[437,489,490],{"class":468},"value\n",[437,492,494],{"class":439,"line":493},4,[437,495,496],{"class":451},"}\n",[394,498,499],{},"The compiler ensures the value remains valid. Do not rewrite correct code merely because “locals belong on the stack.”",[389,501,503],{"id":502},"reachability","Reachability",[394,505,506],{},"The garbage collector can reclaim heap objects that are no longer reachable from live program roots. It does not know whether a reachable object is still logically useful.",[394,508,509],{},"If a global cache, goroutine, map, or slice keeps a reference, the object remains reachable.",[394,511,512],{},"Garbage collection handles memory, not every resource. Files, response bodies, database rows, locks, and external leases require explicit lifecycle handling.",[389,514,516],{"id":515},"escape-analysis","Escape analysis",[394,518,519],{},"The compiler analyses whether values must escape their current scope. Inspect diagnostics with a compiler flag in a real Go environment:",[427,521,525],{"className":522,"code":523,"language":524,"meta":432,"style":432},"language-bash shiki shiki-themes solarized-light material-theme one-dark-pro","go build -gcflags=-m=3 .\u002F...\n","bash",[434,526,527],{"__ignoreMap":432},[437,528,529,532,536,540],{"class":439,"line":440},[437,530,431],{"class":531},"sqRHY",[437,533,535],{"class":534},"sVazx"," build",[437,537,539],{"class":538},"s2SA0"," -gcflags=-m=3",[437,541,542],{"class":534}," .\u002F...\n",[394,544,545],{},"The output explains inlining and escape decisions. It can be noisy. Use it after a profile identifies allocation pressure in relevant code.",[394,547,548],{},"Escape decisions are compiler implementation details and may change with code shape, target, and Go version. Correctness must not depend on them.",[389,550,552],{"id":551},"interfaces-and-allocations","Interfaces and allocations",[394,554,555],{},"Converting a value to an interface does not guarantee a heap allocation, nor does it guarantee the absence of one. The compiler considers how the interface value is used.",[394,557,558],{},"Measure the actual operation with benchmarks and allocation reporting rather than using a universal rule.",[389,560,562],{"id":561},"slice-retention","Slice retention",[394,564,565],{},"A small subslice can keep a much larger backing array reachable:",[427,567,569],{"className":429,"code":568,"language":431,"meta":432,"style":432},"func prefix(data []byte) []byte {\n    return data[:16]\n}\n",[434,570,571,600,616],{"__ignoreMap":432},[437,572,573,575,578,581,585,588,591,594,596,598],{"class":439,"line":440},[437,574,444],{"class":443},[437,576,577],{"class":447}," prefix",[437,579,580],{"class":451},"(",[437,582,584],{"class":583},"syre3","data",[437,586,587],{"class":451}," []",[437,589,590],{"class":458},"byte",[437,592,593],{"class":451},")",[437,595,587],{"class":451},[437,597,590],{"class":458},[437,599,462],{"class":451},[437,601,602,604,607,610,613],{"class":439,"line":465},[437,603,484],{"class":483},[437,605,606],{"class":468}," data",[437,608,609],{"class":451},"[:",[437,611,612],{"class":476},"16",[437,614,615],{"class":451},"]\n",[437,617,618],{"class":439,"line":480},[437,619,496],{"class":451},[394,621,622,623,625],{},"If ",[434,624,584],{}," is several megabytes and the returned prefix lives for a long time, the entire backing array may remain reachable.",[394,627,628],{},"Copy when the lifetime difference matters:",[427,630,632],{"className":429,"code":631,"language":431,"meta":432,"style":432},"func copiedPrefix(data []byte) []byte {\n    length := min(16, len(data))\n    result := make([]byte, length)\n    copy(result, data[:length])\n    return result\n}\n",[434,633,634,657,684,707,729,737],{"__ignoreMap":432},[437,635,636,638,641,643,645,647,649,651,653,655],{"class":439,"line":440},[437,637,444],{"class":443},[437,639,640],{"class":447}," copiedPrefix",[437,642,580],{"class":451},[437,644,584],{"class":583},[437,646,587],{"class":451},[437,648,590],{"class":458},[437,650,593],{"class":451},[437,652,587],{"class":451},[437,654,590],{"class":458},[437,656,462],{"class":451},[437,658,659,662,664,667,669,671,674,677,679,681],{"class":439,"line":465},[437,660,661],{"class":468},"    length",[437,663,473],{"class":472},[437,665,666],{"class":447}," min",[437,668,580],{"class":451},[437,670,612],{"class":476},[437,672,673],{"class":451},",",[437,675,676],{"class":447}," len",[437,678,580],{"class":451},[437,680,584],{"class":468},[437,682,683],{"class":451},"))\n",[437,685,686,689,691,694,697,699,701,704],{"class":439,"line":480},[437,687,688],{"class":468},"    result",[437,690,473],{"class":472},[437,692,693],{"class":447}," make",[437,695,696],{"class":451},"([]",[437,698,590],{"class":458},[437,700,673],{"class":451},[437,702,703],{"class":468}," length",[437,705,706],{"class":451},")\n",[437,708,709,712,714,717,719,721,723,726],{"class":439,"line":493},[437,710,711],{"class":447},"    copy",[437,713,580],{"class":451},[437,715,716],{"class":468},"result",[437,718,673],{"class":451},[437,720,606],{"class":468},[437,722,609],{"class":451},[437,724,725],{"class":468},"length",[437,727,728],{"class":451},"])\n",[437,730,732,734],{"class":439,"line":731},5,[437,733,484],{"class":483},[437,735,736],{"class":468}," result\n",[437,738,740],{"class":439,"line":739},6,[437,741,496],{"class":451},[394,743,744],{},"The copy costs CPU and a small allocation but can release a large backing array sooner. Measure the real trade-off.",[389,746,748],{"id":747},"goroutine-retention","Goroutine retention",[394,750,751],{},"A blocked goroutine keeps its stack and referenced values alive. Goroutine leaks are therefore also memory-retention problems.",[394,753,754],{},"Every goroutine needs a clear completion or cancellation path. Monitor goroutine counts and capture profiles when growth is unexplained.",[389,756,758,759],{"id":757},"reuse-and-syncpool","Reuse and ",[434,760,761],{},"sync.Pool",[394,763,764,766],{},[434,765,761],{}," holds temporary objects that may be removed at any time, especially around garbage collection. It is not a durable cache and provides no guarantee that a stored item will be returned.",[394,768,769],{},"It can reduce pressure for frequently allocated temporary objects shared across goroutines, after profiling proves the benefit. Pooled objects must be reset so data does not leak between uses.",[389,771,773],{"id":772},"garbage-collector-controls","Garbage collector controls",[394,775,776,777,780],{},"The runtime exposes controls such as ",[434,778,779],{},"GOGC"," and a soft memory limit. Their exact behaviour and tuning guidance belong to runtime documentation and can evolve.",[394,782,783],{},"First reduce accidental retention, bound caches and queues, and profile allocation hot spots. Tuning cannot fix unbounded ownership.",[389,785,787],{"id":786},"common-mistakes","Common mistakes",[398,789,790,797,803,809,815,821],{},[401,791,792,796],{},[793,794,795],"strong",{},"Equating pointers with heap allocation:"," escape depends on use, not syntax alone.",[401,798,799,802],{},[793,800,801],{},"Calling every retained object a GC bug:"," reachable references usually explain retention.",[401,804,805,808],{},[793,806,807],{},"Forcing zero allocations everywhere:"," complexity grows while irrelevant code becomes harder to maintain.",[401,810,811,814],{},[793,812,813],{},"Pooling objects containing secrets without complete reset:"," data can cross requests.",[401,816,817,820],{},[793,818,819],{},"Using finalizers for ordinary cleanup:"," timing is nondeterministic; explicit close\u002Frelease remains necessary.",[401,822,823,830],{},[793,824,825,826,829],{},"Optimising from ",[434,827,828],{},"-gcflags"," output alone:"," compiler diagnostics do not show user impact.",[389,832,834],{"id":833},"practice","Practice",[394,836,837],{},"Write a function that reads a large byte slice and returns its first line. Create one version that returns a subslice and one that copies. Explain the lifetime trade-off, then design a benchmark that reports time, bytes, and allocations without claiming which is better before measurement.",[839,840,841],"accordion",{},[842,843,846,1358],"accordion-item",{"icon":844,"label":845},"i-lucide-lightbulb","Show solution",[427,847,849],{"className":429,"code":848,"language":431,"meta":432,"style":432},"func FirstLineView(data []byte) []byte {\n    if index := bytes.IndexByte(data, '\\n'); index >= 0 {\n        return data[:index]\n    }\n    return data\n}\n\nfunc FirstLineCopy(data []byte) []byte {\n    line := FirstLineView(data)\n    result := make([]byte, len(line))\n    copy(result, line)\n    return result\n}\n\nfunc BenchmarkFirstLineView(b *testing.B) {\n    data := append(bytes.Repeat([]byte(\"x\"), 1\u003C\u003C20), '\\n')\n    b.ReportAllocs()\n    for b.Loop() {\n        result := FirstLineView(data)\n        runtime.KeepAlive(result)\n    }\n}\n\nfunc BenchmarkFirstLineCopy(b *testing.B) {\n    data := append(bytes.Repeat([]byte(\"x\"), 1\u003C\u003C20), '\\n')\n    b.ReportAllocs()\n    for b.Loop() {\n        result := FirstLineCopy(data)\n        runtime.KeepAlive(result)\n    }\n}\n",[434,850,851,874,923,937,942,949,953,960,984,1000,1023,1039,1046,1051,1056,1084,1141,1155,1173,1189,1206,1211,1216,1221,1245,1292,1303,1318,1333,1348,1353],{"__ignoreMap":432},[437,852,853,855,858,860,862,864,866,868,870,872],{"class":439,"line":440},[437,854,444],{"class":443},[437,856,857],{"class":447}," FirstLineView",[437,859,580],{"class":451},[437,861,584],{"class":583},[437,863,587],{"class":451},[437,865,590],{"class":458},[437,867,593],{"class":451},[437,869,587],{"class":451},[437,871,590],{"class":458},[437,873,462],{"class":451},[437,875,876,879,882,884,887,890,893,895,897,899,903,906,909,912,914,918,921],{"class":439,"line":465},[437,877,878],{"class":483},"    if",[437,880,881],{"class":468}," index",[437,883,473],{"class":472},[437,885,886],{"class":468}," bytes",[437,888,889],{"class":451},".",[437,891,892],{"class":447},"IndexByte",[437,894,580],{"class":451},[437,896,584],{"class":468},[437,898,673],{"class":451},[437,900,902],{"class":901},"sED5a"," '",[437,904,905],{"class":538},"\\n",[437,907,908],{"class":901},"'",[437,910,911],{"class":451},");",[437,913,881],{"class":468},[437,915,917],{"class":916},"sRBoR"," >=",[437,919,920],{"class":476}," 0",[437,922,462],{"class":451},[437,924,925,928,930,932,935],{"class":439,"line":480},[437,926,927],{"class":483},"        return",[437,929,606],{"class":468},[437,931,609],{"class":451},[437,933,934],{"class":468},"index",[437,936,615],{"class":451},[437,938,939],{"class":439,"line":493},[437,940,941],{"class":451},"    }\n",[437,943,944,946],{"class":439,"line":731},[437,945,484],{"class":483},[437,947,948],{"class":468}," data\n",[437,950,951],{"class":439,"line":739},[437,952,496],{"class":451},[437,954,956],{"class":439,"line":955},7,[437,957,959],{"emptyLinePlaceholder":958},true,"\n",[437,961,963,965,968,970,972,974,976,978,980,982],{"class":439,"line":962},8,[437,964,444],{"class":443},[437,966,967],{"class":447}," FirstLineCopy",[437,969,580],{"class":451},[437,971,584],{"class":583},[437,973,587],{"class":451},[437,975,590],{"class":458},[437,977,593],{"class":451},[437,979,587],{"class":451},[437,981,590],{"class":458},[437,983,462],{"class":451},[437,985,987,990,992,994,996,998],{"class":439,"line":986},9,[437,988,989],{"class":468},"    line",[437,991,473],{"class":472},[437,993,857],{"class":447},[437,995,580],{"class":451},[437,997,584],{"class":468},[437,999,706],{"class":451},[437,1001,1003,1005,1007,1009,1011,1013,1015,1017,1019,1021],{"class":439,"line":1002},10,[437,1004,688],{"class":468},[437,1006,473],{"class":472},[437,1008,693],{"class":447},[437,1010,696],{"class":451},[437,1012,590],{"class":458},[437,1014,673],{"class":451},[437,1016,676],{"class":447},[437,1018,580],{"class":451},[437,1020,439],{"class":468},[437,1022,683],{"class":451},[437,1024,1026,1028,1030,1032,1034,1037],{"class":439,"line":1025},11,[437,1027,711],{"class":447},[437,1029,580],{"class":451},[437,1031,716],{"class":468},[437,1033,673],{"class":451},[437,1035,1036],{"class":468}," line",[437,1038,706],{"class":451},[437,1040,1042,1044],{"class":439,"line":1041},12,[437,1043,484],{"class":483},[437,1045,736],{"class":468},[437,1047,1049],{"class":439,"line":1048},13,[437,1050,496],{"class":451},[437,1052,1054],{"class":439,"line":1053},14,[437,1055,959],{"emptyLinePlaceholder":958},[437,1057,1059,1061,1064,1066,1069,1071,1075,1077,1080,1082],{"class":439,"line":1058},15,[437,1060,444],{"class":443},[437,1062,1063],{"class":447}," BenchmarkFirstLineView",[437,1065,580],{"class":451},[437,1067,1068],{"class":583},"b",[437,1070,455],{"class":443},[437,1072,1074],{"class":1073},"snwKR","testing",[437,1076,889],{"class":451},[437,1078,1079],{"class":1073},"B",[437,1081,593],{"class":451},[437,1083,462],{"class":451},[437,1085,1087,1090,1092,1095,1097,1100,1102,1105,1107,1109,1111,1114,1117,1119,1122,1125,1128,1131,1133,1135,1137,1139],{"class":439,"line":1086},16,[437,1088,1089],{"class":468},"    data",[437,1091,473],{"class":472},[437,1093,1094],{"class":447}," append",[437,1096,580],{"class":451},[437,1098,1099],{"class":468},"bytes",[437,1101,889],{"class":451},[437,1103,1104],{"class":447},"Repeat",[437,1106,696],{"class":451},[437,1108,590],{"class":458},[437,1110,580],{"class":451},[437,1112,1113],{"class":901},"\"",[437,1115,1116],{"class":534},"x",[437,1118,1113],{"class":901},[437,1120,1121],{"class":451},"),",[437,1123,1124],{"class":476}," 1",[437,1126,1127],{"class":916},"\u003C\u003C",[437,1129,1130],{"class":476},"20",[437,1132,1121],{"class":451},[437,1134,902],{"class":901},[437,1136,905],{"class":538},[437,1138,908],{"class":901},[437,1140,706],{"class":451},[437,1142,1144,1147,1149,1152],{"class":439,"line":1143},17,[437,1145,1146],{"class":468},"    b",[437,1148,889],{"class":451},[437,1150,1151],{"class":447},"ReportAllocs",[437,1153,1154],{"class":451},"()\n",[437,1156,1158,1161,1164,1166,1169,1171],{"class":439,"line":1157},18,[437,1159,1160],{"class":483},"    for",[437,1162,1163],{"class":468}," b",[437,1165,889],{"class":451},[437,1167,1168],{"class":447},"Loop",[437,1170,452],{"class":451},[437,1172,462],{"class":451},[437,1174,1176,1179,1181,1183,1185,1187],{"class":439,"line":1175},19,[437,1177,1178],{"class":468},"        result",[437,1180,473],{"class":472},[437,1182,857],{"class":447},[437,1184,580],{"class":451},[437,1186,584],{"class":468},[437,1188,706],{"class":451},[437,1190,1192,1195,1197,1200,1202,1204],{"class":439,"line":1191},20,[437,1193,1194],{"class":468},"        runtime",[437,1196,889],{"class":451},[437,1198,1199],{"class":447},"KeepAlive",[437,1201,580],{"class":451},[437,1203,716],{"class":468},[437,1205,706],{"class":451},[437,1207,1209],{"class":439,"line":1208},21,[437,1210,941],{"class":451},[437,1212,1214],{"class":439,"line":1213},22,[437,1215,496],{"class":451},[437,1217,1219],{"class":439,"line":1218},23,[437,1220,959],{"emptyLinePlaceholder":958},[437,1222,1224,1226,1229,1231,1233,1235,1237,1239,1241,1243],{"class":439,"line":1223},24,[437,1225,444],{"class":443},[437,1227,1228],{"class":447}," BenchmarkFirstLineCopy",[437,1230,580],{"class":451},[437,1232,1068],{"class":583},[437,1234,455],{"class":443},[437,1236,1074],{"class":1073},[437,1238,889],{"class":451},[437,1240,1079],{"class":1073},[437,1242,593],{"class":451},[437,1244,462],{"class":451},[437,1246,1248,1250,1252,1254,1256,1258,1260,1262,1264,1266,1268,1270,1272,1274,1276,1278,1280,1282,1284,1286,1288,1290],{"class":439,"line":1247},25,[437,1249,1089],{"class":468},[437,1251,473],{"class":472},[437,1253,1094],{"class":447},[437,1255,580],{"class":451},[437,1257,1099],{"class":468},[437,1259,889],{"class":451},[437,1261,1104],{"class":447},[437,1263,696],{"class":451},[437,1265,590],{"class":458},[437,1267,580],{"class":451},[437,1269,1113],{"class":901},[437,1271,1116],{"class":534},[437,1273,1113],{"class":901},[437,1275,1121],{"class":451},[437,1277,1124],{"class":476},[437,1279,1127],{"class":916},[437,1281,1130],{"class":476},[437,1283,1121],{"class":451},[437,1285,902],{"class":901},[437,1287,905],{"class":538},[437,1289,908],{"class":901},[437,1291,706],{"class":451},[437,1293,1295,1297,1299,1301],{"class":439,"line":1294},26,[437,1296,1146],{"class":468},[437,1298,889],{"class":451},[437,1300,1151],{"class":447},[437,1302,1154],{"class":451},[437,1304,1306,1308,1310,1312,1314,1316],{"class":439,"line":1305},27,[437,1307,1160],{"class":483},[437,1309,1163],{"class":468},[437,1311,889],{"class":451},[437,1313,1168],{"class":447},[437,1315,452],{"class":451},[437,1317,462],{"class":451},[437,1319,1321,1323,1325,1327,1329,1331],{"class":439,"line":1320},28,[437,1322,1178],{"class":468},[437,1324,473],{"class":472},[437,1326,967],{"class":447},[437,1328,580],{"class":451},[437,1330,584],{"class":468},[437,1332,706],{"class":451},[437,1334,1336,1338,1340,1342,1344,1346],{"class":439,"line":1335},29,[437,1337,1194],{"class":468},[437,1339,889],{"class":451},[437,1341,1199],{"class":447},[437,1343,580],{"class":451},[437,1345,716],{"class":468},[437,1347,706],{"class":451},[437,1349,1351],{"class":439,"line":1350},30,[437,1352,941],{"class":451},[437,1354,1356],{"class":439,"line":1355},31,[437,1357,496],{"class":451},[394,1359,1360],{},"The view avoids copying but shares the input's lifetime. The copy has independent storage. A microbenchmark measures operation cost, but a retention experiment or heap profile is needed to evaluate the long-lived memory effect in a representative workflow.",[389,1362,1364],{"id":1363},"summary","Summary",[398,1366,1367,1370,1373,1376,1379],{},[401,1368,1369],{},"Compiler storage decisions are implementation details; write correct value semantics first.",[401,1371,1372],{},"The collector reclaims unreachable memory, not reachable data that your program forgot to release logically.",[401,1374,1375],{},"Escape diagnostics explain current compiler decisions but do not replace profiles.",[401,1377,1378],{},"Subslice and goroutine lifetimes can retain much larger object graphs.",[401,1380,1381],{},"Reduce allocations only when evidence shows a meaningful result.",[394,1383,1384,1385,889],{},"Official reference: ",[1386,1387,1391],"a",{"href":1388,"rel":1389},"https:\u002F\u002Fgo.dev\u002Fdoc\u002Fgc-guide",[1390],"nofollow","A Guide to the Go Garbage Collector",[1393,1394],"learning-lesson-complete",{},[1396,1397],"learning-lesson-navigation",{"next":266,"next-label":265,"previous":256,"previous-label":255},[1399,1400,1401],"style",{},"html pre.shiki code .sEOdo, html code.shiki .sEOdo{--shiki-light:#859900;--shiki-default:#89DDFF;--shiki-dark:#C678DD}html pre.shiki code .sisdC, html code.shiki .sisdC{--shiki-light:#268BD2;--shiki-default:#82AAFF;--shiki-dark:#61AFEF}html pre.shiki code .shFME, html code.shiki .shFME{--shiki-light:#657B83;--shiki-default:#89DDFF;--shiki-dark:#ABB2BF}html pre.shiki code .sF6sA, html code.shiki .sF6sA{--shiki-light:#586E75;--shiki-light-font-weight:bold;--shiki-default:#C792EA;--shiki-default-font-weight:inherit;--shiki-dark:#C678DD;--shiki-dark-font-weight:inherit}html pre.shiki code .s45-6, html code.shiki .s45-6{--shiki-light:#268BD2;--shiki-default:#EEFFFF;--shiki-dark:#E06C75}html pre.shiki code .su2KV, html code.shiki .su2KV{--shiki-light:#859900;--shiki-default:#89DDFF;--shiki-dark:#E5C07B}html pre.shiki code .sAJzr, html code.shiki .sAJzr{--shiki-light:#D33682;--shiki-default:#F78C6C;--shiki-dark:#D19A66}html pre.shiki code .s7Bcg, html code.shiki .s7Bcg{--shiki-light:#859900;--shiki-light-font-style:inherit;--shiki-default:#89DDFF;--shiki-default-font-style:italic;--shiki-dark:#C678DD;--shiki-dark-font-style:inherit}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sqRHY, html code.shiki .sqRHY{--shiki-light:#268BD2;--shiki-default:#FFCB6B;--shiki-dark:#61AFEF}html pre.shiki code .sVazx, html code.shiki .sVazx{--shiki-light:#2AA198;--shiki-default:#C3E88D;--shiki-dark:#98C379}html pre.shiki code .s2SA0, html code.shiki .s2SA0{--shiki-light:#CB4B16;--shiki-default:#C3E88D;--shiki-dark:#D19A66}html pre.shiki code .syre3, html code.shiki .syre3{--shiki-light:#657B83;--shiki-light-font-style:inherit;--shiki-default:#EEFFFF;--shiki-default-font-style:italic;--shiki-dark:#E06C75;--shiki-dark-font-style:italic}html pre.shiki code .sED5a, html code.shiki .sED5a{--shiki-light:#2AA198;--shiki-default:#89DDFF;--shiki-dark:#98C379}html pre.shiki code .sRBoR, html code.shiki .sRBoR{--shiki-light:#859900;--shiki-default:#89DDFF;--shiki-dark:#56B6C2}html pre.shiki code .snwKR, html code.shiki .snwKR{--shiki-light:#CB4B16;--shiki-default:#FFCB6B;--shiki-dark:#E5C07B}",{"title":432,"searchDepth":465,"depth":465,"links":1403},[1404,1405,1406,1407,1408,1409,1410,1411,1413,1414,1415,1416],{"id":391,"depth":465,"text":392},{"id":418,"depth":465,"text":419},{"id":502,"depth":465,"text":503},{"id":515,"depth":465,"text":516},{"id":551,"depth":465,"text":552},{"id":561,"depth":465,"text":562},{"id":747,"depth":465,"text":748},{"id":757,"depth":465,"text":1412},"Reuse and sync.Pool",{"id":772,"depth":465,"text":773},{"id":786,"depth":465,"text":787},{"id":833,"depth":465,"text":834},{"id":1363,"depth":465,"text":1364},"Reason about reachability, escape analysis, allocation, retention, and resource cleanup without relying on myths.","md",null,{},{"icon":263},{"description":1423,"title":260},"Learn Go memory semantics, garbage collection, stack and heap allocation, escape analysis, retention, pooling, and measurement.","vTMF3UYhHHdplGmCdYQW940tSnrCZw_f8z0KRFiBZjs",[1426,1428],{"title":255,"path":256,"stem":257,"description":1427,"icon":258,"children":-1},"Inspect types and values at runtime while containing panics, complexity, and lost type safety.",{"title":265,"path":266,"stem":267,"description":1429,"icon":268,"children":-1},"Measure representative performance, locate bottlenecks, compare changes, and avoid misleading microbenchmarks.",1794118103046]