V8 (the JavaScript engine node is built upon) until very recently had a hard limit on heap size of about 1.9 GB.
Node v0.10 is stuck on an older version of V8 (3.14) due to breaking V8 API changes around native addons. Node 0.12 will update to the newest V8 (3.26), which will break many native modules, but opens the door for the 1.9 GB heap limit to be raised.
So as it stands, a single node process can keep no more than 1.9 GB of JavaScript code, objects, strings, etc combined. That means the maximum length of a string is under 1.9 GB.
You can get around this by using Buffer
s, which store data outside of the V8 heap (but still in your process's heap). A 64-bit build of node can pretty much fill all your RAM as long as you never have more than 1.9 GB of data in JavaScript variables.
All that said, you should never come anywhere near this limit. When dealing with this much data, you must deal with it as a stream. You should never have more than a few megabytes (at most) in memory at one time. The good news is node is especially well-suited to dealing with streaming data.
你应该问自己几个问题:
- What kind of data are you actually receiving from the user?
- 为什么要以JSON格式存储它?
- 把千兆字节塞进JSON真的是个好主意吗?(答案是否定的.)
- What will happen with the data later, after it is stored? Will your code read it? Something else?
你发布的问题实际上是相当模糊的,与你真正想要实现的目标有关.要获得更具体的建议,请使用更多信息更新您的问题.
如果您希望数据永远不会那么大,只需在输入上抛出10MB或更大的合理限制,将其全部缓冲,然后使用JSON.stringify
.
If you expect to deal with data any larger, you need to stream the input straight to disk. Look in to transform streams if you need to process/modify the data before it goes to disk. For example, there are modules that deal with streaming JSON.