php-general Digest 26 May 2008 04:40:18 -0000 Issue 5479

Topics (messages 274675 through 274682):

Re: Advantages of declared vs undeclared object properties
        274675 by: Nathan Nobbe

Re: array recursion from database rows
        274676 by: Wolf

Still need help with some kind of memory leak in my php  database program.  
General hints for php would help
        274677 by: Mary Anderson
        274679 by: Chris
        274680 by: Chris
        274681 by: Bastien Koert

Re: PHP + MySQL transactions
        274678 by: Chris

Image modifications
        274682 by: Ronald Wiplinger

Administrivia:

To subscribe to the digest, e-mail:
        [EMAIL PROTECTED]

To unsubscribe from the digest, e-mail:
        [EMAIL PROTECTED]

To post to the list, e-mail:
        [EMAIL PROTECTED]


----------------------------------------------------------------------
--- Begin Message ---
On Sun, May 25, 2008 at 3:51 AM, Weston C <[EMAIL PROTECTED]> wrote:

> In a setup like you've got with a SimpleXML object, where object
> properties aren't necessarily declared in the class definition but are
> added on an ad hoc basis, is there any performance hit?
>
> If not, other than the ability to mark properties as private, is there
> any other particular advantage to declaring them at the top of the
> class definition?


its often quite helpful so that other developers, and even yourself know
what exactly is going on inside a class.  dynamically added variables are
only visible at runtime, and therefore are difficult to understand during
design time.  SimpleXMLElement is exceptional, in that it has rather
specialized semantics in terms of its interface.  without such well defined
semantics, imagine just adding and retrieving variables to an object,
ad-hoc, all over the place; it could easily lead to a big mess.

-nathan

--- End Message ---
--- Begin Message ---
Bob,  

Post your failing code and we'l be glad to give you pointers on fixing it.  
This list doesn't exist to write code for you (unless your dropping coin into 
our paypal account) but all of us are willing to help with posted code.

Wolf

-----Original Message-----
From: Bob <[EMAIL PROTECTED]>
Sent: Saturday, May 24, 2008 2:25 PM
To: [EMAIL PROTECTED]
Subject: [PHP] array recursion from database rows

Hi.

I have a database table I have created for navigation.

The table fields are uid, parent_id, menu_name.

Each entry is either a top level element with a parent_id of 0 or a child
which has a parent_id that relates to the parent uid.

What I am trying to do is recurse through a set of rows adding the
child(ren) of a parent to a multi-dimensional array.

Does anyone know how I can do this, I've tried (unsuccessfully) to traverse
the rows to create this array but I keep failing miserably.

This is probably very easy for the people on this list so I am hoping
someone could help me out.

Thanks in advance.

Cheers.

Bob


-- 


[The entire original message is not included]


--- End Message ---
--- Begin Message ---
Hi,
Something in my program is eating up memory. I am running php 5 and hooking into postgres8.3, but I don't think it is a database program. The command in question works fine in psql, the problem comes in loading a php variable. And in the past, unsetting the variable has helped.

  The program itself is perhaps 1000 lines long, so I won't post it.
  I tried to isolate the problem with this code:




require_once('/hdir/0/maryfran/unproto/memdev/config_file.php'); // connects to database require_once('/hdir/0/maryfran/unproto/memdev/DMEM_stuff.php'); // has constants for app

function do_select_many($pg,
                       $select_cmd){
global $DMEM_ERROR, $DMEM_DEBUG;
    @pg_connection_status($pg) === PGSQL_CONNECTION_OK
      or die("Database connection bad.\n");
    $sql_result = @pg_query($pg, $select_cmd);
    if (pg_result_status($sql_result) != PGSQL_TUPLES_OK){
        if ($DMEM_DEBUG){
           echo pg_last_error($pg);
           echo "\n<br>\n";
           echo $select_cmd;
           echo "\n<br>\n";
       }
       return array($DMEM_ERROR);
    }
$values_hashed_by_column = pg_fetch_all($sql_result); ///WHERE THE APPLICATION BOMBS
    pg_free_result($sql_result);
    return $values_hashed_by_column;
}

