4 ms·
clear([]byte) if you want to go to the extreme and clean your own memory.
by gethly 11mo ago
clear([]byte) if you want to go to the extreme and clean your own memory.
- kbolino 11mo agoThis only wipes one copy. The GC is free to move the allocation around, and is not required to clear the old copies.
- gethly 11mo agoWrong, the []byte is a pointer. There is no copy anywhere. That's why using []byte in encryption makes sense - you can wipe it out whenever you need, whereas string, the only immutable type in Go, would be a problem. Same can be done with array([32]byte).
- kbolino 11mo agoGo has a precise garbage collector that knows where all the pointers are. So while yes, []byte is a (fat) pointer, the GC is free to move its target to a new location, as long as it updates all the pointers to it. This means that you may observe the address returned by &s[0] for some slice s changing even though you did nothing to s yourself. The process of actually moving allocated memory around by the GC is known as compaction. It's not done aggressively today but Go reserves the right to do so in the future.