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条)

报告相同问题?

悬赏问题

  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码