skip to Main Content

I have a server running Ubuntu 22.04 where I have apache and php installed. I have recently started using libvips for php, and I have noticed that every time a php script is run, the amount of ram used increases more and more. For example, if the free ram is 15000 MB, after running the script dozens of times (not simultaneously), the free ram drops to 5000 MB, without being freed.

php file example:

<?php
require __DIR__ . '/vendor/autoload.php';
use JcupittVips;

$image = VipsImage::newFromFile($inputImagePath);
$image = $image->thumbnail_image($width, ['height' => $height, 'crop' => 'centre']);
$image->writeToFile($outputImagePath, ['Q' => $quality]);

Instead if at the end of the file i call the function VipsConfig::shutDown(); the memory is released.

Also sometimes libvips returns an error:

(process:211949): GLib-GObject-WARNING **: 16:54:47.542: cannot register existing type 'VipsObject'

(process:211949): GLib-CRITICAL **: 16:54:47.542: g_once_init_leave: assertion 'result != 0' failed

(process:211949): GLib-GObject-CRITICAL **: 16:54:47.542: g_type_register_static: assertion 'parent_
type > 0' failed

(process:211949): GLib-CRITICAL **: 16:54:47.542: g_once_init_leave: assertion 'result != 0' failed

libvips42 version: 8.12.1, php-vips version: 2.0.3

3

Answers


  1. Chosen as BEST ANSWER

    libvips 8.13 resolved these issues.


  2. VipsConfig::shutDown() releases the various libvips caches, so I would call it if you can. The alternative is to fork() for each request and rely on the OS for clean up (if I understand what you’re doing).

    Don’t use thumbnail_image unless you have to, it’s only there for cases where you must do some processing before thumbnailing. For many image formats it’s much faster to simply do:

    $image = VipsImage::thumbnail($inputImagePath, $width, [
        'height' => $height, 
        'crop' => 'centre'
    ]);
    

    Because thumbnail combines load and resize in one operation, it can exploit tricks like libjpeg shrink-on-load.

    If you use:

    $image = VipsImage::newFromFile($inputImagePath);
    

    libvips will (probably, it depends on the image format and size) decompress the image to memory and keep it in a cache in case you need it again.

    You can size the cache smaller with eg.:

    VipsConfig::cacheSetMax(5);
    

    Now libvips will just cache the previous 5 operations.

    If you use sequential mode for open, you can avoid decompressing to memory entirely. There’s a chapter in the docs about the various ways libvips opens files and how that affects memory use:

    https://www.libvips.org/API/current/How-it-opens-files.html

    Login or Signup to reply.
  3. You could try calling

    VipsConfig::concurrencySet(1);
    

    before doing any processing.
    It solved the problem in my application, with php running in cli mode.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search