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

Don't try to grow the buffer more than allowed by the system #75

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

targos
Copy link
Member

@targos targos commented Feb 1, 2025

  • refactor: move endianness check to separate file
  • fix: don't try to grow the buffer more than allowed by the system

Closes: image-js/fast-png#42

Copy link

codecov bot commented Feb 1, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (9a39938) to head (0d1486a).

Additional details and impacted files
@@            Coverage Diff            @@
##              main       #75   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            2         3    +1     
  Lines          337       378   +41     
  Branches        75        84    +9     
=========================================
+ Hits           337       378   +41     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@targos
Copy link
Member Author

targos commented Feb 1, 2025

I don't know how to reliably detect what is the maximum size for a typed array. Since Node.js 22, there is no arbitrary limit in V8.
On macOS, what I pushed here works because the memory is not really allocated (it's in the virtual space), but on Linux it's real and makes the process crash when we try too high.

@lpatiny Any idea?

@lpatiny
Copy link
Member

lpatiny commented Feb 1, 2025

Maybe using on node

> require('v8').getHeapStatistics()
{
  total_heap_size: 543752192,
  total_heap_size_executable: 524288,
  total_physical_size: 543752192,
  total_available_size: 3804536944,
  used_heap_size: 541746816,
  heap_size_limit: 4345298944,
  malloced_memory: 278632,
  peak_malloced_memory: 874496,
  does_zap_garbage: 0,
  number_of_native_contexts: 1,
  number_of_detached_contexts: 0,
  total_global_handles_size: 12800,
  used_global_handles_size: 4512,
  external_memory: 2312309
}
> var b=new Array(2**26).fill(0).map(Math.random)
undefined
> require('v8').getHeapStatistics()
{
  total_heap_size: 3260137472,
  total_heap_size_executable: 524288,
  total_physical_size: 3260137472,
  total_available_size: 1102417256,
  used_heap_size: 3225912760,
  heap_size_limit: 4345298944,
  malloced_memory: 278632,
  peak_malloced_memory: 874496,
  does_zap_garbage: 0,
  number_of_native_contexts: 1,
  number_of_detached_contexts: 0,
  total_global_handles_size: 12800,
  used_global_handles_size: 4608,
  external_memory: 2312293
}

and in the browser

performance.memory.jsHeapSizeLimit
performance.memory.totalJSHeapSize

Actually this is something very important also in nmr-load-save so if we have a package that is node / browser compatible it would be very usefull.

@targos
Copy link
Member Author

targos commented Feb 1, 2025

I'm not sure the js heap size is relevant. ArrayBuffer data are notably not counted in it.

@targos
Copy link
Member Author

targos commented Feb 1, 2025

And in browsers, the typed array size limit is unrelated to the memory limit. It's arbitrarily chosen by the browser.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

RangeError: Array buffer allocation failed
2 participants