Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for performance.measureUserAgentSpecificMemory() #208

Open
kenzieschmoll opened this issue Mar 13, 2024 · 2 comments
Open

Add support for performance.measureUserAgentSpecificMemory() #208

kenzieschmoll opened this issue Mar 13, 2024 · 2 comments

Comments

@kenzieschmoll
Copy link
Contributor

In the DevTools flutter web app, we'd like to periodically check our memory usage to take action and release resources before OOMing (flutter/devtools#7002). To do this, I was looking at using the browser memory API:
https://developer.mozilla.org/en-US/docs/Web/API/Performance/measureUserAgentSpecificMemory

This doesn't appear to be available from the Performance API in package:web:

extension type Performance._(JSObject _) implements EventTarget, JSObject {

Are there plans to support this? Is there a better way we should be checking the total memory usage for the web app?

@navaronbracke
Copy link

navaronbracke commented Mar 14, 2024

Since that API is experimental, it is not exposed by package:web, per the note in https://github.com/dart-lang/web?tab=readme-ov-file#compatibility

You might get away with using an extension / extension type?

import 'dart:js_interop';

import 'package:web/web.dart';

extension PerformanceMemory on Performance {
  @JS('measureUserAgentSpecificMemory')
  external JSFunction? get _measureUserAgentSpecificMemory;

  Future<UserAgentSpecificMemory?> measureUserAgentSpecificMemory() {
    if (_measureUserAgentSpecificMemory == null) {
      return Future<UserAgentSpecificMemory?>.value();
    }

    return (_measureUserAgentSpecificMemory!.callAsFunction() as JSPromise<UserAgentSpecificMemory>).toDart;
  }
}

@JS()
extension type UserAgentSpecificMemory._(JSObject _) implements JSObject {
  external int get bytes;

  external JSArray<UserAgentSpecificMemoryBreakdownElement> get breakdown;
}

@JS()
extension type UserAgentSpecificMemoryBreakdownElement._(JSObject _) implements JSObject {
  external JSArray<UserAgentSpecificMemoryBreakdownAttributionElement> get attribution;

  external int get bytes;

  external JSArray<JSString> get types;
}

@JS()
extension type UserAgentSpecificMemoryBreakdownAttributionElement._(JSObject _) implements JSObject {
  external UserAgentSpecificMemoryBreakdownAttributionContainerElement? get container;

  external String get scope;

  external String get url;
}

@JS()
extension type UserAgentSpecificMemoryBreakdownAttributionContainerElement._(JSObject _) implements JSObject {
  external String get id;

  external String get url;
}

Although, when I inspect if the method is defined, on my Chrome 122.0.6261.128 instance , typeof performance.measureUserAgentSpecificMemory results in undefined?

I don't think there is a different API for it though.

@srujzs
Copy link
Contributor

srujzs commented Mar 14, 2024

This may get added back as part of #209. It's on track to be a standard, but it's marked experimental, so it depends on which way we lean. If experimental APIs lead to too many breakages, we may exclude them. @navaronbracke is correct here - you can expose your own types/APIs to workaround this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants