Page 1 of 1

I have memory leak

Posted: 2017-01-30T00:02:14-07:00
by Firsim
Hello. I downloaded and installed according to this instruction PerlMagick: https://www.imagemagick.org/script/perl-magick.php

And when I try to use Image::Magick::Q16 in the endless cycle of leak memory for image processing. Even if I do not read the image in memory, and only designate magichka gem: "my $image = Image::Magick::Q16->new;", and then use the undef: "undef $image;". The memory is still not released, and eventually. I tested it for a week..
If you need any information, I am ready to provide it.

Before installing PerlMagick from the site it was installed last PerlMagick from Ubuntu repository. He observed the same problem.

Code: Select all

OS: Ubuntu Server 16.04.1

convert -version
Version: ImageMagick 7.0.4-5 Q16 x86_64 2017-01-26 http://www.imagemagick.org
Copyright: © 1999-2017 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC HDRI OpenMP
Delegates (built-in): fontconfig freetype jbig jng jpeg lzma png tiff x zlib

Re: I have memory leak

Posted: 2017-01-30T04:10:59-07:00
by magick
Perl garbage collection does not automatically release memory. Instead undef may mark it for deletion and a GC sweep in the future may finally return the memory to the system. To detect a memory leak, try allocating memory in a loop. If the resource requirements grow, you have found a true memory leak. Does this script show a memory leak in your environment?

Code: Select all

use Image::Magick;

for ($i=0; $i < 100000; $i++) {
  my $image = Image::Magick->new;
  my $x = $image->Read('logo:');
}

Re: I have memory leak

Posted: 2017-01-31T01:29:41-07:00
by Firsim
magick, Hello. Thanks for the quick response. I checked as you requested, and do not really have a problem. All this time I tested.
The problem is still relevant for me. Here's the code of the script which I use, maybe you can help ..
I tried different ways to use "Image::Magick", as he has in the script => is a memory leak.

Code: Select all

#!/usr/bin/perl -w
use diagnostics;
use warnings;
use strict;

use DBI;
use LWP::Simple;
use Image::Magick;

sub ImgReplace {
	my ($row, $water, $waterS) = @_;
		my $name = $row->{name};
		my $status = $row->{retry_count};
		
	if ($row->{url} =~ m/udp/i) { # - So you need..
		print "Do nothing - ".$name."\n";
	} else {
			
			#
			my $image_url = 'https://localhost/'.$name.'/preview.jpg';
			my $image_temp = '/home/firsim/temp/'.$name.'.jpg';
			
			my $rc = getstore($image_url, $image_temp);
			if (is_error($rc)) {
			  print "getstore of <$image_url> failed with $rc\n";
			}
			# - Saving an image from the web server to the local. So you need..
			
			#
			my $image_patch = '/var/www/temp/preview/'.$name.'.jpg';
			my $image_patchS = '/var/www/temp/preview/nosignal/'.$name.'.jpg';
			
			my $image_patchT = '/var/www/temp/preview/'.$name.'.jpg.tmp';
			my $image_patchST = '/var/www/temp/preview/nosignal/'.$name.'.jpg.tmp';
			# - Template links.
			
			if(-e "$image_temp") { # - Check for file.
			#
				my $image = Image::Magick->new;
				$image->Read($image_temp);
				$image->AdaptiveResize(geometry=>'900x');
				$image->Crop(geometry=>'895x500+0+0', gravity=>'center',);
				$image->Composite(image=>$water, compose=>'Atop', y=>-220, x=>290);
				if ($status ne 0) {
					$image->Composite(image=>$waterS, compose=>'Atop');
						$image->Write(filename => $image_patchT, quality=>'85');
						$image->Write(filename => $image_patchST, quality=>'85');
				} else {
					$image->Write(filename => $image_patchT, quality=>'85');
						$image->Composite(image=>$waterS, compose=>'Atop');
						$image->Write(filename => $image_patchST, quality=>'85');
				}
			# - Some manipulation of the image and save.
			}
		
		#
		rename $image_patchT, $image_patch;
		rename $image_patchST, $image_patchS;
		# - Save.
	}
}

sub StartArray {
my $watermark = '/home/firsim/perl/ImgReplace/watermark.png'; # - The path to the watermark 1
my $watermarkS = '/home/firsim/perl/ImgReplace/nosignal.png'; # - The path to the watermark 2

my $water = Image::Magick->new;
my $waterS = Image::Magick->new;

$water->Read($watermark);
$waterS->Read($watermarkS);

my $db = DBI->connect("DBI:mysql:$database:$hostname", $user, $password); # - Array in DB.
$db->do("SET NAMES 'utf8'");
###
	my $array = $db->prepare("SELECT * FROM `streams` ORDER by `name` ASC;") or print $DBI::errstr;
	$array->execute() or print $DBI::errstr;
		
		while(my $row = $array->fetchrow_hashref){
			ImgReplace($row, $water, $waterS); # - Then we pass the incoming data into a function creating previews.
		}
	$db->disconnect;	
}

# Run script
while (1) {
	StartArray;
	sleep 60;
}

Re: I have memory leak

Posted: 2017-01-31T04:20:13-07:00
by magick
The only way we can help is if you can reduce your script to something we can drop in our environment and run that illustrates a memory leak. Our standard mantra is "If we can reproduce it, we can fix it."

Re: I have memory leak

Posted: 2017-01-31T05:25:07-07:00
by Firsim
magick wrote: 2017-01-31T04:20:13-07:00 The only way we can help is if you can reduce your script to something we can drop in our environment and run that illustrates a memory leak. Our standard mantra is "If we can reproduce it, we can fix it."
OK. Right now I prepare.

Re: I have memory leak

Posted: 2017-02-03T01:43:33-07:00
by Firsim
Hello. In general, I tested and a memory leak occurs only when the use Image::Magick::Q16. Once compiled the latest version, I have always used this module. With the module Image::Magick is not found problems.
Thank you.