How can I use var_dump + output buffering without memory errors?

前端 未结 5 1840
迷失自我
迷失自我 2021-01-01 09:00

I\'m using a debugging aid in an application that uses var_dump() with output buffering to capture variables and display them. However, I\'m running into an iss

5条回答
  •  难免孤独
    2021-01-01 09:22

    I do not believe that there is any way to determine how much memory a specific function will eventually take up. One thing you can do is use memory_get_usage() to check how much memory the script is currently taking right before $largeVar is set, then compare it with the amount after. This will give you a good idea of the size of $largeVar, and you can run trials to determine what a maximum acceptable size limit would be before you exit gracefully.

    You could also reimplement the var_dump() function yourself. Have the function walk through the structure and echo the resulting content as it is generated, or store it in a temp file, rather that storing a gigantic string in memory. This will allow you to get the same desired result, but without the memory problems you are encountering.

提交回复
热议问题