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

gue*_*314 6 javascript blob

特定

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

哪个是存储的数组的二进制数据表示?

Jus*_*nas 13

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

TLDR; 在RAM中

  • 您能否提供特定于浏览器中“Blob”实现的 _"TLDR; In RAM"_ 的详细信息? (2认同)

pwn*_*all 11

Blob 代表一组可以存在于任何地方的数据。该文件API规范故意不提供阅读的Blob的任何内容同步方式。

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

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

在 Chrome 中,我们使用多进程架构,其中浏览器进程拥有所有活动 Blob 的中央注册表,并作为 Blob 内容的真实来源。在渲染器中(通过 JavaScript)创建 Blob 时,其内容将通过 IPC、共享内存或临时文件移动到浏览器进程,具体取决于 Blob 的大小。浏览器进程也可能将内存中的 Blob 内容驱逐到临时文件中。上一个答案中提到的 500mb 限制在 2016 年左右解除。更多实现细节在Chrome 的 Blob 子系统的自述文件中


The*_*tan 9

这不会完全回答你的问题.

那么new Blob()宣布a时会发生什么?

官方文件API文档,

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. 
Run Code Online (Sandbox Code Playgroud)

A Blob与任何其他存储器一样存储在存储器中ArrayBuffer.它存储在ram中,就像在窗口中声明的其他对象一样.

看着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
Run Code Online (Sandbox Code Playgroud)

在打印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; }
...
Run Code Online (Sandbox Code Playgroud)

  • 你说它“物理存储在内存中”,但它有一个文件系统路径。那么什么时候它会从 RAM 复制到磁盘呢? (3认同)

Ale*_*lin 5

Blob存储在内存中.在浏览器blob存储中.如果创建blob对象,可以在Firefox内存分析器(about:memory)中进行检查.这里我们可以看到firefox输出的一个例子,选择了文件.Blob和File之间存在差异.Blob存储在内存中,文件存储在文件系统中.

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)
Run Code Online (Sandbox Code Playgroud)

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