Linking to shared library increases memory footprint

System info
Linux host1 4.4.0-131-generic #157-Ubuntu SMP Thu Jul 12 15:51:36 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
Go version
go version go1.10.4 linux/amd64

I have a hello-world application below:
package main
import “fmt”
func main() {
fmt.Println(“hello world”)
for {
}
}

When i compile it with -linkshared flag, I see RES memory go really high ~20MB
However, when i build statically without linkshared flag, the RES memory footprint is only ~1MB

Whats going on? I didn’t expect RES memory to go up when using shared lib (linkshared). Only the shared memory should have gone up…

I started 3 instances of the application.

Statically linked build (1.4MB memory footprint)

go build hello-world.go

$ top -b -n1 | grep hello
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
59838 root 20 0 3164 1428 1020 R 106.7 0.0 0:12.26 hello-world
59847 root 20 0 3164 1428 1020 R 106.7 0.0 0:10.05 hello-world
59853 root 20 0 3164 1480 1084 R 100.0 0.0 0:08.63 hello-world

$ ldd hello-world
not a dynamic executable

Shared library build (20MB memory footprint)

go build -linkshared hello-world.go

$ top -b -n1 | grep hello
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
61395 root 20 0 158820 20396 12684 R 106.2 0.2 0:12.85 hello-world
61405 root 20 0 158820 20484 12760 R 106.2 0.2 0:11.38 hello-world
61385 root 20 0 158820 20464 12744 R 100.0 0.2 0:15.11 hello-world

$ ldd hello-world
linux-vdso.so.1 => (0x00007ffff6ed1000)
libstd.so => /usr/lib/go-1.10/pkg/linux_amd64_dynlink/libstd.so (0x00007f40e9d8a000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f40e99c0000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f40e97bc000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f40e959f000)
/lib64/ld-linux-x86-64.so.2 (0x00007f40ec218000)

As far as I know the resident parts of the library are also counted as RSS (in addition to shared). I bet the libs also allocate stuff, tables for all kinds of stuff.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.