nodejs/node · error

could not read dictionary: out of memory\n

Error message

could not read dictionary: out of memory\n

What it means

`malloc` for the dictionary buffer returned NULL — the process could not allocate `context->dictionary_size` bytes to hold the dictionary contents in memory. The file was successfully sized but the heap is exhausted or the allocation exceeds platform limits.

Source

Thrown at deps/brotli/c/tools/brotli.c:918

  file_size_64 = FileSize(context->dictionary_path);
  if (file_size_64 == -1) {
    fprintf(stderr, "could not get size of dictionary file [%s]",
            PrintablePath(context->dictionary_path));
    fclose(f);
    return BROTLI_FALSE;
  }

  if (file_size_64 > kMaxDictionarySize) {
    fprintf(stderr, "dictionary [%s] is larger than maximum allowed: %d\n",
            PrintablePath(context->dictionary_path), kMaxDictionarySize);
    fclose(f);
    return BROTLI_FALSE;
  }
  context->dictionary_size = (size_t)file_size_64;

  buffer = (uint8_t*)malloc(context->dictionary_size);
  if (!buffer) {
    fprintf(stderr, "could not read dictionary: out of memory\n");
    fclose(f);
    return BROTLI_FALSE;
  }
  bytes_read = fread(buffer, sizeof(uint8_t), context->dictionary_size, f);
  if (bytes_read != context->dictionary_size) {
    free(buffer);
    fprintf(stderr, "failed to read dictionary [%s]: %s\n",
            PrintablePath(context->dictionary_path), strerror(errno));
    fclose(f);
    return BROTLI_FALSE;
  }
  fclose(f);
  context->dictionary = buffer;
  if (command == COMMAND_COMPRESS) {
    context->prepared_dictionary = BrotliEncoderPrepareDictionary(
        BROTLI_SHARED_DICTIONARY_RAW, context->dictionary_size,
        context->dictionary, BROTLI_MAX_QUALITY, NULL, NULL, NULL);
    if (context->prepared_dictionary == NULL) {

View on GitHub (pinned to 1b2de5e052)

Solutions

  1. Free memory or increase the container/cgroup memory limit.
  2. Use a smaller dictionary file.
  3. On 32-bit platforms, switch to a 64-bit build to enlarge the address space.
  4. Check `ulimit -v` and `ulimit -d` and raise them if set low.
Defensive patterns

Strategy: validation

Validate before calling

#include <sys/sysinfo.h>
#include <stdbool.h>

bool enough_memory_for_dict(size_t dict_size) {
    struct sysinfo si;
    if (sysinfo(&si) != 0) return true; // can't check, proceed
    // Need at least dict_size + overhead of free RAM
    unsigned long long freeram = (unsigned long long)si.freeram * si.mem_unit;
    return freeram > dict_size * 2; // 2x safety margin
}

Prevention

When it happens

Trigger: System memory is exhausted; the dictionary size is large enough to fail `malloc` on a 32-bit build (near or above 2 GB); container memory cgroup limit hit; ulimit on data segment (`ulimit -d`) too low.

Common situations: Running brotli in a memory-constrained container or embedded device; processing a dictionary that is near the brotli maximum on a system with limited RAM; many concurrent brotli processes competing for memory.

Related errors


AI-assisted analysis of nodejs/node@1b2de5e052 (2026-08-13). Data as JSON: /api/errors/d287b41a05de64fe. Report an issue: GitHub.