Go Language Memory management (i) Memory allocation

Source: Internet
Author: User

Go Language Memory management (i) Memory allocation

Golang, as a "high-level language," also provides its own memory management mechanism. This simplifies the coding process and reduces the frequency of problems caused by memory usage (C-language users, especially beginners, should be impressed), and is friendly to the program ape. On the other hand, it can reduce memory-related system calls and improve performance.

First understand the following memory management approximate strategy:

    • Request a larger address space (virtual memory) for memory allocation and management (GOLANG:SPANS+BITMAP+ARENA->512M+16G+512G)
    • When space is low, request a larger memory, such as 100KB or 1MB, to the system.
    • The requested memory block is divided into a number of small chunks of memory (golang:_numsizeclasses = 67) and managed with a linked list, by a specific size.
    • When creating an object, find the most appropriate block of memory from the idle list by object size
    • When the object is destroyed, the corresponding memory block is returned to the idle list for reuse
    • Returns the operating system when free memory reaches the threshold

The following, based on the go1.9 version, see the basic idea of golang memory allocation implementation.

Implementation of GO memory management

The memory management implementation of Go is based on Tcmalloc (Thread-caching Malloc).

The Tcmalloc is a multi-level memory allocator developed by Google, with features that combat memory fragmentation and are suitable for high concurrency scenarios. It is said that its memory allocation speed is several times the number of malloc implemented in glibc2.3.

In the same way as Tcmalloc, Go's memory allocation is based on two granular memory units: Span and object. span is a continuous page that is categorized by the number of page, such as a span of span,4 page divided into 2 page. object is a preset size block in span and is also categorized by size. In the same span, there is only one type (size) of object.

Go memory allocation consists of three management components:

    • Mcache

Per-p (processer, see the concept of Go g,m,p) private cache for lock-free object assignment

    • Mcentral

Global memory, providing a size-divided span for each cache

    • Mheap

Global memory, page management, request to system when memory is low

By dividing the memory allocation process into three tiers, the processer level (Mcache) is guaranteed to be free of lock allocation, and memory is shared globally at the mcentral level to avoid waste.

Go divides the memory request into three types by size: Tiny,small,large. Tiny is less than 16 bytes of application, small is less than 32KB of the application, greater than 32KB for large, three types of processing differ.

_TinySize      16_MaxSmallSize   32768

Let's take a small object as an example and look at the memory application process:

    1. Calculate the size of the object, and find the corresponding specification Mspan from the private mcache, according to the predefined Sizeclass table (see below). For example, the size of the object of the 8192 byte, corresponding to the size of byte Mspan. An idle block is then found through the Mspan idle bitmap, and if the free block exists, the allocation is complete.

The above is the allocation operation within the Mcache, no need to lock.

    1. If the Mspan does not have free blocks, the mcentral is requested for the corresponding size of the idle Mspan. For example, the object of the mcentral byte, need to apply to the 8192 byte size of the idle Mspan.

Because the request obtains the global Mspan, it needs to be locked at the mcentral level.

    1. If there is no idle Mspan in the mcentral, apply to Mheap and divide the object.

    2. If the mheap does not have enough free page, apply to the operating system not less than 1M page.

The above is the memory allocation process for the small object.

Large the application of the object, skips the Mcache and mcentral, and distributes it directly from the mheap.

For tiny object applications, there is a dedicated memory area "tiny" for special processing in Mcache. "Tiny" aligns the object by size with Tinyoffset ("Tiny" currently assigned address), assigns it, and records the new Tinyoffset for the next assignment. If there is not enough space, an additional memory block of byte is requested.

sizeclass//class Bytes/obj Bytes/span objects Waste bytes//1 8 8192 1024 0//        2 16 8192 512 0//3 32 8192 256 0//4 48      8192 170 32//5 64 8192 128 0//6 80 8192 102 32//7 96 8192 85 32//8 112 8192 73 1        6//9 128 8192 64 0//10 144 8192 56 128//11        160 8192 51 32//12 176 8192 46 96//13 192 8192          42 128//14 208 8192 39 80//15 224 8192 36        128//16 240 8192 34 32//17 256 8192 32 0//18   288 8192    28 128//19 320 8192 25 192//20 352 8192 23        96//21 384 8192 21 128//22 416 8192 19 288//23 448 8192 18 128//24 480 8192 17 32//25 512 81          92 16 0//26 576 8192 14 128//27 640 8192 12         512//28 704 8192 11 448//29 768 8192 10 512//30        896 8192 9 128//31 1024 8192 8 0//32 1152           8192 7 128//33 1280 8192 6 512//34 1408 16384 11    896//35 1536 8192 5 512//36 1792 16384 9 256//        37 20488192 4 0//38 2304 16384 7 256//39 2688 8192 3    128//40 3072 24576 8 0//41 3200 16384 5 384//       42 3456 24576 7 384//43 4096 8192 2 0//44 4864        24576 5 256//45 5376 16384 3 256//46 6144 24576 4 0//47 6528 32768 5 128//48 6784 40960 6 256/ /49 6912 49152 7 768//50 8192 8192 1 0//51 94        72 57344 6 512//52 9728 49152 5 512//53 10240 40960            4 0//54 10880 32768 3 128//55 12288 24576 2 0//56 13568      40960 3 256//57 14336 57344 4 0//58 16384 16384 1 0//59 18432 73728 4 0//60 19072 57344 3 12 8//61 20480 40960 2 0//62 21760 65536 3 256//63 2         4576 24576 1 0//64 27264 81920 3 128//65 28672 57344 2 0//66 32768 32768 1 0

Reference documents:

Tcmalloc

Graphic Tcmalloc

Go Language Memory management (i) Memory allocation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.