How to release map memory in golang?
Golang's method of releasing map memory: first delete all keys in the map, and the memory occupied by the map is still in [used state]; then map is set to nil, and the memory occupied by map is in [idle state]; finally it is idle State memory can be reused in the next application within a certain period of time without having to apply to the operating system again.
Golang’s method of releasing map memory:
Sample code:
go version: 1.12.9
package main import ( "log" "runtime" ) var intMap map[int]int var cnt = 8192 func main() { printMemStats() initMap() runtime.GC() printMemStats() log.Println(len(intMap)) for i := 0; i < cnt; i++ { delete(intMap, i) } log.Println(len(intMap)) runtime.GC() printMemStats() intMap = nil runtime.GC() printMemStats() } func initMap() { intMap = make(map[int]int, cnt) for i := 0; i < cnt; i++ { intMap[i] = i } } func printMemStats() { var m runtime.MemStats runtime.ReadMemStats(&m) log.Printf("Alloc = %v TotalAlloc = %v Sys = %v NumGC = %v\n", m.Alloc/1024, m.TotalAlloc/1024, m.Sys/1024, m.NumGC) }
Output result:
2019/11/22 15:42:33 Alloc = 138 TotalAlloc = 138 Sys = 68290 NumGC = 0 2019/11/22 15:42:33 Alloc = 456 TotalAlloc = 460 Sys = 68610 NumGC = 1 2019/11/22 15:42:33 8192 2019/11/22 15:42:33 0 2019/11/22 15:42:33 Alloc = 458 TotalAlloc = 464 Sys = 68674 NumGC = 2 2019/11/22 15:42:33 Alloc = 146 TotalAlloc = 466 Sys = 68674 NumGC = 3
Field description:
Alloc
: The memory size occupied by the object on the current heap;TotalAlloc
: The total memory size allocated on the heap;#Sys
: The program operates from The total memory size requested by the system;NumGC
: The number of garbage collection runs.
#It can be seen from the running results that the memory occupied after the key in the map is deleted has not been released.
Conclusion:
Golang’s map will not release the memory immediately after the key is deleted, so as the program runs, the memory occupied by the map will actually only increase. The bigger. In addition, GC will access every element in the map during the marking phase, which will bring a very large overhead to program performance when the map is very large. However, after go version 1.5, if the key and value of the map do not contain pointers, the GC will ignore the map.
Related learning recommendations: Go language tutorial
The above is the detailed content of How to release map memory in golang?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



For mechanical hard drives or SATA solid-state drives, you will feel the increase in software running speed. If it is an NVME hard drive, you may not feel it. 1. Import the registry into the desktop and create a new text document, copy and paste the following content, save it as 1.reg, then right-click to merge and restart the computer. WindowsRegistryEditorVersion5.00[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SessionManager\MemoryManagement]"DisablePagingExecutive"=d

According to news from this website on September 3, Korean media etnews reported yesterday (local time) that Samsung Electronics and SK Hynix’s “HBM-like” stacked structure mobile memory products will be commercialized after 2026. Sources said that the two Korean memory giants regard stacked mobile memory as an important source of future revenue and plan to expand "HBM-like memory" to smartphones, tablets and laptops to provide power for end-side AI. According to previous reports on this site, Samsung Electronics’ product is called LPWide I/O memory, and SK Hynix calls this technology VFO. The two companies have used roughly the same technical route, which is to combine fan-out packaging and vertical channels. Samsung Electronics’ LPWide I/O memory has a bit width of 512

Reading and writing files safely in Go is crucial. Guidelines include: Checking file permissions Closing files using defer Validating file paths Using context timeouts Following these guidelines ensures the security of your data and the robustness of your application.

According to news from this site on June 7, GEIL launched its latest DDR5 solution at the 2024 Taipei International Computer Show, and provided SO-DIMM, CUDIMM, CSODIMM, CAMM2 and LPCAMM2 versions to choose from. ▲Picture source: Wccftech As shown in the picture, the CAMM2/LPCAMM2 memory exhibited by Jinbang adopts a very compact design, can provide a maximum capacity of 128GB, and a speed of up to 8533MT/s. Some of these products can even be stable on the AMDAM5 platform Overclocked to 9000MT/s without any auxiliary cooling. According to reports, Jinbang’s 2024 Polaris RGBDDR5 series memory can provide up to 8400

According to news from this website on July 23, the JEDEC Solid State Technology Association, the microelectronics standard setter, announced on the 22nd local time that the DDR5MRDIMM and LPDDR6CAMM memory technical specifications will be officially launched soon, and introduced the key details of these two memories. The "MR" in DDR5MRDIMM stands for MultiplexedRank, which means that the memory supports two or more Ranks and can combine and transmit multiple data signals on a single channel without additional physical The connection can effectively increase the bandwidth. JEDEC has planned multiple generations of DDR5MRDIMM memory, with the goal of eventually increasing its bandwidth to 12.8Gbps, compared with the current 6.4Gbps of DDR5RDIMM memory.

When the prices of ultra-high-frequency flagship memories such as 7600MT/s and 8000MT/s are generally high, Lexar has taken action. They have launched a new memory series called Ares Wings ARES RGB DDR5, with 7600 C36 and 8000 C38 is available in two specifications. The 16GB*2 sets are priced at 1,299 yuan and 1,499 yuan respectively, which is very cost-effective. This site has obtained the 8000 C38 version of Wings of War, and will bring you its unboxing pictures. The packaging of Lexar Wings ARES RGB DDR5 memory is well designed, using eye-catching black and red color schemes with colorful printing. There is an exclusive &quo in the upper left corner of the packaging.

According to news from this site on August 12, Korean media ETNews reported that Samsung Electronics has internally confirmed its investment plan to build a 1cnm DRAM memory production line at the Pyeongtaek P4 factory. The production line is targeted to be put into operation in June next year. Pyeongtaek P4 is a comprehensive semiconductor production center divided into four phases. In the earlier planning, the first phase was for NAND flash memory, the second phase was for logic foundry, and the third and fourth phases were for DRAM memory. Samsung has introduced DRAM production equipment in the first phase of P4, but has shelved the second phase of construction. 1cnm DRAM is the sixth-generation 20~10nm memory process, and each company's 1cnm (or corresponding 1γnm) products have not yet been officially released. Korean media reported that Samsung Electronics plans to start 1cnm memory production at the end of this year. ▲Samsung Pyeongtaek

Recently, a blogger revealed the parameters of the iPhone 17 series. This series will be equipped with LTPO screens as standard, and the memory will be upgraded to up to 12GB. The iPhone17 series will include four models: iPhone17, iPhone17Pro, iPhone17ProMax and iPhone17Slim, with screen sizes of 6.27 inches, 6.27 inches, 6.86 inches and 6.65 inches respectively. All models will be equipped with LTPO panels and support ProMotion variable refresh rates. This is the first time Apple has provided high refresh rate screens on standard models. In addition, the two Pro models iPhone17Pro and iPhone17ProMax will come standard with 1
