duanlou2917 2016-07-24 13:53
浏览 116
已采纳

了解MySQL的内存使用情况导致PHP(PDO)

I am trying to understand why the memory usage for a single PDO result is so high. Here are a few things to know about the query/result:

  • I am pulling a single VARCHAR(6) column from a single table
  • The result is less than 30K rows
  • This fetching this result uses ~12MB of memory in PHP (source: memory_get_usage)
  • If I json_encode the result and dump it to a file, the actual data (in text form) is only ~1MB
  • Using PHP7, MySQL 5.7, deployed on Ubuntu 14.04.

My question is, where exactly does the 11MB of bloat come in? If the actual data in text form is only about 1MB, then 11MB seems like a lot of overhead just to parse the data in PHP. Is there a reason for this? Am I missing something?

Edit:

Just to clarify, I am looking for a technical explanation as to why the bloat exists, not a workaround for the issue.

  • 写回答

2条回答 默认 最新

  • doudi1979 2016-07-25 01:21
    关注

    Ok, so thanks to Ollie Jones's answer, I realized I've been looking in the wrong place for my information. This isn't a PDO memory usage issue but an issue with the way PHP stores its arrays. (Maybe issue isnt the right word, it is what it is)

    After a bit of digging I found this incredibly helpful article which gives a great breakdown of how PHP allocates memory for array elements:

    https://nikic.github.io/2011/12/12/How-big-are-PHP-arrays-really-Hint-BIG.html

    Spoiler alert, it uses a TON of memory for each element. Apparently it has gotten a lot better in PHP7. The article states that for a simple integer array (in PHP5) element it will use about 18 times the more memory than the size of the integer itself. Since i'm seeing about a 12* increase on associative string data, id say that is indeed a vast improvement over PHP5.

    According to the article, memory is being allocated for the following:

    • zvalue_value union, which relates to the weak typecasting that PHP allows
    • Type storage and garbage collection data
    • The Zend Memory Manager allocation
    • Hash table buckets

    If you had some interest in this as well, I highly recommend reading that article, its a quick read and has a lot of great information.

    Thanks again to Ollie Jones for pointing me in the right direction.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥500 求华为P30PRO手机硬盘数据恢复
  • ¥15 关于#vscode#的问题:ESP32开发板对接MQTT实现小灯泡的开关
  • ¥15 TMC2209串口模式下读取不到寄存器的值串口助手蓝色字体是发过去的消息,绿色字体是收到的消息,第二行发送读取寄存器的指令但是没有读取到寄存器的值串口助手如下图:接线如下图,如何解决?
  • ¥15 高通安卓11提取完整线刷包软件,或者优博讯dt50顺丰刷机包
  • ¥20 C,有个译码器,换了信道就跑不出原来数据
  • ¥15 MIMIC数据库安装问题
  • ¥60 基于JTag协议开发Fpga下载器上位机,哪位大🐂有偿指导?
  • ¥20 全书网Java爬取数据
  • ¥15 怎么获取红包封面的原始链接,并且获取红包封面序列号
  • ¥100 微信小程序跑脚本授权的问题