How to reduce virtual memory by optimising my PHP code?
Posted
by
iCeR
on Stack Overflow
See other posts from Stack Overflow
or by iCeR
Published on 2010-12-31T01:28:14Z
Indexed on
2010/12/31
1:54 UTC
Read the original article
Hit count: 227
My current code (see below) uses 147MB of virtual memory! My provider has allocated 100MB by default and the process is killed once run, causing an internal error. The code is utilising curl multi and must be able to loop with more than 150 iterations whilst still minimizing the virtual memory. The code below is only set at 150 iterations and still causes the internal server error. At 90 iterations the issue does not occur.
How can I adjust my code to lower the resource use / virtual memory?
Thanks!
<?php
function udate($format, $utimestamp = null) {
if ($utimestamp === null)
$utimestamp = microtime(true);
$timestamp = floor($utimestamp);
$milliseconds = round(($utimestamp - $timestamp) * 1000);
return date(preg_replace('`(?<!\\\\)u`', $milliseconds, $format), $timestamp);
}
$url = 'https://www.testdomain.com/';
$curl_arr = array();
$master = curl_multi_init();
for($i=0; $i<150; $i++)
{
$curl_arr[$i] = curl_init();
curl_setopt($curl_arr[$i], CURLOPT_URL, $url);
curl_setopt($curl_arr[$i], CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_arr[$i], CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl_arr[$i], CURLOPT_SSL_VERIFYPEER, FALSE);
curl_multi_add_handle($master, $curl_arr[$i]);
}
do {
curl_multi_exec($master,$running);
} while($running > 0);
for($i=0; $i<150; $i++)
{
$results = curl_multi_getcontent ($curl_arr[$i]);
$results = explode("<br>", $results);
echo $results[0];
echo "<br>";
echo $results[1];
echo "<br>";
echo udate('H:i:s:u');
echo "<br><br>";
usleep(100000);
}
?>
Processor Information Total processors: 8
Processor #1
Vendor GenuineIntel
Name Intel(R) Xeon(R) CPU E5405 @ 2.00GHz
Speed 1995.120 MHz
Cache 6144 KB
Processor #2
Vendor GenuineIntel
Name Intel(R) Xeon(R) CPU E5405 @ 2.00GHz
Speed 1995.120 MHz
Cache 6144 KB
Processor #3
Vendor GenuineIntel
Name Intel(R) Xeon(R) CPU E5405 @ 2.00GHz
Speed 1995.120 MHz
Cache 6144 KB
Processor #4
Vendor GenuineIntel
Name Intel(R) Xeon(R) CPU E5405 @ 2.00GHz
Speed 1995.120 MHz
Cache 6144 KB
Processor #5
Vendor GenuineIntel
Name Intel(R) Xeon(R) CPU E5405 @ 2.00GHz
Speed 1995.120 MHz
Cache 6144 KB
Processor #6
Vendor GenuineIntel
Name Intel(R) Xeon(R) CPU E5405 @ 2.00GHz
Speed 1995.120 MHz
Cache 6144 KB
Processor #7
Vendor GenuineIntel
Name Intel(R) Xeon(R) CPU E5405 @ 2.00GHz
Speed 1995.120 MHz
Cache 6144 KB
Processor #8
Vendor GenuineIntel
Name Intel(R) Xeon(R) CPU E5405 @ 2.00GHz
Speed 1995.120 MHz
Cache 6144 KB
Memory Information
Memory for crash kernel (0x0 to 0x0) notwithin permissible range Memory: 8302344k/9175040k available (2176k kernel code, 80272k reserved, 901k data, 228k init, 7466304k highmem)
System Information
Linux server3.server.com 2.6.18-194.17.1.el5PAE #1 SMP Wed Sep 29 13:31:51 EDT 2010 i686 i686 i386 GNU/Linux
Physical Disks
SCSI device sda: 1952448512 512-byte hdwr sectors (999654 MB) sda: Write Protect is off sda: Mode Sense: 03 00 00 08 SCSI device sda: drive cache: write back SCSI device sda: 1952448512 512-byte hdwr sectors (999654 MB) sda: Write Protect is off sda: Mode Sense: 03 00 00 08 SCSI device sda: drive cache: write back sd 0:1:0:0: Attached scsi disk sda sd 4:0:0:0: Attached scsi removable disk sdb sd 0:1:0:0: Attached scsi generic sg4 type 0 sd 4:0:0:0: Attached scsi generic sg7 type 0
Current Memory Usage
total used free shared buffers cached
Mem: 8306672 7847384
459288 0 487912
6444548 -/+ buffers/cache: 914924 7391748 Swap: 4095992 496 4095496 Total: 12402664 7847880 4554784Current Disk Usage
Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup00-LogVol00 898G 307G 546G 36% / /dev/sda1
99M 19M 76M 20% /boot none
4.0G 0 4.0G 0% /dev/shm /var/tmpMnt 4.0G 1.8G 2.0G 48% /tmp
© Stack Overflow or respective owner