Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/bs_electron_ci_ec2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ jobs:

- name: Start EC2 runner
id: start-ec2-runner
uses: brightsign/ec2-github-runner@0fa8b183dd4124fd191ccdbc48b68f0ea46a9634
uses: brightsign/ec2-github-runner@3ef5e07bd58c60757b26c39b8cf25f234df7b974
with:
mode: start
github-app-private-key: ${{ secrets.GH_APP_PRIVATE_KEY }}
Expand Down Expand Up @@ -98,11 +98,11 @@ jobs:
aws-region: ${{ inputs.aws_region }}

- name: Stop EC2 runner
uses: brightsign/ec2-github-runner@0fa8b183dd4124fd191ccdbc48b68f0ea46a9634
uses: brightsign/ec2-github-runner@3ef5e07bd58c60757b26c39b8cf25f234df7b974
with:
mode: stop
github-app-private-key: ${{ secrets.GH_APP_PRIVATE_KEY }}
github-app-id: 287690
label: ${{ needs.start-runner.outputs.label }}
ec2-instance-id: ${{ needs.start-runner.outputs.ec2-instance-id }}
leave-ec2-instance-running: ${{ inputs.leave_ec2_instance_running }}
leave-ec2-instance-running: ${{ inputs.leave_ec2_instance_running }}
80 changes: 80 additions & 0 deletions docs/api/memory-pressure-monitor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# memoryPressureMonitor

> Monitor and trigger system memory pressure events.

