Home > Article > Backend Development > 是不是收到了1G的http post数据,这个进程占用的内存就会有1G?
如果内存只有1G,接下来又会发生什么,比如php,死机?
如果是node.js,单进程本来就有1G的内存限制。又会发生什么,崩溃?
<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的东西