「50」Map GC问题

问题的起因:「29」map delete Mem不释放问题

那么问题来:

  • map内存释放的时机呢?
  • 释放如果map过大,释放内存过程中会不会有GC StopWorld问题?

代码部分:

map的value不包含pointer:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package main

import (
"fmt"
"os"
"runtime/trace"
"strconv"
"time"
)

func AddMap() {
now:=time.Now().Unix()
fmt.Println(now)
for i := 0; i < 10000; i++ {
mmp := make(map[string]int)
for ia := 0; ia < 100000; ia++ {
v := ia
mmp[strconv.Itoa(ia)] = v
}
}
fmt.Println(time.Now().Unix()-now)
}


func main() {
trace.Start(os.Stderr)
defer trace.Stop()
AddMap()
}

map的value包含pointer:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package main

import (
"fmt"
"os"
"runtime/trace"
"strconv"
"time"
)

func AddMap() {
now:=time.Now().Unix()
fmt.Println(now)
for i := 0; i < 10000; i++ {
mmp := make(map[string]*int)
for ia := 0; ia < 100000; ia++ {
v := ia
mmp[strconv.Itoa(ia)] = &v
}
}
fmt.Println(time.Now().Unix()-now)
}


func main() {
trace.Start(os.Stderr)
defer trace.Stop()
AddMap()
}

trace结果:

初步方案:

Slice替代Map方案:

Test

benchmark

待更新