Skip to content

Commit 66ae90f

Browse files
authored
[wasm] Improve handling OOM error (#104963)
Improve handling OOM error
1 parent 3f607bd commit 66ae90f

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

src/mono/browser/runtime/loader/run.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ async function initializeModules (es6Modules: [RuntimeModuleExportsInternal, Nat
486486
await configureRuntimeStartup(emscriptenModule);
487487
loaderHelpers.runtimeModuleLoaded.promise_control.resolve();
488488

489-
emscriptenFactory((originalModule: EmscriptenModuleInternal) => {
489+
const result = emscriptenFactory((originalModule: EmscriptenModuleInternal) => {
490490
Object.assign(emscriptenModule, {
491491
ready: originalModule.ready,
492492
__dotnet_runtime: {
@@ -496,6 +496,12 @@ async function initializeModules (es6Modules: [RuntimeModuleExportsInternal, Nat
496496

497497
return emscriptenModule;
498498
});
499+
result.catch((error) => {
500+
if (error.message && error.message.toLowerCase().includes("out of memory")) {
501+
throw new Error(".NET runtime has failed to start, because too much memory was requested. Please decrease the memory by adjusting EmccMaximumHeapSize. See also https://aka.ms/dotnet-wasm-features");
502+
}
503+
throw error;
504+
});
499505
}
500506

501507
async function downloadOnly ():Promise<void> {

src/mono/browser/runtime/types/internal.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ export type RuntimeModuleExportsInternal = {
507507
}
508508

509509
export type NativeModuleExportsInternal = {
510-
default: (unificator: Function) => EmscriptenModuleInternal
510+
default: (unificator: Function) => Promise<EmscriptenModuleInternal>
511511
}
512512

513513
export type HybridGlobalizationModuleExportsInternal = {

src/mono/wasm/features.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ WebSocket support in NodeJS hosts requires the `ws` npm package.
7171
### Initial Memory Size
7272
By default the .NET runtime will reserve a small amount of memory at startup, and as your application allocates more objects the runtime will attempt to "grow" this memory. This growth operation takes time and could fail if your device's memory is limited, which would result in an application error or "tab crash".
7373

74-
To reduce startup time and increase the odds that your application will work on devices with limited memory, you can set an initial size for the memory allocation, based on an estimate of how much memory your application typically uses. To set an initial memory size, include an MSBuild property like `<EmccInitialHeapSize>16777216</EmccInitialHeapSize>`, where you have changed the number of bytes to an appropriate value for your application. This value must be a multiple of 16384.
74+
To reduce startup time and increase the odds that your application will work on devices with limited memory, you can set an initial size for the memory allocation, based on an estimate of how much memory your application typically uses. To set an initial memory size, include an MSBuild property like `<EmccInitialHeapSize>16777216</EmccInitialHeapSize>`, where you have changed the number of bytes to an appropriate value for your application. This value must be a multiple of 16384. The default value is `2,147,483,648 bytes`, which may be too large and result in the app failing to start, because the browser refuses to grant it.
7575

7676
This property requires the [wasm-tools workload](#wasm-tools-workload) to be installed.
7777

0 commit comments

Comments
 (0)