1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2024-11-24 08:12:38 +02:00

Optimize memory usage

This commit is contained in:
DarthSim 2017-09-28 19:43:21 +06:00
parent 02a9ed4210
commit 6d840c7cb6
3 changed files with 19 additions and 5 deletions

View File

@ -4,12 +4,20 @@ import (
"log"
"net"
"net/http"
"runtime/debug"
"time"
"golang.org/x/net/netutil"
)
func main() {
// Force garbage collection
go func() {
for _ = range time.Tick(time.Second) {
debug.FreeOSMemory()
}
}()
l, err := net.Listen("tcp", conf.Bind)
if err != nil {
log.Fatal(err)

View File

@ -176,6 +176,7 @@ func processImage(data []byte, imgtype imageType, po processingOptions) ([]byte,
err := C.int(0)
var img *C.struct__VipsImage
defer C.clear_image(&img)
defer C.vips_cleanup()
@ -241,8 +242,11 @@ func processImage(data []byte, imgtype imageType, po processingOptions) ([]byte,
}
// Finally, save
imgsize := C.size_t(0)
var ptr unsafe.Pointer
defer C.g_free(C.gpointer(ptr))
imgsize := C.size_t(0)
switch po.format {
case JPEG:
err = C.vips_jpegsave_go(img, &ptr, &imgsize, 1, C.int(conf.Quality), 0)
@ -255,10 +259,7 @@ func processImage(data []byte, imgtype imageType, po processingOptions) ([]byte,
return nil, vipsError()
}
C.g_object_unref(C.gpointer(img))
buf := C.GoBytes(ptr, C.int(imgsize))
C.g_free(C.gpointer(ptr))
return buf, nil
}

7
vips.h
View File

@ -21,9 +21,14 @@ vips_initialize()
return vips_init("imgproxy");
}
void
clear_image(VipsImage **in) {
g_clear_object(in);
}
void
swap_and_clear(VipsImage **in, VipsImage *out) {
g_object_unref((gpointer) *in);
clear_image(in);
*in = out;
}