From d58516b1e5334b635ce6205f0296a68f2ef56f3e Mon Sep 17 00:00:00 2001 From: Yuki May Date: Wed, 4 Jan 2023 19:37:30 +0600 Subject: [PATCH 1/4] Add Go Memory Model entry file --- content/go/concepts/memory/go-memory.md | 73 +++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 content/go/concepts/memory/go-memory.md diff --git a/content/go/concepts/memory/go-memory.md b/content/go/concepts/memory/go-memory.md new file mode 100644 index 00000000000..7ebdd82cbb3 --- /dev/null +++ b/content/go/concepts/memory/go-memory.md @@ -0,0 +1,73 @@ +--- +Title: 'Go: Memory' +Description: 'The Go memory model specifies the rules for how memory is accessed and modified in concurrent programs. It allows for the concurrent execution of multiple threads and includes operations such as reading, writing, allocating, and freeing memory. ' +Subjects: + - 'Computer Science' + - 'Developer Tools' + +Tags: + - 'Concurrency' + - 'Data Structures' + - 'Memory' + - 'Variables' + +CatalogContent: + - 'learn-go' + - 'paths/back-end-engineer-career-path' + - 'paths/computer-science' +--- + +# Go: Memory Model + +The Go programming language has a specific memory model that describes how variables are stored in memory and how they can be accessed by different threads. This memory +model is important because it determines the behavior of programs that use concurrency, or the execution of multiple threads at the same time. + +## Memory Operations + +In Go, there are several basic operations that can be performed on memory: + +1. **Reading**: This operation involves accessing a memory location and obtaining the value stored at that location. For example: + +```go +x := 10 +y := x +``` + +In this code, the value of `x` is being read and stored in the variable `y`. + +2. **Writing**: This operation involves storing a value in a specific memory location. For example: + +```go +x := 10 +y := 20 +x = y +``` +In this code, the value of `y` is being written to the memory location associated with `x`. + +3. **Allocating**: This operation involves creating a new block of memory and returning a pointer to the start of the block. In Go, this can be done using the `new` +function: + +```go +x := new(int) +``` + +This code creates a new block of memory for an `int` value and stores a pointer to the start of the block in the variable `x`. + +4. **Freeing**: This operation involves releasing a block of memory that is no longer needed, so that it can be reused by the program. In Go, memory is automatically +managed using a garbage collector, so explicit freeing of memory is not usually necessary. + +## Memory Model + +The Go memory model is a set of rules that dictate how memory operations can be performed in a concurrent program. These rules specify the order in which memory operations must be performed and how they can be observed by different threads. + +### Concurrent Access to Shared Memory + +One important aspect of the Go memory model is that it allows multiple threads to access shared memory concurrently, as long as they follow the rules for memory operations. This feature allows Go programs to take advantage of multiple CPU cores and improve performance. + +### Ordering of Memory Operations + +However, the Go memory model also requires that certain memory operations be performed in a specific order to ensure that the program behaves correctly. For example, a write operation must occur before a read operation that depends on the written value. + +### Importance of the Go Memory Model + +Overall, the Go memory model is a crucial aspect of the language that enables efficient and correct concurrent programming. Understanding how it works is essential for writing correct and performant Go programs. From 804999dcbe8a3614d295cd5c18342c8d97976702 Mon Sep 17 00:00:00 2001 From: ethersea <112825040+ethersea@users.noreply.github.com> Date: Sat, 7 Jan 2023 19:37:43 +0600 Subject: [PATCH 2/4] Update content/go/concepts/memory/go-memory.md Co-authored-by: SSwiniarski <86081858+SSwiniarski@users.noreply.github.com> --- content/go/concepts/memory/go-memory.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/go/concepts/memory/go-memory.md b/content/go/concepts/memory/go-memory.md index 7ebdd82cbb3..4e7f37ac1fd 100644 --- a/content/go/concepts/memory/go-memory.md +++ b/content/go/concepts/memory/go-memory.md @@ -1,5 +1,5 @@ --- -Title: 'Go: Memory' +Title: 'Memory' Description: 'The Go memory model specifies the rules for how memory is accessed and modified in concurrent programs. It allows for the concurrent execution of multiple threads and includes operations such as reading, writing, allocating, and freeing memory. ' Subjects: - 'Computer Science' From 051f2dba6724eb628efc495806a82040e3781af7 Mon Sep 17 00:00:00 2001 From: Christine Yang Date: Thu, 23 Feb 2023 20:50:51 -0500 Subject: [PATCH 3/4] Fix tests --- content/go/concepts/memory/go-memory.md | 73 ------------------------- content/go/concepts/memory/memory.md | 67 +++++++++++++++++++++++ 2 files changed, 67 insertions(+), 73 deletions(-) delete mode 100644 content/go/concepts/memory/go-memory.md create mode 100644 content/go/concepts/memory/memory.md diff --git a/content/go/concepts/memory/go-memory.md b/content/go/concepts/memory/go-memory.md deleted file mode 100644 index 4e7f37ac1fd..00000000000 --- a/content/go/concepts/memory/go-memory.md +++ /dev/null @@ -1,73 +0,0 @@ ---- -Title: 'Memory' -Description: 'The Go memory model specifies the rules for how memory is accessed and modified in concurrent programs. It allows for the concurrent execution of multiple threads and includes operations such as reading, writing, allocating, and freeing memory. ' -Subjects: - - 'Computer Science' - - 'Developer Tools' - -Tags: - - 'Concurrency' - - 'Data Structures' - - 'Memory' - - 'Variables' - -CatalogContent: - - 'learn-go' - - 'paths/back-end-engineer-career-path' - - 'paths/computer-science' ---- - -# Go: Memory Model - -The Go programming language has a specific memory model that describes how variables are stored in memory and how they can be accessed by different threads. This memory -model is important because it determines the behavior of programs that use concurrency, or the execution of multiple threads at the same time. - -## Memory Operations - -In Go, there are several basic operations that can be performed on memory: - -1. **Reading**: This operation involves accessing a memory location and obtaining the value stored at that location. For example: - -```go -x := 10 -y := x -``` - -In this code, the value of `x` is being read and stored in the variable `y`. - -2. **Writing**: This operation involves storing a value in a specific memory location. For example: - -```go -x := 10 -y := 20 -x = y -``` -In this code, the value of `y` is being written to the memory location associated with `x`. - -3. **Allocating**: This operation involves creating a new block of memory and returning a pointer to the start of the block. In Go, this can be done using the `new` -function: - -```go -x := new(int) -``` - -This code creates a new block of memory for an `int` value and stores a pointer to the start of the block in the variable `x`. - -4. **Freeing**: This operation involves releasing a block of memory that is no longer needed, so that it can be reused by the program. In Go, memory is automatically -managed using a garbage collector, so explicit freeing of memory is not usually necessary. - -## Memory Model - -The Go memory model is a set of rules that dictate how memory operations can be performed in a concurrent program. These rules specify the order in which memory operations must be performed and how they can be observed by different threads. - -### Concurrent Access to Shared Memory - -One important aspect of the Go memory model is that it allows multiple threads to access shared memory concurrently, as long as they follow the rules for memory operations. This feature allows Go programs to take advantage of multiple CPU cores and improve performance. - -### Ordering of Memory Operations - -However, the Go memory model also requires that certain memory operations be performed in a specific order to ensure that the program behaves correctly. For example, a write operation must occur before a read operation that depends on the written value. - -### Importance of the Go Memory Model - -Overall, the Go memory model is a crucial aspect of the language that enables efficient and correct concurrent programming. Understanding how it works is essential for writing correct and performant Go programs. diff --git a/content/go/concepts/memory/memory.md b/content/go/concepts/memory/memory.md new file mode 100644 index 00000000000..f2395785450 --- /dev/null +++ b/content/go/concepts/memory/memory.md @@ -0,0 +1,67 @@ +--- +Title: 'Memory' +Description: 'The Go memory model specifies the rules for how memory is accessed and modified in concurrent programs.' +Subjects: + - 'Computer Science' + - 'Developer Tools' +Tags: + - 'Concurrency' + - 'Data Structures' + - 'Memory' + - 'Variables' +CatalogContent: + - 'learn-go' + - 'paths/back-end-engineer-career-path' + - 'paths/computer-science' +--- + +The Go memory model is a set of rules that dictate how memory operations can be performed in a concurrent program. These rules specify the order in which memory operations must be performed and how they can be observed by different threads. + +## Memory Operations + +In Go, the concurrent execution of multiple threads includes basic operations that can be performed on memory: + +### 1. Reading + +This operation involves accessing a memory location and obtaining the value stored at that location. For example: + +```go +x := 10 +y := x +``` + +In this code, the value of `x` is being read and stored in the variable `y`. + +### 2. Writing + +This operation involves storing a value in a specific memory location. For example: + +```go +x := 10 +y := 20 +x = y +``` + +In this code, the value of `y` is being written to the memory location associated with `x`. + +### 3. Allocating + +This operation involves creating a new block of memory and returning a pointer to the start of the block. In Go, this can be done using the `new` function: + +```go +x := new(int) +``` + +This code creates a new block of memory for an `int` value and stores a pointer to the start of the block in the variable `x`. + +### 4. Freeing + +This operation involves releasing a block of memory that is no longer needed, so that it can be reused in the program. In Go, memory is automatically managed using a garbage collector, so explicit freeing of memory is not usually necessary. + +## Concurrent Access to Shared Memory + +One important aspect of the Go memory model is that it allows multiple threads to access shared memory concurrently. This feature allows Go programs to take advantage of multiple CPU cores and improve performance. Access to the shared data within memory should be serialized. + +## Ordering of Memory Operations + +However, the Go memory model also requires that certain memory operations be performed in a specific order to ensure that the program behaves correctly. For example, a write operation must occur before a read operation which depends on the written value. From fd7757463013c708740b07de849deb7961fe1d7b Mon Sep 17 00:00:00 2001 From: Christine Yang Date: Thu, 23 Feb 2023 20:54:40 -0500 Subject: [PATCH 4/4] Reword beginning of paragraph --- content/go/concepts/memory/memory.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/content/go/concepts/memory/memory.md b/content/go/concepts/memory/memory.md index f2395785450..84e5959aacc 100644 --- a/content/go/concepts/memory/memory.md +++ b/content/go/concepts/memory/memory.md @@ -30,7 +30,7 @@ x := 10 y := x ``` -In this code, the value of `x` is being read and stored in the variable `y`. +In this code, the value of `x` is read and stored in the variable `y`. ### 2. Writing @@ -42,7 +42,7 @@ y := 20 x = y ``` -In this code, the value of `y` is being written to the memory location associated with `x`. +In this code, the value of `y` is written to the memory location associated with `x`. ### 3. Allocating @@ -56,7 +56,7 @@ This code creates a new block of memory for an `int` value and stores a pointer ### 4. Freeing -This operation involves releasing a block of memory that is no longer needed, so that it can be reused in the program. In Go, memory is automatically managed using a garbage collector, so explicit freeing of memory is not usually necessary. +This operation involves releasing a block of memory that is no longer needed so that it can be reused in the program. In Go, memory is automatically managed using a garbage collector, so explicit freeing of memory is not usually necessary. ## Concurrent Access to Shared Memory @@ -64,4 +64,4 @@ One important aspect of the Go memory model is that it allows multiple threads t ## Ordering of Memory Operations -However, the Go memory model also requires that certain memory operations be performed in a specific order to ensure that the program behaves correctly. For example, a write operation must occur before a read operation which depends on the written value. +The Go memory model also requires that certain memory operations be performed in a specific order to ensure that the program behaves correctly. For example, a write operation must occur before a read operation which depends on the written value.