Skip to content

Commit

Permalink
Commit the Sitewards ImportImageMemoryLeakFix for publishing to GitHub
Browse files Browse the repository at this point in the history
  • Loading branch information
David Manners committed May 10, 2016
0 parents commit e849bd3
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Sitewards ImportImageMemoryLeakFix
==================================

Issue
-----

Product import with images easily goes over 4Gb on memory with just couple hundreds of products.
The reason for this is that the image model is never properly destoried.

Fix
---

Use a different image processor than `Varien_Image`.
Add a `destruct` method into this new processor and register it on shutdown.
Rewrite `Mage_Catalog_Helper_Image` to change the method `validateUploadFile`.
Call new processor, call destruct and then return the mime type of the image.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* @category Sitewards
* @package Sitewards_ImportImageMemoryLeakFix
* @copyright Copyright (c) Sitewards GmbH (http://www.sitewards.com/)
*/
class Sitewards_ImportImageMemoryLeakFix_Helper_Catalog_Helper_Image extends Mage_Catalog_Helper_Image
{
/**
* Check - is this file an image
*
* Difference from original method - we destroy the image object here,
* i.e. we are not wasting memory, without that fix product import with images
* easily goes over 4Gb on memory with just couple hundreds of products.
*
* @param string $sFilePath
*
* @return bool
* @throws Mage_Core_Exception
*/
public function validateUploadFile($sFilePath) {
if (!getimagesize($sFilePath)) {
Mage::throwException($this->__('Disallowed file type.'));
}

/** @var Sitewards_ImportImageMemoryLeakFix_Model_Destructable_Image $oImageProcessor */
$oImageProcessor = Mage::getModel('sitewards_importimagememoryleakfix/destructable_image', $sFilePath);
$sMimeType = $oImageProcessor->getMimeType();
$oImageProcessor->destruct();

return $sMimeType !== null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* @category Sitewards
* @package Sitewards_ImportImageMemoryLeakFix
* @copyright Copyright (c) Sitewards GmbH (http://www.sitewards.com/)
*/
class Sitewards_ImportImageMemoryLeakFix_Model_Destructable_Image extends Varien_Image
{
/**
* Constructor,
* difference from original constructor - we register a destructor here.
*
* @param string $sFileName
* @param Varien_Image_Adapter $oAdapter Default value is GD2
*/
public function __construct($sFileName = null, $oAdapter = Varien_Image_Adapter::ADAPTER_GD2)
{
parent::__construct($sFileName, $oAdapter);

// Initialize shutdown function
register_shutdown_function(array($this, 'destruct'));
}

/**
* Destroy object image on shutdown
*/
public function destruct()
{
$oAdapter = $this->_getAdapter();
if (method_exists($oAdapter, 'destruct')) {
$oAdapter->destruct();
} else {
Mage::log('Image can not be destructed properly, adapter doesn\'t support the method.');
}
}
}
22 changes: 22 additions & 0 deletions app/code/local/Sitewards/ImportImageMemoryLeakFix/etc/config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0"?>
<config>
<modules>
<Sitewards_ImportImageMemoryLeakFix>
<version>0.1.0</version>
</Sitewards_ImportImageMemoryLeakFix>
</modules>
<global>
<models>
<sitewards_importimagememoryleakfix>
<class>Sitewards_ImportImageMemoryLeakFix_Model</class>
</sitewards_importimagememoryleakfix>
</models>
<helpers>
<catalog>
<rewrite>
<image>Sitewards_ImportImageMemoryLeakFix_Helper_Catalog_Helper_Image</image>
</rewrite>
</catalog>
</helpers>
</global>
</config>
12 changes: 12 additions & 0 deletions app/etc/modules/Sitewards_ImportImageMemoryLeakFix.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0"?>
<config>
<modules>
<Sitewards_ImportImageMemoryLeakFix>
<active>true</active>
<codePool>local</codePool>
<depends>
<Mage_Catalog/>
</depends>
</Sitewards_ImportImageMemoryLeakFix>
</modules>
</config>
8 changes: 8 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "sitewards/import_image_memory_leak_fix",
"description": "Fix a memory leak issue with the product image import process.",
"type": "magento-module",
"require": {
"php": ">=5.5"
}
}
2 changes: 2 additions & 0 deletions modman
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
app/code/local/Sitewards/ImportImageMemoryLeakFix app/code/local/Sitewards/ImportImageMemoryLeakFix
app/etc/modules/Sitewards_ImportImageMemoryLeakFix.xml app/etc/modules/Sitewards_ImportImageMemoryLeakFix

0 comments on commit e849bd3

Please sign in to comment.