jehutyy
(Jehutyy)
September 18, 2020, 11:15am
1
I am trying to allocate 1GB of memory with go and be able to observe memory graph rising up then going down.
So far here how i’m doing it :
package main
import (
"fmt"
"time"
)
func sleep() {
duration := time.Second * 3
time.Sleep(duration)
}
func main() {
fmt.Println("Starting")
a := make([]uint, 1 << 30)
_ = a
sleep()
}
It seems to work in a way that I actually see a bump on memory usage graph but definitely not 1GB.
Edit:
I was definitely confused about type thanks to @NobbZ and @petrus I understand now that I should have used uint8 to effectively allocate 1GB and not 4/8GB.
And I should have used allocated memory to actually see memory consumption.
Thank you very much both of you
NobbZ
(Norbert Melzer)
September 18, 2020, 11:25am
2
You are requesting memory for 1³⁰ uint
, and uint
is specified as either having 32 or 64 bit width.
Therefore the observed memory consumption will be either 32/8 * 1³⁰ or 64/8 * 1³⁰. Meaning it is either 4 or 8 GiB of memory.
You probably want to use []
(u
)int8
.
3 Likes
petrus
(petrus)
September 18, 2020, 11:32am
3
Try making an attempt to use the memory.
package main
import (
"fmt"
"time"
)
func sleep() {
duration := 5 * time.Second
time.Sleep(duration)
}
func main() {
fmt.Println("Starting")
a := make([]byte, 1024*1024*1024)
for i := 0; i < len(a); i += 1024 {
a[i] = byte(i / 42)
}
sleep()
for i := 0; i < len(a); i += 1024 {
a[i] = byte(i % 42)
}
fmt.Println("Stopping")
}
1 Like
system
(system)
Closed
December 17, 2020, 11:32am
4
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.