function fu_bar($pg, $sql_cmd){
       $foo = do_select_many($pg,
                             $sql_cmd);
       return($foo);
}

$BIG_SELECT_cmd = " SELECT value,
       x.d8_dv_id,
       x.dm2_dv_id,
       x.dm1_dv_id,
       x.d5_dv_id,
       x.d7_dv_id,
       x.xp_data_batch_id,
       x.xp_sub_batch_id

FROM display.tmp_xprod_table x LEFT JOIN display.tmp_data_values v
ON  x.d8_dv_id = v.d8_dv_id
AND x.dm2_dv_id = v.dm2_dv_id
AND x.dm1_dv_id = v.dm1_dv_id
AND x.d5_dv_id = v.d5_dv_id
AND x.d7_dv_id = v.d7_dv_id
AND x.xp_data_batch_id = v.data_batch_id
AND x.xp_sub_batch_id = v.sub_batch_id
ORDER BY x.d8_value_rank,
         x.dm2_value_rank,
         x.dm1_value_rank,
         x.d5_value_rank,
         x.d7_value_rank,
         x.xp_dmem_status,
         x.xp_created_date ";


$many_selected = fu_bar($pg,
                        $BIG_SELECT_cmd);

// IF I RUN THIS THROUGH A LOOP:
// for($j=0;$j<20, $j++){ $many_selected[$jj] = fu_bar($pg, $BIG_SELECT_cmd)} THE TEST PROGRAM CRASHES so I think I have
more copies of the result of this query than I need.



?>
<p> <?php echo_submit('push_my_button', "Push"); ?></p> //Code which prints a submit button (<input type="submit">)


BIG_SELECT is the only command which pulls a significant amount of data (40K to 2M) into the database. In the real application, executing the function which runs this command results in an memory fault at the point in do_select_many that I have indicated. However, this piece of test code runs just fine! And the BIG_SELECT_cmd works just fine when cut and pasted directly into psql. Sometimes, even the real application will work just fine. I am stymied. I can neither isolate the problem in a piece of test code, nor can I reliably fix the problem when it occurs. Any help on debuggers which will find memory leaks in php code, (Yeah. PHP 5.2 has some!) or tricks of the trade in finding them, or tips on where to look would be greatly appreciated. At this point, I really do not know when and where php frees memory. Let me be a little more specific:

1. Does passing a large array as a reference into or out of a function save space?

2. Suppose I hit a submit button. Are all the variables on the page still taking up space?

3. Any known bugs with pg_fetch_all running on 8.3? How do I find them?

4. Suppose I follow a link. Do my variables on the old page still take up space?

        5.  Should I be handling my arrays one chunk at a time?

6. When I exit a php function, isn't all the space used by function variables freed?

7. Why would it sometimes work to clear the cache? This problem seems to me to be an intermittent one.

        8.  Most important,  what good programming practices should I be
following to keep this from happening?


Mary Anderson



--- End Message ---
--- Begin Message ---
> // IF I RUN THIS THROUGH A LOOP:
> // for($j=0;$j<20, $j++){ $many_selected[$jj] = fu_bar($pg,
> $BIG_SELECT_cmd)} THE TEST PROGRAM CRASHES  so I think I have
> more copies of the result of this query than I need.

This is just going to create a multi-dimensional array - with each
element being one result set - so yes that's going to take a lot of memory.


To debug your memory issue, see some ideas here:

http://www.davedevelopment.co.uk/2008/05/12/log-memory-usage-using-declare-and-ticks-in-php/

but instead of keeping a global variable (or session variable), log it
to a file instead:

http://www.davedevelopment.co.uk/2008/05/12/log-memory-usage-using-declare-and-ticks-in-php/#comment-52074

-- 
Postgresql & php tutorials
http://www.designmagick.com/

--- End Message ---
--- Begin Message ---
Mary Anderson wrote:
> Thanks for responding.
> 
> I hadn't realized memory_get_usage was available.  That will be an
> enormous help.
> 
> And I wasn't too clear in my note.  Actually, it did not surprise me
> that the loop crashed the test code.  I expected it to.  What does
> surprise me is that the test code works with one call to the data base,
> but the same logic in the application crashes the system.

<random guess> Maybe you're accidentally calling the same code multiple
times?

