iobuffer icon indicating copy to clipboard operation
iobuffer copied to clipboard

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

Open targos opened this issue 1 year ago • 5 comments

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

Closes: https://github.com/image-js/fast-png/issues/42

targos avatar Feb 01 '25 11:02 targos

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

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     

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar Feb 01 '25 11:02 codecov[bot]

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?

targos avatar Feb 01 '25 11:02 targos

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.

lpatiny avatar Feb 01 '25 15:02 lpatiny

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

targos avatar Feb 01 '25 15:02 targos

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

targos avatar Feb 01 '25 15:02 targos