Device Memory API

Draft Community Group Report,

This version:
https://wicg.github.io/device-memory/
Issue Tracking:
GitHub
Editors:
(Google)
(Microsoft)
Former Editor:
Shubhie Panicker (Google)

Abstract

This document defines a HTTP Client Hint header and a JavaScript API to surface device capability for memory (device RAM) in order to enable web apps to customize content depending on device memory constraints.

Status of this document

This specification was published by the Web Platform Incubator Community Group. It is not a W3C Standard nor is it on the W3C Standards Track. Please note that under the W3C Community Contributor License Agreement (CLA) there is a limited opt-out and other conditions apply. Learn more about W3C Community and Business Groups.

1. Introduction

A device-class signal is used for several reasons, including:

Device identification and classification based on advertised User-Agent, and other characteristics of the client, are commonly used to select and provide optimized content. Such solutions frequently rely on commercial device databases, which are costly, hard to integrate, and hard to maintain.

This specification defines a (Client Hint) Header Field and a JavaScript API which exposes the approximate device memory (RAM) to address these needs without needing to use a device database.

2. Computing device memory value

The user agent’s deviceMemory is calculated by these steps:
  1. Let physicalDeviceMemory be the amount of physical memory in bytes.

  2. Let minimumLowerBound be an implementation-defined minimum bound.

  3. Let maximumUpperBound be an implementation-defined maximum bound.

  4. Let deviceMemory be physicalDeviceMemory / 1024.0.

  5. Let power be 0.

  6. While deviceMemory is greater than 1:

    1. Bitwise shift deviceMemory right 1 place.

    2. Increment power by 1.

  7. Let lowerBound be 2 to the power of power.

  8. Let upperBound be 2 to the power of |power + 1|.

  9. If physicalDeviceMemory − lowerBound ≤ upperBound − physicalDeviceMemory, then deviceMemory’s value is lowerBound.

  10. Otherwise deviceMemory’s value is upperBound.

  11. If deviceMemory < minimumLowerBound, then deviceMemory is minimumLowerBound.

  12. If deviceMemory > maximumUpperBound, then deviceMemory is maximumUpperBound.

  13. Return deviceMemory.

The algorithm includes the ability for implementation-defined upper bound and a lower bounds. The range between the upper and the lower bounds should include the majority of device memory values but exclude rare device memory values to mitigate device fingerprinting. Implementations may adjust these bounds over time. These bounds may differ on different device types.

3. Sec-CH-Device-Memory (Client Hint) Header Field

The Sec-CH-Device-Memory header field is a HTTP Client Hint header. It is a structured header value containing an item which value is a decimal that indicates the client’s approximate amount of device memory (RAM) in GiB.

If `Sec-CH-Device-Memory` header field occurs in a message more than once, the last value overrides all previous occurrences.

The ABNF (Augmented Backus-Naur Form) syntax for the `Sec-CH-Device-Memory` header field is as follows:

Sec-CH-Device-Memory = sf-decimal

The `Sec-CH-Device-Memory`'s value should be set to the user agent’s deviceMemory.

3.1. Client Hint examples

A server opts in to receive a `Sec-CH-Device-Memory` HTTP Client Hint using the Accept-CH header field, or an equivalent HTML meta element with http-equiv attribute:
Accept-CH: Sec-CH-Device-Memory

In turn, on receiving the above preferences from the server, a compatible user agent would then advertise the device capability for memory, via the `Sec-CH-Device-Memory` request header field:

GET /example HTTP/1.1
Sec-CH-Device-Memory: 8
...

4. Device Memory JavaScript API

[
    SecureContext,
    Exposed=(Window,Worker)
] interface mixin NavigatorDeviceMemory {
    readonly attribute double deviceMemory;
};

Navigator includes NavigatorDeviceMemory;
WorkerNavigator includes NavigatorDeviceMemory;

The NavigatorDeviceMemory’s deviceMemory getter steps are to return the user agent’s deviceMemory.

4.1. JavaScript examples

A web application can either enable or disable features based on device memory.

Note: The web application should consider how to handle browsers that do not support the API: either by enabling by default, or disabling by default.

