Blob 二进制数据存储在哪里?

Where is Blob binary data stored?

给出

var data = new Array(1000000);
for (var i = 0; i < data.length; i++) {
  data[i] = 1;
}
var blob = new Blob([data]);

数组的二进制数据表示存储在哪里?

所有未在任何其他存储中显式表示的变量都存储在内存 (RAM) 中,并一直存在到程序结束或取消设置时(从内存中清除)。

TLDR; 在 RAM 中

这不会完全回答您的问题。

So what happens when a new Blob() is declared?

来自 official fileAPI documentation

The Blob() constructor can be invoked with zero or more parameters. When the Blob() constructor is invoked, user agents must run the following Blob constructor steps:
[1] If invoked with zero parameters, return a new Blob object with its readability state set to OPENED, consisting of 0 bytes, with size set to 0, and with type set to the empty string.
[2] Otherwise, the constructor is invoked with a blobParts sequence. Let a be that sequence.
[3] Let bytes be an empty sequence of bytes.
[4] Let length be `a`s length. For 0 ≤ i < length, repeat the following steps:
    1. Let element be the ith element of a.
    2. If element is a DOMString, run the following substeps:
        Let s be the result of converting element to a sequence of Unicode characters [Unicode] using the algorithm for doing so in WebIDL.
        Encode s as UTF-8 and append the resulting bytes to bytes.
    Note:
        The algorithm from WebIDL [WebIDL] replaces unmatched surrogates in an invalid UTF-16 string with U+FFFD replacement characters. Scenarios exist when the Blob constructor may result in some data loss due to lost or scrambled character sequences.  

    3. If element is an ArrayBufferView [TypedArrays], convert it to a sequence of byteLength bytes from the underlying ArrayBuffer, starting at the byteOffset of the ArrayBufferView [TypedArrays], and append those bytes to bytes.
    4. If element is an ArrayBuffer [TypedArrays], convert it to a sequence of byteLength bytes, and append those bytes to bytes.
    5. If element is a Blob, append the bytes it represents to bytes. The type of the Blob array element is ignored.  
[5] If the type member of the optional options argument is provided and is not the empty string, run the following sub-steps:
    1. Let t be the type dictionary member. If t contains any characters outside the range U+0020 to U+007E, then set t to the empty string and return from these substeps.
    2. Convert every character in t to lowercase using the "converting a string to ASCII lowercase" algorithm.
[6] Return a Blob object with its readability state set to OPENED, referring to bytes as its associated byte sequence, with its size set to the length of bytes, and its type set to the value of t from the substeps above. 

A Blob 与其他任何 ArrayBuffer 一样存储在内存中。它存储在 ram 中,就像 window 中声明的其他对象一样。

查看 chrome://blob-internals,我们可以看到它是如何物理存储在 ram 中的。这是一个示例 blob。

c7828dad-dd4f-44e6-b374-9239dbe35e35
    Refcount: 1
    Status: BlobStatus::DONE: Blob built with no errors.
    Content Type: application/javascript
    Type: file
    Path: /Users/Chetan/Library/Application Support/Google/Chrome/Default/blob_storage/c7828dad-dd4f-44e6-b374-9239dbe35e35/0
    Modification Time: Monday, June 5, 2017 at 4:29:53 PM
    Offset: 4,917,846
    Length: 224,733

在打印 blob 的实际内容时,我们得到一个普通的 js 文件。

$ cat c7828dad-dd4f-44e6-b374-9239dbe35e35/0

...
html {
   font-family: sans-serif;
   /* 1 */
   -ms-text-size-adjust: 100%;
   /* 2 */
   -webkit-text-size-adjust: 100%;
   /* 2 */ }

/**
 * Remove default margin.
 */
body {
    margin: 0; }
...

Blob 存储在内存中。在浏览器 blob 存储中。如果您创建了一个 blob 对象,您可以在 Firefox 内存分析器(关于:内存)中检查它。 一个 firefox 输出的例子,在这里我们可以看到,选择的文件。 Blob 和 File 是有区别的。 Blob 存储在内存中,File 存储在文件系统中。

651.04 MB (100.0%) -- explicit
├──430.49 MB (66.12%) -- dom
│  ├──428.99 MB (65.89%) -- memory-file-data
│  │  ├──428.93 MB (65.88%) -- large
│  │  │  ├────4.00 MB (00.61%) ── file(length=2111596, sha1=b95ccd8d05cb3e7a4038ec5db1a96d206639b740)
│  │  │  ├────4.00 MB (00.61%) ── file(length=2126739, sha1=15edd5bb2a17675ae3f314538b2ec16f647e75d7)

GoogleChrome 中存在错误。 Chrome 有 blob 限制。当您创建的 blob 总量超过 500mb 时。浏览器将停止创建 blob,因为 blob 存储已达到 500mb 的限制。避免这种情况的唯一方法是将一个 blob 写入 IndexDb 并从 IndexDb 中删除。当一个 blob 被写入 indexDb 时,blob 对象会自动保存到一个文件系统中(blob 将被转换为文件)。 在您停止使用 Blob 或使 blob = null 后,垃圾收集器将从内存中清除 Blob。但是 GC 会在一段时间后删除 blob,而不是立即删除。

Blob 代表一堆可以存在于任何地方的数据。 File API specification 有意不提供任何读取 Blob 内容的同步方式。

这里有一些具体的可能性。

  1. 当您通过构造函数创建 Blob 并向其传递 in-memory 数据时,例如 Uint8Array,Blob 的内容至少会存在一段时间。
  2. 当您从 <input type="file"> 获取 Blob 时,Blob 的内容位于磁盘上,位于用户选择的文件中。规范提到了快照,但没有实现,因为它会给用户操作增加很多延迟。
  3. 当您从另一个 client-side 存储 API(如 IndexedDB 或缓存存储 API)获取 Blob 时,Blob 的内容位于 API 的后备存储中在磁盘上。
  4. 一些 API 可能 return 一个 Blob,其数据流来自网络。 XMLHttpRequest 规范使这成为不可能,而且我认为获取规范还需要在创建 Blob 之前检索整个响应。但是,未来可能会有流式传输 HTTP 响应的规范。
  5. 通过片段数组通过 Blob 构造函数创建的 Blob 的内容可能分散在上述所有地方。

在 Chrome 中,我们使用 multi-process 架构,其中浏览器进程拥有所有活动 Blob 的中央注册表,并作为 blob 内容的真实来源。在渲染器中创建 Blob 时(通过 JavaScript),其内容将通过 IPC、共享内存或临时文件移动到浏览器进程,具体取决于 Blob 的大小。浏览器进程也可能将 in-memory Blob 内容逐出到临时文件。上一个答案中提到的 500mb 限制在 2016 年左右取消。更多实施细节在 the README for Chrome's Blobs subsystem.