Skip to content
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

fix: improve memory usage on NVD update #6321

Merged

Conversation

markitovtr1
Copy link
Contributor

@markitovtr1 markitovtr1 commented Dec 18, 2023

Fixes Issue

Fix high memory usage when updating NVD database.

Before:
image

After:
image

Description of Change

Just moved BufferedInputStream to wrap GzipInputStream and not the other way around.

Test description

I did not test this extensively. I lost some hours finding a way to run this, so sorry if I did not do this as it should.

  1. Download NVD cache generated from vulnz and create a local HTTP server to serve it (remove Download latency from equation)
  2. Load project via Eclipse
  3. Add two run configurations on CLI module pointing to local HTTP server for data feed: one with --purge and other with --updateonly
  4. Monitor memory usage when running --updateonly after database is purged

How I got to that point of error?

On NvdApiDatasource, I saw that there were two ExecutorServices when using Datafeed, so first I limited Download to 1 thread and saw no difference in memory usage and then I limited processingExecutorService to 1 and saw a huge difference.

Further improvements

I believe first gunzipping to a temporary file and reading from it would save a lot of memory. Because GzipInputStream has an internal buffer, as Gzip decompression works in blocks, and JSON usually has a high compression rate (around 80 to 95% in some cases, but in this case I saw around 90%), there is a lot of gunzipped content in memory probably. Not sure if BufferedInputStream is enough though, so I did not bother to test that.

Have test cases been added to cover the new functionality?

No

Signed-off-by: Marcos Romero <marcos.romero@mercadoderecebiveis.com.br>
@boring-cyborg boring-cyborg bot added the core changes to core label Dec 18, 2023
Copy link
Owner

@jeremylong jeremylong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jeremylong jeremylong added this to the 9.0.7 milestone Dec 18, 2023
@jeremylong jeremylong merged commit 667fde1 into jeremylong:main Dec 18, 2023
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core changes to core
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants