3

I am making an app, which needs a lot of GPU memory sometimes (many huge textures). I could make a system, where I keep frequently used textures in GPU memory, and I upload and delete the rest, only when they are needed.

In general, there are always more textures than the GPU memory, and the more memory I can use - the faster my program is. I don't want to restrict myself e.g. to 100 MB or 1 GB of memory, when there could be 4x more memory free to use. But if I try to allocate too much, the browser will kill my program.

I see, that in WebGL, there is no direct way to tell, how much memory is available. What would be your strategy, how to solve such issue?

Ivan Kuckir
  • 2,327
  • 3
  • 27
  • 46
  • Don't do that! ............ sorry I don't have a better answer. The browser in general doesn't handle out of memory in Javascript either, the page just crashes. That's better than the whole browser crashing but there's no way to detect it that I know of. The same with WebGL. The best I can think of is track your own memory usage and associate that with info from `WEBGL_debug_renderer_info`. Send it back to your server and keep a DB how how much you can allocate on each device. Sorry I don't know another way. – gman Aug 20 '17 at 08:29
  • Possible duplicate of [jQuery or javascript to find memory usage of page](https://stackoverflow.com/questions/2530228/jquery-or-javascript-to-find-memory-usage-of-page) – Nikola Lukic Aug 28 '17 at 07:18
  • Make revision at whole project . You can't prevent this bug with looking at memory usage . You will need to find where is the main bug. Check all for each or loop's also optimise texture's size and other big data that you have. – Nikola Lukic Aug 28 '17 at 07:21
  • @gman 99% of RAM in my app is used by ArrayBuffers, and it throws an error when there is not enough RAM for a new ArrayBuffer. I catch it and adapt to it. There has to be a way to do it for WebGL. Even if there isn't right now, it has to be created. – Ivan Kuckir Aug 28 '17 at 07:57
  • @NikolaLukic What bug do you mean? In my program or in a browser? It is obvious, that once I start creating textures, the memory will run out eventually, I just want to be able to detect it. – Ivan Kuckir Aug 28 '17 at 07:59
  • For me if I just keep allocating `ArrayBuffer`s the browser eventually just crashes the tab: https://jsfiddle.net/greggman/s7czs0mq/1/ I know of no part of the spec that suggests running out of memory will throw an exception. Because of the way JavaScript works almost everything you do requires memory so if you're actually out of memory there is no way for JavaScript to recover. As an example the Exception object itself requires memory to be allocated. – gman Aug 28 '17 at 09:08
  • @gman I understand what you mean. I just want to be sure, so I reported a bug to Chrome: https://bugs.chromium.org/p/chromium/issues/detail?id=759503 – Ivan Kuckir Aug 28 '17 at 10:19
  • You mis-understood. They *can't* fix that issue by definition of JavaScript. In order to throw an exception you need memory. So if you're out of memory how to do you allocate more memory to throw the exception. It gets worse, that exception will run code (all the catch blocks). Every one of those blocks could also allocate memory and/or could try to throw another exception but there's no memory to allocate another exception object since you're out of memory. That's just the way JavaScript works (and probably most GC languages) – gman Aug 28 '17 at 16:09
  • @gman Yes, I understand how JS works and that it can not be fixed. But maybe they could make it work for large ArrayBuffers only (not enough memory for a big arraybuffer, but still enough to throw an exception). When so much much memory is needed, authors are probably making some performance-critical program, and they probably use ArrayBuffers for it. – Ivan Kuckir Aug 28 '17 at 23:16
  • @gman that's not a good design and could be solved, they could just throw an error when you reach 99% or close to that number so it allows you to gracefully handle it. – Totty.js Jan 13 '19 at 07:23
  • Throwing an error requires memory. If there's no memory left there's no memory left to make space for the exception itself. How much memory is undefined since there's no way to know what a developer is going to do `try {} catch(e) { doSometingThatRequiresMemoryWhichIsEverythingInJS(); }` – gman Jan 13 '19 at 08:02

0 Answers0