Skip to content

feat: Add a JS binding for MemoryPressureMonitor#118

Merged
caneraltinbasak merged 4 commits into28-x-y-bsfrom
memory-pressure-monitor-binding
Mar 3, 2026
Merged

feat: Add a JS binding for MemoryPressureMonitor#118
caneraltinbasak merged 4 commits into28-x-y-bsfrom
memory-pressure-monitor-binding

Conversation

@caneraltinbasak
Copy link

This pull request has 3 parts:

  • It adds a JS binding for MemoryPressureMonitor. It is global as electron.memoryPresssureMonitor. We can send memory pressure events externally using this API(upstreameable)
  • It removes the guards around broadcasting the memorypressure to render processes (upstreamable)
  • It removes our previous attempt of enabling memory pressure monitor on main and render processes.(internal)

@caneraltinbasak caneraltinbasak changed the title Memory pressure monitor binding feat: Add a JS binding for MemoryPressureMonitor Feb 24, 2026
@caneraltinbasak caneraltinbasak force-pushed the memory-pressure-monitor-binding branch from 6cb9adb to 32c4148 Compare February 24, 2026 15:22
### `memoryPressureMonitor.notifyMemoryPressure(level)`

* `level` string - The pressure level to broadcast. Must be `moderate` or
`critical`.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering why notifyMemoryPressure doesn't send none as an option (i.e. if we notify the level is moderate then after the memory usage has been reduced and the pressure level goes back down to normal)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Memory pressure handlers generally don't hold state, they apply an action. But it is a valid point, I'll address that.

@caneraltinbasak caneraltinbasak force-pushed the memory-pressure-monitor-binding branch from 32c4148 to 7c948d5 Compare February 27, 2026 10:51
@caneraltinbasak caneraltinbasak force-pushed the memory-pressure-monitor-binding branch from 7c948d5 to 8b497fc Compare February 27, 2026 13:02
* process. Chromium's internal caches, blink, and V8 will react by
* releasing memory. Respects the notification-suppressed flag.
*
* @param level - 'moderate' or 'critical'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should now also include 'none'

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out "none" is not a valid state for MemoryPressureMonitor, there is a DCHECK which triggers if NONE is notified. Tests failed due to that. I'll remove all the changes which I've added for triggering "kNone". As I stated before MemoryPressureListeners do action, they don't hold state.


// static
void MemoryPressureListener::NotifyMemoryPressure(
    MemoryPressureLevel memory_pressure_level) {
  DCHECK_NE(memory_pressure_level, MEMORY_PRESSURE_LEVEL_NONE);
  TRACE_EVENT_INSTANT(
      trace_event::MemoryDumpManager::kTraceCategory,
      "MemoryPressureListener::NotifyMemoryPressure",
      [&](perfetto::EventContext ctx) {
        auto* event = ctx.event<perfetto::protos::pbzero::ChromeTrackEvent>();
        auto* data = event->set_chrome_memory_pressure_notification();
        data->set_level(
            trace_event::MemoryPressureLevelToTraceEnum(memory_pressure_level));
      });
  if (AreNotificationsSuppressed())
    return;
  DoNotifyMemoryPressure(memory_pressure_level);
}

*
* @param level - 'moderate' or 'critical'
*/
notifyMemoryPressure (level: string): void {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should have the type here?
notifyMemoryPressure (level: 'none' | 'moderate' | 'critical' )

* Returns the current memory pressure level reported by the OS.
* @returns {'none' | 'moderate' | 'critical'}
*/
getCurrentPressureLevel (): string {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should have the type here?

getCurrentPressureLevel (): 'none' | 'moderate' | 'critical' {

createMemoryPressureMonitor(): MemoryPressureMonitorBinding;
getCurrentPressureLevel(): string;
notifyMemoryPressure(level: string): void;
emit: (event: string, ...args: any[]) => boolean;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should have typing here instead of string for getCurrentPressureLevel and notifyMemoryPressure?

@caneraltinbasak caneraltinbasak force-pushed the memory-pressure-monitor-binding branch from 8b497fc to 2ef568c Compare March 2, 2026 10:16
@caneraltinbasak caneraltinbasak force-pushed the memory-pressure-monitor-binding branch from 2ef568c to da0390a Compare March 2, 2026 10:47
Add memoryPressureMonitor singleton for main process that wraps Chromiums
memory pressure system. Enables listening for OS memory pressure events
and programmatically triggering pressure notifications to force cache drops
and GC.

Changes:
- New C++ API: shell/browser/api/electron_api_memory_pressure_monitor.*
- New TypeScript wrapper: lib/browser/api/memory-pressure-monitor.ts
- Methods: getCurrentPressureLevel(), notifyMemoryPressure(level)
- Event: memory-pressure emitted on OS pressure changes
- Tests: spec/api-memory-pressure-monitor-spec.ts
- Docs: docs/api/memory-pressure-monitor.md
- Registration: module-list.ts, node_bindings.cc, filenames.gni, typings
…S-20753, OS-20757

Remove IS_ANDROID guards from Chromium's cross-process memory pressure
IPC to enable notifyMemoryPressure() to reach all renderer processes on
all platforms via content::mojom::ChildProcess::OnMemoryPressure.

Iterate RenderProcessHost instances in the browser process and forward
the pressure level to each live renderer, matching the pattern used by
UserLevelMemoryPressureSignalGenerator on Android.
We will use the new electron.memoryPressureMonitor to trigger
memory pressure events. Electron's memoryPressureMonitor propagates
events to render processes too. Remove our previous attempt where
we relied on Chromium's own LinuxMemoryPressureMonitor and launching
the monitor on both main and renderer processes.
@caneraltinbasak caneraltinbasak force-pushed the memory-pressure-monitor-binding branch 3 times, most recently from ce720f2 to 107aa16 Compare March 2, 2026 18:47
@caneraltinbasak caneraltinbasak force-pushed the memory-pressure-monitor-binding branch from 107aa16 to 08ad14d Compare March 2, 2026 19:04
@caneraltinbasak caneraltinbasak merged commit ff8ed5e into 28-x-y-bs Mar 3, 2026
5 checks passed
@caneraltinbasak caneraltinbasak deleted the memory-pressure-monitor-binding branch March 3, 2026 09:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants