Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit the memory allocation in Go Language?

Tags:

go

I'm finding a way to limit the memory usage in Go language. My application implementing with Go language has a big data that must be loaded in main memory, so I want to limit the maximum memory size of the process to the size specified by the user.

In C language, actually, I accumulate the sizes of malloc'ed memory to do that, but I don't know how to do same thing in Go language.

Please let me know if there is a way to do it.

Thank you.

like image 672
Brian Avatar asked Jun 30 '11 10:06

Brian


People also ask

Does Golang have memory allocation?

Go supports automatic memory management, such as automatic memory allocation and automatic garbage collection, which avoids many lurking bugs.

How memory is managed in Golang?

Go uses OS function mmap similar to TCMalloc (Thread-Caching Malloc) to allocate memory in heap. Go uses goroutine, which maintain a small stack (memory segment) act as memory pool for some memory blocks. The maximum stack size is 1 GB on 64-bit systems, and 250 MB on 32-bit systems, can be set by SetMaxStack function.

Do you have to manage memory in Go?

Go is a language which supports automatic memory management, such as automatic memory allocation and automatic garbage collection. So Go programmers can do programming without handling the underlying verbose memory management.


1 Answers

The Go garbage collector is not deterministic and it is conservative. Therefore, using the runtime.MemStats variable is not going to be accurate for your purpose.

Fix your approximate memory usage by setting the maximum size of data that you are going to allow to be loaded at one time into a process using the input from the user.

like image 174
peterSO Avatar answered Sep 24 '22 20:09

peterSO