Aliases and embedded memory usage

PHPz
Release: 2024-02-09 13:15:09
forward
662 people have browsed it

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
Copy after login

and embedded type (2)

type String2 struct {
    string
}
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!