-
Notifications
You must be signed in to change notification settings - Fork 30k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error when writing huge of content to a file json #13465
Comments
Could it be related to String size limit? See #9489 What is String length you try to output in JSON? |
@vsemozhetbyt The JSON file is more than 500mb when I carry out writing |
Could you provide a small code fragment to reproduce the issue? |
cc @nodejs/buffer ? |
Yes, you are either hitting the string length limit (which I thought was 256 MB?), or are going out of memory, neither of which are very unlikely when handling strings this huge. I agree with @vsemozhetbyt, in order to see if we are even able to help you, you’d need to provide enough code to understand exactly what is going on. You might also want to look into the possibility of using a streaming JSON module to handle your data. |
Sounds reasonable, yes. Maybe we should also export V8’s |
Will this allow to increase the String limit for v8? It is hard for me to assess the effects. cc @nodejs/v8 ? |
@vsemozhetbyt I’m talking about just providing the value to JS scripts, not to make it mutable (I have no idea if that would work). That might be useful for giving programmers a way to tell whether something can even be turned into a string, or whether it’s too large. |
Why is this limit in place? Why doesn't v8 allow arbitrary strings as long as it does not run out of heap space? |
That’s really a question for @nodejs/v8. ;) |
Maybe we should also foresee the cases when using this check is not so obvious. For example: fs.writeFileSync('file.json', JSON.stringify(hugeObject), 'utf16le'); Maybe some checks should be carried out by |
@vsemozhetbyt I don’t quite get your example here. By the time |
@addaleax I just mean that sometimes a user cannot even see an intermediate String issue to think of a limit check. Yes, maybe this is not the best example. Then how a user can use a check? Should the buffer size be checked? How a user can check if |
I don’t think that’s possible. :/ In that case, a streaming JSON generator might be the only solution that allows you to be somewhat sure that everything works. |
Then the only use of proposed |
@vsemozhetbyt There are a lot of other ways to generate long strings but that’s what I’m having in mind, yes |
This does not really affect the problem @trungducng has, right? If the result of |
FWIW: console.log('*'.repeat(Math.pow(2, 28) - 16).length);
console.log(JSON.stringify('*'.repeat(Math.pow(2, 28) - 16 - 1)).length); 268435440
test.js:5
console.log(JSON.stringify('*'.repeat(Math.pow(2, 28) - 16 - 1)).length);
^
RangeError: Invalid string length
at JSON.stringify (<anonymous>)
at Object.<anonymous> (test.js:5:18)
at Module._compile (module.js:569:30)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
at Function.Module._load (module.js:458:3)
at Function.Module.runMain (module.js:605:10)
at startup (bootstrap_node.js:158:16)
at bootstrap_node.js:575:3 Maybe some concatenations of JSON strings have a place. |
@vsemozhetbyt I think that would work if you use |
What seems to fail is converting the string returned by |
Yes, I've just confimed what @tniessen said) This is OK: console.log('*'.repeat(Math.pow(2, 28) - 16).length);
console.log(JSON.stringify('*'.repeat(Math.pow(2, 28) - 16 - 2)).length); |
There is talk of raising it: https://bugs.chromium.org/p/v8/issues/detail?id=6148 |
Other VMs might use different max string length values. Is this something that could be communicated via n-api perhaps? |
I think it has at least one other useful side-effect, it would show up in the TOC, and the docs for it would make clear that there is a maximum. Without it, I'm not sure where in the docs the max string size docs would go. |
Add `buffer.constants`, containing length limits for `Buffer` and `string` instances. This could be useful for programmers to tell whether a value can be turned into a string or not. Ref: nodejs#13465
Add `buffer.constants`, containing length limits for `Buffer` and `string` instances. This could be useful for programmers to tell whether a value can be turned into a string or not. Ref: #13465 PR-URL: #13467 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Add `buffer.constants`, containing length limits for `Buffer` and `string` instances. This could be useful for programmers to tell whether a value can be turned into a string or not. Ref: #13465 PR-URL: #13467 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Add `buffer.constants`, containing length limits for `Buffer` and `string` instances. This could be useful for programmers to tell whether a value can be turned into a string or not. Ref: #13465 PR-URL: #13467 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Add `buffer.constants`, containing length limits for `Buffer` and `string` instances. This could be useful for programmers to tell whether a value can be turned into a string or not. Ref: #13465 PR-URL: #13467 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Add `buffer.constants`, containing length limits for `Buffer` and `string` instances. This could be useful for programmers to tell whether a value can be turned into a string or not. Ref: #13465 PR-URL: #13467 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Add `buffer.constants`, containing length limits for `Buffer` and `string` instances. This could be useful for programmers to tell whether a value can be turned into a string or not. Ref: #13465 PR-URL: #13467 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
I've been seeing this OOM crash repeatedly with far shorter JSON. My code writes a report of constant size periodically to disk, as data is being updated:
When it succeeds (most of the time), the size of the file (including 2-space indentation) is ~18MB, and he file is ~620,000 lines long. (I'd be happy to share if it weren't proprietary data.) However, one in about 150-250 tries, Node crashes with an Out of Memory error. I've attached a sample report. I'm running node v12.11.1 with |
This same bug kills Chromium 61 Android with a giant array. |
Error when writing huge of content to a file json, Can anyone help me solve this propblem? I tried
But it still help me about out of memory in heap problem
The text was updated successfully, but these errors were encountered: