cli icon indicating copy to clipboard operation
cli copied to clipboard

sf data query --bulk throws Fatal Error Javascript Heap out of memory

Open AllanOricil opened this issue 2 years ago • 7 comments

Is your feature request related to a problem? Please describe.

Bulk query stores all records in memory, instead of writing them to disk

image src: https://github.com/jsforce/jsforce/blob/95932f288a59a0b68ff9fe8cc1a94523a4d0fb85/src/api/bulk.ts#L1077-L1099

image src: https://github.com/jsforce/jsforce/blob/95932f288a59a0b68ff9fe8cc1a94523a4d0fb85/src/api/bulk.ts#L1244-L1264

this could throw an exception like Process out of Memory Exception if someone tries to export a few million records, because all records are stored in memory inside of a record[] array. For instance,

sf data query --query "SELECT Id FROM Contact" --bulk

image src: https://github.com/salesforcecli/plugin-data/blob/b56e778f6c9ef8affbc0719cafb629b07d19ce2c/src/commands/data/query.ts#L135-L153

This type of exception can't be captured by a try/catch. The node runtime would exit 1 straight away.

This issue does not happen when using jsforce bulk v1 implementation because it was developed with streams in mind.

image src: https://github.com/jsforce/jsforce/blob/95932f288a59a0b68ff9fe8cc1a94523a4d0fb85/src/api/bulk.ts#L959-L983

What are you trying to do Export a few million records using sfdx force:data:query --bulk

Describe the solution you'd like

1 . jsforce must accept a write stream so that memory could be released after its data is written to disk. Additionally, memory can be released when this.records[] reaches N kbs of data. 2. sfdx should create a write stream when the -b flag is true.

Describe alternatives you've considered N/a

Additional context N/a

AllanOricil avatar Mar 12 '23 22:03 AllanOricil

Thank you for filing this feature request. We appreciate your feedback and will review the feature at our next grooming or sprint planning session. We prioritize feature requests with more upvotes and comments.

github-actions[bot] avatar Mar 12 '23 22:03 github-actions[bot]

This issue has been linked to a new work item: W-12677563

git2gus[bot] avatar Mar 12 '23 22:03 git2gus[bot]

@mshanemc @RodEsp I think you could take a look at this since you are maintaining jsforce 2.0

AllanOricil avatar Mar 12 '23 22:03 AllanOricil

Heya @AllanOricil, I'm not involved with JSForce anymore but I'm sure Shane can take a look.

RodEsp avatar Mar 12 '23 22:03 RodEsp

Experiment with 103k Product2 records

image

image

This is the result when using the default heap size image

This is the result after configuring the heap size to 128Mb image

image

AllanOricil avatar Mar 16 '23 01:03 AllanOricil

Thanks for this also @AllanOricil, I'll add this to a JSForce category in our back log

iowillhoit avatar Mar 21 '23 21:03 iowillhoit

These PRs fix this bug

AllanOricil avatar Mar 27 '23 21:03 AllanOricil