OK, I get it. I've just tried it and unfortunately it doesn't seem to be better than the traditional approach.
With the pool in place, it still user too much memory (unlike my solution with using a heap via glibc). Anyway, thanks - it was worth trying. On Wednesday, May 3, 2017 at 3:33:12 PM UTC+2, T L wrote: > > It would be great if it is possible that the collector only collect a > specified memory block groups. > For example, runtime.GC(group []unsafe.Pointer), will only collect the > memory blocks only referenced by the group parameter. > > On Wednesday, May 3, 2017 at 2:35:43 AM UTC+8, Piotr Narewski wrote: >> >> My app uses large amounts of memory; it actively allocates and frees lots >> of slices. >> I'm talking about gigabytes here. >> >> With the default GOGC=100 setting, it needs twice more memory. >> >> When I decrease GOGC to a lower value, it affects performance pretty >> badly. >> >> So far the best solution I have found is this: >> >> ************* >> /* >> #include <stdlib.h> >> */ >> import "C" >> >> func gcc_malloc(le uint32) unsafe.Pointer { >> ptr := unsafe.Pointer(C.malloc(C.size_t(le+4))) >> *((*uint32)(unsafe.Pointer(ptr))) = le >> return ptr >> } >> >> func gcc_free(ptr unsafe.Pointer) { >> C.free(unsafe.Pointer(ptr)) >> } >> >> func gcc_len(ptr unsafe.Pointer) int { >> return int(*((*uint32)(ptr))) >> } >> >> func gcc_slice(ptr unsafe.Pointer) []byte { >> le := gcc_len(ptr) >> return >> *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{Data:uintptr(ptr)+4, Len:le, >> Cap:le})) >> } >> ************* >> >> So if I need a new slice to store a data, I do: >> slice := gcc_slice(gcc_malloc( HOW_MANY_BYTES )) >> >> Then, when I no longer need this slice, I just do something like: >> gcc_free(unsafe.Pointer(&slice)) >> >> It works and performs very well, but I have a problem with compatibility >> on different platforms. >> For instance, it needs gcc compiler... >> >> Does anyone know a way to do something like this (freeing memory on >> demand), but using pure Go? >> Something that will compile (and work) on any platform, without any extra >> dependencies.. >> >> -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.