• Resolved c.scharfenberg

    (@cscharfenberg)


    Hello,

    I’m not sure if this is the right forum for my request. Nevertheless I post my question here.

    I’m writing a tools that imports thousands of products from an external source (web service) into WooCommerce. Everything works as expected. Only I have some kind of memory leak.
    The ammount of memory reserved by PHP increases everytime I call ‘WC()->api->WC_API_Products->create_product’ or ‘WC()->api->WC_API_Products->edit_product’.
    So obviously this memory leak is not in my code.

    I can imagine that this is due to some WooCommerce and/or WordPress internal caching mechanism to speed up the database base access. After some recherche I’m calling ‘wp_suspend_cache_addition(true)’ noe to deactivate WordPress caching. But this does not help.

    So: can anybody tell me what to do to keep the memory usage low?

    Thanks a lot,
    Carsten

    PS: I’ve made a strange observation: the memory usage on Linux is much higher than on Windows: on Windows each database access increases the memory usage by about 5kb, on Linux by about 150kb.

    CORRECTION:
    ‘wp_suspend_cache_addition(true)’ helps on Windows (PHP 5.4.24) but not on Linux (PHP 5.4.29). Can it be some PHP/Wordpress configuration issue?

    https://wordpress.org/plugins/woocommerce/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Contributor Mike Jolley

    (@mikejolley)

    Want to share your product creation script?

    Thread Starter c.scharfenberg

    (@cscharfenberg)

    I’ve dramatically shortened my script and posted this version below.
    It shows the same behavior as my original script. I’ve ran it with the following four configurations resulting in the specified memory usage increase per product:

    1. Linux with caching: about 116KB per product
    2. Linux without caching: about 180KB per product
    3. Window with caching: about 10KB per product
    4. Windows without caching: about 2KB per product

    So Windows behaves as I would expect. But Linux behaves very strange.

    <?php
    
    function generateRandomString($length) {
    
        $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $charactersLength = strlen($characters);
        $randomString = '';
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[rand(0, $charactersLength - 1)];
        }
    
        return $randomString;
    }
    
    function generateRandomPrice($min, $max, $decimals = 2){
    
        $randomfloat = $min + mt_rand() / mt_getrandmax() * ($max - $min);
    
        return number_format($randomfloat, $decimals);
    }
    
    function createRandomProductData() {
    
        $result = [ 'product' => [
            'title' => generateRandomString(10),
            'type' => 'simple',
            'status' => 'publish',
            'virtual' => false,
            'sku' => generateRandomString(4),
            'managing_stock' => false,
            'downloadable' => false,
            'review_allowed' => false,
            'short_description' => generateRandomString(20),
            'regular_price' => generateRandomPrice(10.0, 99.0)
        ] ];
    
        return $result;
    }
    
    function instantiateWooCommerce() {
    
        $wpLoadPath = 'C:/inetpub/wwwroot/wordpress/wp-load.php';
        //$wpLoadPath = '/opt/wordpress-3.9.1-1/apps/wordpress/htdocs/wp-load.php';
        require_once $wpLoadPath;
    
        $wooCommercePath = realpath(WP_PLUGIN_DIR . '/woocommerce/woocommerce.php');
        require_once $wooCommercePath;
    
        WC()->api->includes();
        WC()->api->register_resources(new WC_API_Server( '/' ));
    
        $credentials = [
            'user_login' => 'FP_product_uploader',
            'user_password' => '12345678'
        ];
        $user = wp_signon($credentials, false);
        wp_set_current_user($user->ID);
    }
    
    function printMemoryUsage($label) {
    
        $memoryUsage = memory_get_usage();
        echo $label . ': ' . $memoryUsage . "\n";
    }
    
    instantiateWooCommerce();
    wp_suspend_cache_addition(true);
    
    $api = WC()->api->WC_API_Products;
    
    printMemoryUsage('before');
    for($i = 0; $i < 10; ++$i) {
        printMemoryUsage($i);
        $productData = createRandomProductData();
        $api->create_product($productData);
    }
    printMemoryUsage('after');
    Thread Starter c.scharfenberg

    (@cscharfenberg)

    Meanwhile I’ve tried a different Linux WordPress installation (running PHP 5.6.12). The result is similar to my Windows result. So this is an issue of the specific WordPress installation (whatever that may be).

    Still there is an increase in memory usage of 2 to 3KB per product – even after turning off WordPress database caching. This is a memory leak I can live with. But I would prefer to get rid of it, of course. So I appreciate any help on this.

    Plugin Contributor Mike Jolley

    (@mikejolley)

    After creation, the new/populated product is returned. This will be retrieved using https://developer.wordpress.org/reference/functions/get_post/ which has its own caches etc. Thats going to be stored in memory to save on future get_post calls for the same product.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Programming Question: Memory Leak when accessing products’ is closed to new replies.