How to trace PHP errors?

2019-02-19 00:12发布

问题:

I get the following error:

[23-Feb-2011 19:51:29] PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 512 bytes) in /obj/class.db.php on line 60

My problem is that I don't know where this error happens. Line 60 of class.db.php is one of the most used functions in my application (Database Read() function).

How can I trace back where this error originates from?

回答1:

You can try using http://www.xdebug.org/



回答2:

That's most likely exactly where the problem occurred - your script is sucking up almost all of the 128megabyte limit, and then the database class tries to allocate a bit more space to read something from the database, the limit gets crossed and the script barfs.

What are you doing that requires using 128meg of memory?



回答3:

If you're running out of memory while selecting data from a database, chances are you're trying to grab way too much data at once from the DB and trying to load it all into memory. Does your Database Read() method do something like automagically fetch the data and get it all into a variable for you?

You should probably consider incrementally processing the data, as an alternative to grabbing it all at once, or work with smaller datasets. On the other hand, if you have the ability to do so and you really need to process so much data at once, you could consider changing the memory limit and increasing it using ini_set('memory_limit', '256M'); to increase it to 256MB, for example.

See:

http://php.net/manual/en/ini.core.php#ini.memory-limit and http://php.net/ini_set

Edit:

For figuring out the trace of how you got to this point, Xdebug is probably your best bet as far as free solutions are concerned. I'd take a look at: http://www.xdebug.org/docs/execution_trace

The xdebug.show_mem_delta option will probably be the most beneficial to you for figuring out what's spiking your memory usage when looking at the stack traces.



回答4:

In php you can set your own exception handler and error handler, see also documentation on php.net. When an error occurs you can print backtrace and dump that to file or screen.

Look at the php functions:

set_exception_handler()
set_error_handler()
debug_backtrace();

Hope it helps, Greetz, Erwin Haantjes



回答5:

You can try using PHP Error to get a stack trace for errors.