You can use xdebug to work out what code is called and where it's called
from.

See http://www.xdebug.org/

Extremely handy little tool that one :)

-- 
Postgresql & php tutorials
http://www.designmagick.com/

--- End Message ---
--- Begin Message ---
On Sun, May 25, 2008 at 7:53 PM, Mary Anderson <[EMAIL PROTECTED]>
wrote:

> Hi,
>  Something in my program is eating up memory. I am running php 5 and
> hooking into postgres8.3, but I don't think it is a database program.
> The command in question works fine in psql,  the problem comes in loading a
> php variable.  And in the past, unsetting the variable has helped.
>
>
>
> BIG_SELECT is the only command which pulls a significant amount of data
> (40K to 2M) into the database.  In the real application, executing the
> function which runs this command results in an memory fault at the point in
> do_select_many that I have indicated.  However, this piece of test code runs
> just fine!  And the BIG_SELECT_cmd works just fine when cut and pasted
> directly into psql.  Sometimes, even the real application will work just
> fine.
> I am stymied.  I can neither isolate the problem in a piece of test code,
> nor can I reliably fix the problem when it occurs.  Any help on debuggers
> which will find memory leaks in php code, (Yeah. PHP 5.2 has some!) or
> tricks of the trade in finding them, or tips on where to look would be
> greatly appreciated.
> At this point, I really do not know when and where php frees memory. Let me
> be a little more specific:
>
>   1.  Does passing a large array as a reference into or out of a function
> save space?
>
Yes as you are not creating a duplicate of the array, but working on the
original


>
>        2.  Suppose I hit a submit button.  Are all the variables on the
> page still taking up space?

see point 7


>
>
>        3.  Any known bugs with pg_fetch_all running on 8.3?  How do I find
> them?
>
>        4.  Suppose I follow a link.  Do my variables on the old page still
> take up space?
>
see point 7

>
>        5.  Should I be handling my arrays one chunk at a time?
>
Do you really need all that data at once? Take a step back and look at what
you are attempting to do, perhaps some kind of roll-up or cube query could
do what you want, if you are aggregating the results, normally the reason
for a large result set.

Could you look at caching the results over a time when the server is lightly
loaded? It is truly important to have up to the minute results in the
resulting page?

>
>        6.  When I exit a php function, isn't all the space used by function
> variables freed?
>
No, its freed when the page in its entirety is done processing


>
>        7.  Why would it sometimes work to clear the cache?  This problem
> seems to me to be an intermittent one.
>
If you have multiple sessions asking for the same data, it will likely cause
a crash as well

>
>        8.  Most important,  what good programming practices should I be
> following to keep this from happening?
>
Think before you code. Its sounds rude and trivial, but in my experience,
all of the real issues in the code performance are related to developers not
understanding the true goal, or just not seeing the implications their
decisions on the design.


>
>
> Mary Anderson
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


-- 

Bastien

Cat, the other other white meat

--- End Message ---
--- Begin Message ---
>> See http://dev.mysql.com/doc/refman/5.0/en/savepoints.html
>>
>> The situation might not come up but it can't hurt to have it already
>> built in "just in case".
> 
> This doesn't appear deal with *nested transactions.* It appears that it
> will use a single transaction and you can just save up to a certain
> point upon failure. As of this point, it's all or nothing b/c we are
> dealing with fairly crucial information. However, I will look into
> building savepoints into the class so that I'll have that option in the
> future.

Sounds like a difference in semantics :P In my head they're the same
thing - if the middle bit fails, it'll roll back. If it works, you
continue doing whatever you're doing. If everything succeeds then you
commit everything at once. If part of something fails, then you roll it
all back. At least you're more protected in the situation I mentioned
above where you'd get partial data saved.

-- 
Postgresql & php tutorials
http://www.designmagick.com/

--- End Message ---
--- Begin Message ---
I would like to find some samples to start with.

We want to upload a picture and the user may apply some "filters" or
"instructions" to create a new picture, based on the uploaded picture and
the available "filters" and "instructions".

The idea of it is not really mature, since we have no idea where to start
and what is possible to do.

bye

R.

--- End Message ---

Reply via email to