Home >Backend Development >Golang >Aliases and embedded memory usage

Aliases and embedded memory usage

PHPz
PHPzforward
2024-02-09 13:15:09697browse

Aliases and embedded memory usage

In this article, php editor Xiaoxin will introduce to you the use of aliases and embedded memory in PHP. Aliasing refers to multiple variables pointing to the same memory address. Passing data by reference can save memory and improve performance. Embedded memory usage refers to embedding small data structures directly into variables, reducing the overhead of memory allocation and release. Understanding and correctly using these two features can improve the efficiency and performance of your code during the development process.

Question content

Is there a difference in memory usage between alias types (1)

type String1 string

and embedded type (2)

type String2 struct {
    string
}

I think the second example contains two pointers: String2 has a pointer to string and string has a pointer to UTF-8 bytes pointer.

Is it the same as example 1?

Solution

The structure value in Go is not a reference. It is a block of memory containing structure fields (and possibly holes caused by alignment).

So the two types of values ​​should look the same in memory.

Easy to verify: https://www.php.cn/link/8b36cb431b5aa0d245be4e62cdb8e6b3

The above is the detailed content of Aliases and embedded memory usage. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete