Home  >  Article  >  Backend Development  >  是不是收到了1G的http post数据,这个进程占用的内存就会有1G?

是不是收到了1G的http post数据,这个进程占用的内存就会有1G?

WBOY
WBOYOriginal
2016-06-17 08:32:131400browse

如果内存只有1G,接下来又会发生什么,比如php,死机?
如果是node.js,单进程本来就有1G的内存限制。又会发生什么,崩溃?

回复内容:

PHP 貌似确实会把接收到的 request body 放到内存里。所以为避免麻烦,php.ini 里通常会配置最大 request body size 限制,默认通常是 2MB、 4MB 之类的,总之不大 。超过这个限制的请求会直接被 413 掉。

而且如果用 node.js 的话,这个应该取决于实现 http server 的人自己的设定。比如说我完全可以把接收到的数据写到硬盘里,以免占用内存:
<code class="language-js"><span class="nx">http</span><span class="p">.</span><span class="nx">createServer</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">req</span><span class="p">,</span> <span class="nx">res</span><span class="p">)</span> <span class="p">{</span>
  <span class="kd">var</span> <span class="nx">fd</span> <span class="o">=</span> <span class="nx">fs</span><span class="p">.</span><span class="nx">openSync</span><span class="p">(</span><span class="s1">'/tmp/'</span> <span class="o">+</span> <span class="nb">Math</span><span class="p">.</span><span class="nx">random</span><span class="p">(),</span> <span class="s1">'w+'</span><span class="p">);</span>
  <span class="nx">req</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s1">'data'</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">chunk</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">fs</span><span class="p">.</span><span class="nx">writeSync</span><span class="p">(</span><span class="nx">fd</span><span class="p">,</span> <span class="nx">chunk</span><span class="p">);</span>
  <span class="p">});</span>
  <span class="nx">req</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s1">'end'</span><span class="p">,</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
    <span class="nx">callbackWithRequestBody</span><span class="p">(</span><span class="nx">fd</span><span class="p">);</span>
    <span class="nx">res</span><span class="p">.</span><span class="nx">end</span><span class="p">();</span>
  <span class="p">});</span>
<span class="p">});</span>
</code>
nodejs 的 buffer 内存似乎是另算的,另外这种超大文件上传一般都是流式处理,读多少写多少,内存占用不算高。 可以用流式处理获取到请求的流,慢慢读慢慢写就好了,node server获取到的request本身就是实现了readable stream的东西
详见Stream Node.js v5.1.1 Manual & Documentation
大部分的web框架只是封装了让你不直接接触到请求body的那个网络流。 不是的,都是流的形式,http/tcp底层都是TCP协议,读写都有一定的缓冲区,数据或文件通过流的形式不停写到本地
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn