Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wasm: program allocs too much memory #35390

Closed
5kbpers opened this issue Nov 6, 2019 · 5 comments
Closed

wasm: program allocs too much memory #35390

5kbpers opened this issue Nov 6, 2019 · 5 comments
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.

Comments

@5kbpers
Copy link

5kbpers commented Nov 6, 2019

What version of Go are you using (go version)?

$ go version
go version go1.12.13 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/5kbpers/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/5kbpers/go-linux-amd64-bootstrap"
GOPROXY=""
GORACE=""
GOROOT="/home/5kbpers/go-linux-amd64-bootstrap"
GOTMPDIR=""
GOTOOLDIR="/home/5kbpers/go-linux-amd64-bootstrap/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build234335217=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Hello! I compiled the following program to WASM:

package main

import (
    "fmt"
    "time"
)

func main() {
    for {
        fmt.Println("Hello, WebAssembly!")
        time.Sleep(time.Millisecond * 500)
    }
}

After opening it with my browser (Chrome 78.0.3904.70), take a memory snapshot, that shows it allocs about 1GB memory:
image
I found a related issue #27462, adjust the preallocated memory to 64MB:

// writeMemorySec writes the section that declares linear memories. Currently one linear memory is being used.
func writeMemorySec(ctxt *ld.Link) {
	sizeOffset := writeSecHeader(ctxt, sectionMemory)

	// Linear memory always starts at address zero.
	// The unit of the sizes is "WebAssembly page size", which is 64Ki.
	// The minimum is currently set to 1GB, which is a lot.
	// More memory can be requested with the grow_memory instruction,
	// but this operation currently is rather slow, so we avoid it for now.
	// TODO(neelance): Use lower initial memory size.
	writeUleb128(ctxt.Out, 1)       // number of memories
	ctxt.Out.WriteByte(0x00)        // no maximum memory size
	writeUleb128(ctxt.Out, 1024) // minimum (initial) memory size

	writeSecSize(ctxt, sizeOffset)
}

But the allocated memory is still very high (about 780MB):
image

What did you expect to see?

The allocated memory should approximately be 64MB after adjusting the preallocated memory.

What did you see instead?

The allocated memory is too much higher than expected.

@ALTree
Copy link
Member

ALTree commented Nov 6, 2019

What version of Go are you using (go version)?

go version go1.12.13 linux/amd64

#27462 was fixed for Go1.13 so please try with that version (you are still on 1.12).

@ALTree ALTree added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Nov 6, 2019
@tmthrgd
Copy link
Contributor

tmthrgd commented Nov 6, 2019

Isn’t this likely a duplicate of #35111 (which is fixed on tip)?

@ALTree
Copy link
Member

ALTree commented Nov 6, 2019

How so? #35111 is a memory leak (the memory starts low and keeps increasing), on this one OP immediately sees that the wasm program takes 1GB of RAM.

The "wasm programs start with 1GB of used RAM" issue is #27462 , which I linked above.

@5kbpers
Copy link
Author

5kbpers commented Nov 6, 2019

@ALTree Go1.13 works to this, thank you!
BTW, I'm curious about why it still allocates 780MB memory after the initial size was adjusted to 64MB.

@ALTree
Copy link
Member

ALTree commented Nov 6, 2019

@5kbpers

Go1.13 works to this, thank you!

Nice, I'll close this issue then.

BTW, I'm curious about why it still allocates 780MB memory after the initial size was adjusted to 64MB.

You mean the patch you posted above that you manually applied? I'm not sure, it's likely that it just wasn't enough to fix the issue or possibly you did not build the patched toolchain correctly so it didn't pick up your change.

@ALTree ALTree closed this as completed Nov 6, 2019
@golang golang locked and limited conversation to collaborators Nov 5, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

4 participants