const mem = navigator.deviceMemory;

// Either disable features if it is known to be a low-memory device
if (mem && mem < 2) {
    // disable features to provide a better experience
}

// Or, alternatively only enable features if the device memory is
// either not provided, or known to be above minimum requirements
if (!mem || mem > 4) {
    // enable features to provide a better experience
}
A RUM solution can query the device memory using navigator.deviceMemory API and include this in performance beacons as additional information to help explain or segment the data.
const mem = navigator.deviceMemory;
const analyticsData = {
    memory: mem,
    // Add other metrics here.
};
navigator.sendBeacon("/endpoint", JSON.stringify(analyticsData));

5. Security & privacy considerations

`Sec-CH-Device-Memory` Client Hint header and JavaScript API will only be available to HTTPS secure contexts.

To reduce fingerprinting risk, the reported value is rounded to a single significant bit, as opposed to reporting the exact value. In addition, an implementation-specific upper and lower bound is placed on the reported values. These bounds should be reviewed over time as commonly used device memory characteristics change. Device type should be taken into account when defining these bounds since mobile devices typically have different characteristics than desktops and laptops.

6. IANA considerations

This document defines the `Sec-CH-Device-Memory` HTTP request header field, and registers them in the permanent message header field registry ([RFC3864]).

6.1. Sec-CH-Device-Memory header field

Header field name

Sec-CH-Device-Memory

Applicable protocol

http

Status

standard

Author/Change controller

IETF

Specification document

This specification (§ 3 Sec-CH-Device-Memory (Client Hint) Header Field)

7. Acknowledgements

Special thanks to the previous editor Shubhie Panicker and all the contributors for their technical input and suggestions that led to improvements to this specification.

Conformance

Document conventions

Conformance requirements are expressed with a combination of descriptive assertions and RFC 2119 terminology. The key words “MUST”, “MUST NOT”, “REQUIRED”, “SHALL”, “SHALL NOT”, “SHOULD”, “SHOULD NOT”, “RECOMMENDED”, “MAY”, and “OPTIONAL” in the normative parts of this document are to be interpreted as described in RFC 2119. However, for readability, these words do not appear in all uppercase letters in this specification.

All of the text of this specification is normative except sections explicitly marked as non-normative, examples, and notes. [RFC2119]

Examples in this specification are introduced with the words “for example” or are set apart from the normative text with class="example", like this:

This is an example of an informative example.

Informative notes begin with the word “Note” and are set apart from the normative text with class="note", like this:

Note, this is an informative note.

Index

Terms defined by this specification

Terms defined by reference

References

Normative References

[HTML]
Anne van Kesteren; et al. HTML Standard. Living Standard. URL: https://html.spec.whatwg.org/multipage/
[INFRA]
Anne van Kesteren; Domenic Denicola. Infra Standard. Living Standard. URL: https://infra.spec.whatwg.org/
[RFC2119]
S. Bradner. Key words for use in RFCs to Indicate Requirement Levels. March 1997. Best Current Practice. URL: https://datatracker.ietf.org/doc/html/rfc2119
[RFC8941]
M. Nottingham; P-H. Kamp. Structured Field Values for HTTP. February 2021. Proposed Standard. URL: https://httpwg.org/specs/rfc8941.html
[RFC8942]
I. Grigorik; Y. Weiss. HTTP Client Hints. February 2021. Experimental. URL: https://www.rfc-editor.org/info/rfc8942/
[WEBIDL]
Edgar Chen; Timothy Gu. Web IDL Standard. Living Standard. URL: https://webidl.spec.whatwg.org/

Non-Normative References

[RFC3864]
G. Klyne; M. Nottingham; J. Mogul. Registration Procedures for Message Header Fields. September 2004. Best Current Practice. URL: https://www.rfc-editor.org/info/rfc3864/

IDL Index

[
    SecureContext,
    Exposed=(Window,Worker)
] interface mixin NavigatorDeviceMemory {
    readonly attribute double deviceMemory;
};

Navigator includes NavigatorDeviceMemory;
WorkerNavigator includes NavigatorDeviceMemory;