Created
June 29, 2026 10:44
-
-
Save arxeiss/88cfbf725f323e5a647601d53beca4b0 to your computer and use it in GitHub Desktop.
Test memory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "bufio" | |
| "fmt" | |
| "os" | |
| "runtime" | |
| "strings" | |
| ) | |
| func printOutMemoryUsage() { | |
| var m runtime.MemStats | |
| runtime.ReadMemStats(&m) | |
| rss := getRSS() | |
| fmt.Println(" HeapAlloc | HeapSys | HeapIdle | HeapInUse | RSS") | |
| fmt.Printf("Memory: %6d | %7d | %8d | %9d | %3d\n", | |
| m.HeapAlloc/1024/1024, m.HeapSys/1024/1024, m.HeapIdle/1024/1024, | |
| m.HeapInuse/1024/1024, rss/1024) | |
| } | |
| // getRSS returns the Resident Set Size in bytes by reading /proc/self/status. | |
| func getRSS() uint64 { | |
| file, err := os.Open("/proc/self/status") | |
| if err != nil { | |
| return 0 | |
| } | |
| defer file.Close() | |
| scanner := bufio.NewScanner(file) | |
| for scanner.Scan() { | |
| line := scanner.Text() | |
| if strings.HasPrefix(line, "VmRSS:") { | |
| fields := strings.Fields(line) | |
| if len(fields) >= 2 { | |
| var rss uint64 | |
| _, err := fmt.Sscanf(fields[1], "%d", &rss) | |
| if err != nil { | |
| return 0 | |
| } | |
| // VmRSS in /proc/self/status is in kB, convert to bytes | |
| return rss | |
| } | |
| } | |
| } | |
| return 0 | |
| } | |
| type A struct { | |
| a int | |
| b int | |
| c int | |
| } | |
| func main() { | |
| fmt.Print(`=== Memory Usage Statistics === | |
| Heap Alloc - Currently allocated heap objects | |
| Heap Sys - Heap memory obtained from OS | |
| Heap Idle - Idle spans waiting to be released | |
| Heap InUse - In-use spans (cannot be released)`) | |
| fmt.Println("\n\n === Fresh start ===") | |
| printOutMemoryUsage() | |
| fmt.Println("\n === Allocated array ===") | |
| lInt := 1000_000_000 | |
| lPtr := 100_000_000 | |
| // a := make([]int, lInt) | |
| a := make([]*A, lPtr) | |
| _, _ = lPtr, lInt | |
| l := len(a) | |
| fmt.Printf("Len: %d, Cap: %d\n", len(a), cap(a)) | |
| printOutMemoryUsage() | |
| fmt.Println("\n === Fill array ===") | |
| for i := range l { | |
| // a[i] = i | |
| a[i] = &A{a: i, b: i, c: i} | |
| } | |
| fmt.Printf("Len: %d, Cap: %d\n", len(a), cap(a)) | |
| printOutMemoryUsage() | |
| fmt.Println("\n === Clear array ===") | |
| // clear(a) | |
| a = a[:0] | |
| runtime.GC() | |
| fmt.Printf("Len: %d, Cap: %d\n", len(a), cap(a)) | |
| printOutMemoryUsage() | |
| fmt.Println("\n === Re-fill array ===") | |
| for i := 0; i < l; i++ { | |
| // a = append(a, i) | |
| a = append(a, &A{a: i, b: i, c: i}) | |
| } | |
| runtime.GC() | |
| fmt.Printf("Len: %d, Cap: %d\n", len(a), cap(a)) | |
| printOutMemoryUsage() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment