Skip to content

Commit

Permalink
[wasm] Improve handling OOM error (#104963)
Browse files Browse the repository at this point in the history
Improve handling OOM error
  • Loading branch information
mkhamoyan authored Jul 17, 2024
1 parent 3f607bd commit 66ae90f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/mono/browser/runtime/loader/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ async function initializeModules (es6Modules: [RuntimeModuleExportsInternal, Nat
await configureRuntimeStartup(emscriptenModule);
loaderHelpers.runtimeModuleLoaded.promise_control.resolve();

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

return emscriptenModule;
});
result.catch((error) => {
if (error.message && error.message.toLowerCase().includes("out of memory")) {
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");
}
throw error;
});
}

async function downloadOnly ():Promise<void> {
Expand Down
2 changes: 1 addition & 1 deletion src/mono/browser/runtime/types/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ export type RuntimeModuleExportsInternal = {
}

export type NativeModuleExportsInternal = {
default: (unificator: Function) => EmscriptenModuleInternal
default: (unificator: Function) => Promise<EmscriptenModuleInternal>
}

export type HybridGlobalizationModuleExportsInternal = {
Expand Down
2 changes: 1 addition & 1 deletion src/mono/wasm/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ WebSocket support in NodeJS hosts requires the `ws` npm package.
### Initial Memory Size
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".

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.
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.

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

Expand Down

0 comments on commit 66ae90f

Please sign in to comment.