Last active
March 15, 2017 07:58
-
-
Save YanhaoYang/df572e8b1ed37578ce77aba37ee72475 to your computer and use it in GitHub Desktop.
Golang memory allocation and GC -- array + join is much better than string + string + string ...
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 ( | |
"fmt" | |
"runtime" | |
) | |
func main() { | |
m := 1 | |
s := "" | |
for i := 0; i < 100001; i++ { | |
s += "." | |
if i%m == 0 { | |
m = m * 10 | |
ms := runtime.MemStats{} | |
runtime.ReadMemStats(&ms) | |
fmt.Printf("i = %8d, TotalAlloc = %10d, HeapObjects = %8d\n", | |
i, ms.TotalAlloc, ms.HeapObjects) | |
} | |
} | |
} |
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
i = 0, TotalAlloc = 37816, HeapObjects = 108 | |
i = 10, TotalAlloc = 39024, HeapObjects = 122 | |
i = 100, TotalAlloc = 44816, HeapObjects = 214 | |
i = 1000, TotalAlloc = 570368, HeapObjects = 1116 | |
i = 10000, TotalAlloc = 53220720, HeapObjects = 942 | |
i = 100000, TotalAlloc = 5309720936, HeapObjects = 146 |
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
i = 0, TotalAlloc = 37816, HeapObjects = 108 | |
i = 10, TotalAlloc = 39024, HeapObjects = 122 | |
i = 100, TotalAlloc = 44816, HeapObjects = 214 | |
i = 1000, TotalAlloc = 570368, HeapObjects = 1116 | |
i = 10000, TotalAlloc = 53213216, HeapObjects = 10118 | |
i = 100000, TotalAlloc = 5309706304, HeapObjects = 100120 |
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 ( | |
"fmt" | |
"runtime" | |
"strings" | |
) | |
func main() { | |
m := 1 | |
var s []string | |
for i := 0; i < 100001; i++ { | |
s = append(s, ".") | |
if i%m == 0 { | |
m = m * 10 | |
ms := runtime.MemStats{} | |
runtime.ReadMemStats(&ms) | |
fmt.Printf("i = %8d, TotalAlloc = %10d, HeapObjects = %8d\n", | |
i, ms.TotalAlloc, ms.HeapObjects) | |
} | |
} | |
_ = strings.Join(s, "") | |
} |
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
i = 0, TotalAlloc = 49688, HeapObjects = 122 | |
i = 10, TotalAlloc = 51344, HeapObjects = 135 | |
i = 100, TotalAlloc = 54960, HeapObjects = 140 | |
i = 1000, TotalAlloc = 83664, HeapObjects = 145 | |
i = 10000, TotalAlloc = 876912, HeapObjects = 156 | |
i = 100000, TotalAlloc = 9300256, HeapObjects = 125 |
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
i = 0, TotalAlloc = 49480, HeapObjects = 121 | |
i = 10, TotalAlloc = 51072, HeapObjects = 133 | |
i = 100, TotalAlloc = 54688, HeapObjects = 138 | |
i = 1000, TotalAlloc = 83392, HeapObjects = 143 | |
i = 10000, TotalAlloc = 876640, HeapObjects = 154 | |
i = 100000, TotalAlloc = 9298048, HeapObjects = 166 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment