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.

Reply via email to