找到内存使用情况

时间:2012-02-18 13:10:54

标签: php memory-management

我有一张150x150个单元格的表格,每个单元格都有彩色背景和小图像/符号。此外,每个单元格也可以设置零个或多个边框。用户可以更改和添加边框并更改单元格颜色和单元格。使用jquery这一切都很好用,它只是一个简单的html表。

在用户体验结束时,我正在制作此表的pdf供他们下载。这是一项很重要的工作,需要一段时间,这不是我现在主要关注的问题。 jQuery将表数据收集到一个数组中并将其发送到php,使用gd库将其重新创建为图像。

因此,对于每个单元格,我在大图像上绘制一个正确颜色的矩形,加载符号图像并将其重新采样到大图像上,绘制边框并对图像进行破坏符号图像,它工作但需要1分钟。我改变了我的策略,让一个小的彩色rectangke将图像强加在它上面并将其缓存在一个数组中以便再次快速使用。这加快了我的时间并将其降低到30秒,但现在我正在耗尽记忆。

我将桌子分成50个单元块,因此每个块都放在一个页面上,每个块都被制作成一个图像并保存到磁盘上,下一个块被制作并保存,等等。每次gd图像被破坏。然后将所有块插入到pdf中。

所以,毕竟,我的问题是如何找出内存的使用位置,以便我可以尝试将其释放?我发布了我认为导致下面问题的主要功能。在我的测试中,最多有30个不同的符号/颜色图像是25pxX25px,这些是在数组中缓存的图像。

我希望我提供了足够的信息,我的问题已经足够清楚。

感谢您的时间, 托德

//make one "block" of stitches returns image file name.
//function makeStitchChartBlock($img, $startX, $startY, $endX, $endY, $caption, $brand){
function makeStitchChartBlock($stitchChartArray, $startX, $startY, $endX, $endY, $caption, $brand,$blockNumber){
    global $threadColours;
    $stitchCache=array();
    $saveTo = 'result'.$blockNumber.'.jpeg';
    // calculate size of block
    $numRows=($endY-$startY);
    $numColumns=($endX-$startX);
    $heightOfBlock = $numRows*SYMBOL_SIZE; //in pixels --- used to determine size of image to make for block
    $widthOfBlock = $numColumns*SYMBOL_SIZE; //in pixels

    //----plus any extra for captions grid lines
    $heightOfBlock += (($endY-$startY)+1); //each stitch has a grid line before it and the last one also has on after it
    $widthOfBlock += (($endX-$startX)+1);

    // create image size of block to put stitches in
    $newBlockImage = imagecreatetruecolor($widthOfBlock,$heightOfBlock);
    $backStitchColor = imagecolorallocate($newBlockImage, 0, 0, 0);
    // insert caption????
    //draw grid lines
    //$newBlockImage = addGridToImage($newBlockImage);

    //keep track of where to start next cell top left
    $blockX=0; 
    $blockY=0;

    for($y = $startY; $y < $endY; $y++){ //for each pixel in height, move down 1 "row" each iteration
      //echo "<tr>";
      for($x = $startX; $x < $endX; $x++){ // "draws" a row (for each y pixel)
        //rgb(75, 90, 60)
        //x and y are column and row #'s        
        list($r, $g, $b) = getRGBs($stitchChartArray[$y][$x][0]); //get the rgb values for the cell
        $stitchColor = imagecolorallocate($newBlockImage, $r, $g, $b);

        //calculate x & y start positons
        $stitchStartX=($blockX*SYMBOL_SIZE)+$blockX+1; //account for each previous stitch and the grid line, then add one for new grid line
        $stitchStartY=($blockY*SYMBOL_SIZE)+$blockY+1;
        $stitchEndX=$stitchStartX+(SYMBOL_SIZE);
        $stitchEndY=$stitchStartY+(SYMBOL_SIZE);

        /* make a symbol cell image with/without color and save it in the cache */
        if(!isset($stitchCache[$r][$g][$b]))
        {
            //create new image
            $stitchCache[$r][$g][$b] = imagecreatetruecolor(SYMBOL_SIZE,SYMBOL_SIZE);
            $stitchCacheColor = imagecolorallocate($stitchCache[$r][$g][$b], $r, $g, $b);
            //draw colored rectangle
            imagefilledrectangle($stitchCache[$r][$g][$b], 0, 0, SYMBOL_SIZE-1, SYMBOL_SIZE-1, $stitchCacheColor);
            //add the symbol
            $symbolFile=$stitchChartArray[$y][$x][1];
            if($symbolFile){
                $symbolImage = imagecreatefrompng($symbolFile);
                imagecopyresampled ($stitchCache[$r][$g][$b],$symbolImage,0,0,0,0,SYMBOL_SIZE-1,SYMBOL_SIZE-1,imagesx($symbolImage), imagesy($symbolImage) );
                imagedestroy($symbolImage);
            }
        }

        //add image from cache to the block image
        imagecopyresampled ($newBlockImage,$stitchCache[$r][$g][$b],$stitchStartX, $stitchStartY,0,0,SYMBOL_SIZE,SYMBOL_SIZE,SYMBOL_SIZE,SYMBOL_SIZE);

        //add the backstitch lines(borders)
        if($stitchChartArray[$y][$x][2]>1) //top
        {
            imagefilledrectangle($newBlockImage, $stitchStartX, $stitchStartY, $stitchEndX, $stitchStartY+1, $backStitchColor);
        }
        if($stitchChartArray[$y][$x][3]>1) //right
        {
            imagefilledrectangle($newBlockImage, $stitchEndX-1, $stitchStartY, $stitchEndX, $stitchEndY, $backStitchColor);
        }
        if($stitchChartArray[$y][$x][4]>1) //bottom
        {
            imagefilledrectangle($newBlockImage, $stitchStartX, $stitchEndY-1, $stitchEndX, $stitchEndY, $backStitchColor);
        }
        if($stitchChartArray[$y][$x][5]>1) //left
        {
            imagefilledrectangle($newBlockImage, $stitchStartX, $stitchStartY, $stitchStartX+1, $stitchEndY, $backStitchColor);
        }

       //advance x position
       $blockX++;
      }
       //advance y position
      //reset x
      $blockX=0;
      $blockY++;
    }

    imagejpeg($newBlockImage, $saveTo);
    imagedestroy($newBlockImage);


//dump stitch cache
foreach($stitchCache as $r)
{
    foreach($r as $g)
    {
        foreach($g as $b=>$data)
        {
            imagedestroy($data);
        }
    }
}
        return $saveTo;
    }

1 个答案:

答案 0 :(得分:1)

我会通过获得一个好的IDE和一个调试器来开始(如果你还没有),因为这些将是非常宝贵的工具。在这种情况下,您可以使用分析器来确定使用内存的位置。如果没有那些好的'手动调试代码,那就说

$memory = memory_get_usage();

作为内循环内的第一行。然后,当您逐步使用调试器时,您将能够看到内存在不断增加的位置。

顺便说一句,使用全局变量通常不是一个好主意。您可能希望将$ threadColours作为参数传递,或者查看将该数据导入函数的其他方法。

相关问题