Process: [Main](../glossary.md#main-process)

## Events

The `memoryPressureMonitor` module emits the following events:

### Event: 'memory-pressure'

Returns:

* `level` string - The memory pressure level. Can be `moderate` or `critical`.

Emitted when the system reports a change in memory pressure. This is an
OS-level signal indicating that the system is running low on available memory.

At the `moderate` level, modules should free buffers that are cheap to
re-allocate and not immediately needed.

At the `critical` level, modules should free all possible memory. The
alternative is to be killed by the system, which means all memory will have to
be re-created, plus the cost of a cold start.

Apps may react by closing non-essential windows or tabs, clearing caches,
dropping undo history, or other memory-saving measures.

## Methods

The `memoryPressureMonitor` module has the following methods:

### `memoryPressureMonitor.getCurrentPressureLevel()`

Returns `string` - The current memory pressure level. Can be `none`, `moderate`,
or `critical`.

Returns the memory pressure level last set via `notifyMemoryPressure()`. Defaults
to `none` if no level has been set.

### `memoryPressureMonitor.notifyMemoryPressure(level)`

* `level` string - The pressure level to set. Must be `none`, `moderate`, or
`critical`.

Broadcasts a memory pressure notification to all listeners in both the main
process and all renderer processes. This causes Chromium internals (blink
resource cache, V8 garbage collector, network cache, decoded image cache, etc.)
to release memory as if the OS had reported memory pressure.

When called with `none`, the level is stored (so `getCurrentPressureLevel()`
reflects it) but no notification is broadcast, since Chromium does not support
notifying "no pressure".

Use this when your application knows that external resources held by the
Electron process are causing system-wide memory pressure, and you want Electron
to proactively release caches and run garbage collection across all processes.

In the main process, this respects the notification-suppressed flag — if
notifications are suppressed (e.g. during memory measurement), the main-process
notification will be silently dropped. Renderer processes are always notified.

```javascript
const { memoryPressureMonitor } = require('electron')

// Listen for OS memory pressure events
memoryPressureMonitor.on('memory-pressure', (level) => {
console.log(`Memory pressure: ${level}`)
if (level === 'critical') {
// Close non-essential windows, drop caches, etc.
}
})

// Trigger memory pressure to force Electron to release caches
memoryPressureMonitor.notifyMemoryPressure('critical')

// Query current pressure level
console.log(memoryPressureMonitor.getCurrentPressureLevel())
```
2 changes: 2 additions & 0 deletions filenames.auto.gni
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ auto_filenames = {
"docs/api/incoming-message.md",
"docs/api/ipc-main.md",
"docs/api/ipc-renderer.md",
"docs/api/memory-pressure-monitor.md",
"docs/api/menu-item.md",
"docs/api/menu.md",
"docs/api/message-channel-main.md",
Expand Down Expand Up @@ -206,6 +207,7 @@ auto_filenames = {
"lib/browser/api/global-shortcut.ts",
"lib/browser/api/in-app-purchase.ts",
"lib/browser/api/ipc-main.ts",
"lib/browser/api/memory-pressure-monitor.ts",
"lib/browser/api/menu-item-roles.ts",
"lib/browser/api/menu-item.ts",
"lib/browser/api/menu-utils.ts",
Expand Down
2 changes: 2 additions & 0 deletions filenames.gni
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,8 @@ filenames = {
"shell/browser/api/electron_api_in_app_purchase.h",
"shell/browser/api/electron_api_menu.cc",
"shell/browser/api/electron_api_menu.h",
"shell/browser/api/electron_api_memory_pressure_monitor.cc",
"shell/browser/api/electron_api_memory_pressure_monitor.h",
"shell/browser/api/electron_api_native_theme.cc",
"shell/browser/api/electron_api_native_theme.h",
"shell/browser/api/electron_api_net_log.cc",
Expand Down
55 changes: 55 additions & 0 deletions lib/browser/api/memory-pressure-monitor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { EventEmitter } from 'events';

const {
createMemoryPressureMonitor
} = process._linkedBinding('electron_browser_memory_pressure_monitor');

class MemoryPressureMonitor extends EventEmitter {
#native: any;

constructor () {
super();
// Lazily create the native binding on first listener, following
// the powerMonitor pattern. This avoids creating a
// base::MemoryPressureListener until someone actually cares.
this.once('newListener', () => {
this.#native = createMemoryPressureMonitor();
this.#native.emit = this.emit.bind(this);
});
}

/**
* Returns the current memory pressure level reported by the OS.
* @returns {'none' | 'moderate' | 'critical'}
*/
getCurrentPressureLevel (): 'none' | 'moderate' | 'critical' {
if (this.#native) {
return this.#native.getCurrentPressureLevel();
}
// If native binding hasn't been created yet, create it now
this.#native = createMemoryPressureMonitor();
this.#native.emit = this.emit.bind(this);
return this.#native.getCurrentPressureLevel();
}

/**
* Sets the memory pressure level. For 'moderate' and 'critical',
* broadcasts a notification to all listeners in this process and
* renderer processes. For 'none', records the level without
* broadcasting (Chromium does not accept none notifications).
*
* @param level - 'none' | 'moderate' | 'critical'
*/
notifyMemoryPressure (level: 'none' | 'moderate' | 'critical'): void {
if (this.#native) {
this.#native.notifyMemoryPressure(level);
return;
}
// If native binding hasn't been created yet, create it now
this.#native = createMemoryPressureMonitor();
this.#native.emit = this.emit.bind(this);
this.#native.notifyMemoryPressure(level);
}
}

module.exports = new MemoryPressureMonitor();
1 change: 1 addition & 0 deletions lib/browser/api/module-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const browserModuleList: ElectronInternal.ModuleEntry[] = [
{ name: 'globalShortcut', loader: () => require('./global-shortcut') },
{ name: 'ipcMain', loader: () => require('./ipc-main') },
{ name: 'inAppPurchase', loader: () => require('./in-app-purchase') },
{ name: 'memoryPressureMonitor', loader: () => require('./memory-pressure-monitor') },
{ name: 'Menu', loader: () => require('./menu') },
{ name: 'MenuItem', loader: () => require('./menu-item') },
{ name: 'MessageChannelMain', loader: () => require('./message-channel') },
Expand Down
2 changes: 1 addition & 1 deletion patches/chromium/.patches
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,9 @@ add_switch_to_disable_font_matching_cache_os-14352.patch
fix_overlay_strategy_for_html_pages_with_transparency_os-19250.patch
feat_nexus-window_implement_setopacity_support_for_nexus_platform.patch
ozone_nexus_implement_setfullscreen_for_nexuswindow_os-19899.patch
enable_memory_pressure_monitoring_and_enable_for_renderer_process.patch
fix_wayland_window_call_setwindowgeometry_for_position_updates.patch
add_mse_support_to_brightsign_video_player_os-19598.patch
feat_enable_hls_support_when_brightsign_media_player_is_enabled.patch
feat_add_video_audio-codecs-supported_flag_support.patch
brightsign_enable_viewport_options_when_viewport_enabled_os-20339.patch
feat_enable_cross-process_memory_pressure_notification_on_all.patch

This file was deleted.

Loading