|
| 1 | +package metricsop |
| 2 | + |
| 3 | +import ( |
| 4 | + "runtime" |
| 5 | + "runtime/metrics" |
| 6 | + |
| 7 | + "github.com/devlights/gomy/output" |
| 8 | +) |
| 9 | + |
| 10 | +// HeapMemory は、runtime/metrics を利用してヒープメモリ関連の情報を取得するサンプルです. |
| 11 | +// |
| 12 | +// # REFERENCES |
| 13 | +// - https://pkg.go.dev/runtime/metrics@latest |
| 14 | +func HeapMemory() error { |
| 15 | + var ( |
| 16 | + items = []string{ |
| 17 | + "/memory/classes/heap/free:bytes", // 完全に空いていて、システムに戻す資格があるが、戻されていないメモリ |
| 18 | + "/memory/classes/heap/objects:bytes", // ガベージコレクタによってまだ解放されていないメモリ |
| 19 | + "/memory/classes/heap/released:bytes", // 完全に解放され、システムに戻されたメモリ |
| 20 | + "/memory/classes/heap/stacks:bytes", // スタック領域として予約されている、ヒープから割り当てられたメモリ、現在使用中であるか否かを問わない |
| 21 | + "/memory/classes/heap/unused:bytes", // ヒープオブジェクトのために予約されているが、現在ヒープオブジェクトを保持するために使われていないメモリ |
| 22 | + "/gc/heap/allocs:bytes", // アプリケーションによってヒープに割り当てられたメモリの累計。 |
| 23 | + "/gc/heap/allocs:objects", // アプリケーションによって引き起こされたヒープ割り当ての累積カウント |
| 24 | + "/gc/heap/frees:bytes", // ガベージコレクタによって解放されたヒープメモリの累計 |
| 25 | + "/gc/heap/frees:objects", // ストレージがガベージコレクタによって解放されたヒープ割り当ての累積カウント |
| 26 | + "/gc/heap/goal:bytes", // GCサイクル終了時のヒープサイズ目標 |
| 27 | + "/gc/heap/live:bytes", // 前回のGCでマークされたライブオブジェクトが占有するヒープメモリ |
| 28 | + "/gc/heap/objects:objects", // ヒープメモリを占有しているオブジェクトの数 |
| 29 | + } |
| 30 | + samples = make([]metrics.Sample, len(items)) |
| 31 | + bigdata = make([]byte, 1<<28) |
| 32 | + ) |
| 33 | + |
| 34 | + for i, name := range items { |
| 35 | + samples[i].Name = name |
| 36 | + } |
| 37 | + |
| 38 | + runtime.GC() |
| 39 | + metrics.Read(samples) |
| 40 | + |
| 41 | + for _, s := range samples { |
| 42 | + output.Stdoutl("[Name ]", s.Name) |
| 43 | + output.Stdoutf("[Value]", "%+v\n", s.Value) |
| 44 | + output.StdoutHr() |
| 45 | + } |
| 46 | + |
| 47 | + output.Stdoutl("[Buffer]", len(bigdata)) |
| 48 | + |
| 49 | + return nil |
| 50 | + |
| 51 | + /* |
| 52 | + $ task |
| 53 | + task: [build] go build . |
| 54 | + task: [run] ./try-golang -onetime |
| 55 | +
|
| 56 | + ENTER EXAMPLE NAME: metrics_heapmemory |
| 57 | +
|
| 58 | + [Name] "metrics_heapmemory" |
| 59 | + [Name ] /memory/classes/heap/free:bytes |
| 60 | + [Value] {kind:1 scalar:268656640 pointer:<nil>} |
| 61 | + -------------------------------------------------- |
| 62 | + [Name ] /memory/classes/heap/objects:bytes |
| 63 | + [Value] {kind:1 scalar:398432 pointer:<nil>} |
| 64 | + -------------------------------------------------- |
| 65 | + [Name ] /memory/classes/heap/released:bytes |
| 66 | + [Value] {kind:1 scalar:2433024 pointer:<nil>} |
| 67 | + -------------------------------------------------- |
| 68 | + [Name ] /memory/classes/heap/stacks:bytes |
| 69 | + [Value] {kind:1 scalar:458752 pointer:<nil>} |
| 70 | + -------------------------------------------------- |
| 71 | + [Name ] /memory/classes/heap/unused:bytes |
| 72 | + [Value] {kind:1 scalar:682912 pointer:<nil>} |
| 73 | + -------------------------------------------------- |
| 74 | + [Name ] /gc/heap/allocs:bytes |
| 75 | + [Value] {kind:1 scalar:268953784 pointer:<nil>} |
| 76 | + -------------------------------------------------- |
| 77 | + [Name ] /gc/heap/allocs:objects |
| 78 | + [Value] {kind:1 scalar:1662 pointer:<nil>} |
| 79 | + -------------------------------------------------- |
| 80 | + [Name ] /gc/heap/frees:bytes |
| 81 | + [Value] {kind:1 scalar:268555352 pointer:<nil>} |
| 82 | + -------------------------------------------------- |
| 83 | + [Name ] /gc/heap/frees:objects |
| 84 | + [Value] {kind:1 scalar:450 pointer:<nil>} |
| 85 | + -------------------------------------------------- |
| 86 | + [Name ] /gc/heap/goal:bytes |
| 87 | + [Value] {kind:1 scalar:4194304 pointer:<nil>} |
| 88 | + -------------------------------------------------- |
| 89 | + [Name ] /gc/heap/live:bytes |
| 90 | + [Value] {kind:1 scalar:398720 pointer:<nil>} |
| 91 | + -------------------------------------------------- |
| 92 | + [Name ] /gc/heap/objects:objects |
| 93 | + [Value] {kind:1 scalar:1212 pointer:<nil>} |
| 94 | + -------------------------------------------------- |
| 95 | + [Buffer] 268435456 |
| 96 | +
|
| 97 | +
|
| 98 | + [Elapsed] 2.96696ms |
| 99 | + */ |
| 100 | +} |
0 commit comments