mirror of
https://github.com/imgproxy/imgproxy.git
synced 2025-10-10 12:12:40 +02:00
Optimize memory usage
This commit is contained in:
8
main.go
8
main.go
@@ -4,12 +4,20 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"runtime/debug"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"golang.org/x/net/netutil"
|
"golang.org/x/net/netutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
// Force garbage collection
|
||||||
|
go func() {
|
||||||
|
for _ = range time.Tick(time.Second) {
|
||||||
|
debug.FreeOSMemory()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
l, err := net.Listen("tcp", conf.Bind)
|
l, err := net.Listen("tcp", conf.Bind)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
@@ -176,6 +176,7 @@ func processImage(data []byte, imgtype imageType, po processingOptions) ([]byte,
|
|||||||
err := C.int(0)
|
err := C.int(0)
|
||||||
|
|
||||||
var img *C.struct__VipsImage
|
var img *C.struct__VipsImage
|
||||||
|
defer C.clear_image(&img)
|
||||||
|
|
||||||
defer C.vips_cleanup()
|
defer C.vips_cleanup()
|
||||||
|
|
||||||
@@ -241,8 +242,11 @@ func processImage(data []byte, imgtype imageType, po processingOptions) ([]byte,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Finally, save
|
// Finally, save
|
||||||
imgsize := C.size_t(0)
|
|
||||||
var ptr unsafe.Pointer
|
var ptr unsafe.Pointer
|
||||||
|
defer C.g_free(C.gpointer(ptr))
|
||||||
|
|
||||||
|
imgsize := C.size_t(0)
|
||||||
|
|
||||||
switch po.format {
|
switch po.format {
|
||||||
case JPEG:
|
case JPEG:
|
||||||
err = C.vips_jpegsave_go(img, &ptr, &imgsize, 1, C.int(conf.Quality), 0)
|
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()
|
return nil, vipsError()
|
||||||
}
|
}
|
||||||
|
|
||||||
C.g_object_unref(C.gpointer(img))
|
|
||||||
|
|
||||||
buf := C.GoBytes(ptr, C.int(imgsize))
|
buf := C.GoBytes(ptr, C.int(imgsize))
|
||||||
C.g_free(C.gpointer(ptr))
|
|
||||||
|
|
||||||
return buf, nil
|
return buf, nil
|
||||||
}
|
}
|
||||||
|
7
vips.h
7
vips.h
@@ -21,9 +21,14 @@ vips_initialize()
|
|||||||
return vips_init("imgproxy");
|
return vips_init("imgproxy");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
clear_image(VipsImage **in) {
|
||||||
|
g_clear_object(in);
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
swap_and_clear(VipsImage **in, VipsImage *out) {
|
swap_and_clear(VipsImage **in, VipsImage *out) {
|
||||||
g_object_unref((gpointer) *in);
|
clear_image(in);
|
||||||
*in = out;
|
*in = out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user