{"record":{"id":"5e6a6935de2f5386","repo":"MyCATApache/Mycat-Server","slug":"unable-to-acquire-required-bytes-of-memory-got","errorCode":null,"errorMessage":"Unable to acquire ${required} bytes of memory, got ${got}","messagePattern":"Unable to acquire (.+?) bytes of memory, got (.+?)","errorType":"exception","errorClass":"OutOfMemoryError","httpStatus":null,"severity":"critical","filePath":"src/main/java/io/mycat/memory/unsafe/memory/mm/MemoryConsumer.java","lineNumber":92,"sourceCode":"   * @return the amount of released memory in bytes\n   * @throws IOException\n   */\n  public abstract long spill(long size, MemoryConsumer trigger) throws IOException;\n\n  /**\n   * Allocates a LongArray of `size`.\n   */\n  public LongArray allocateLongArray(long size) {\n    long required = size * 8L;\n    MemoryBlock page = dataNodeMemoryManager.allocatePage(required,this);\n    if (page == null || page.size() < required) {\n      long got = 0;\n      if (page != null) {\n        got = page.size();\n        dataNodeMemoryManager.freePage(page, this);\n      }\n      dataNodeMemoryManager.showMemoryUsage();\n      throw new OutOfMemoryError(\"Unable to acquire \" + required + \" bytes of memory, got \" + got);\n    }\n    used += required;\n    return new LongArray(page);\n  }\n\n  /**\n   * Frees a LongArray.\n   */\n  public void freeLongArray(LongArray array) {\n    freePage(array.memoryBlock());\n  }\n\n  public CharArray allocateCharArray(long size) {\n    long required = size * 2L;\n    MemoryBlock page = dataNodeMemoryManager.allocatePage(required,this);\n    if (page == null || page.size() < required) {\n      long got = 0;\n      if (page != null) {","sourceCodeStart":74,"sourceCodeEnd":110,"githubUrl":"https://github.com/MyCATApache/Mycat-Server/blob/65f8d8beb752f935752f2a0eec0ab017facab9ef/src/main/java/io/mycat/memory/unsafe/memory/mm/MemoryConsumer.java#L74-L110","documentation":"MemoryConsumer.allocateLongArray tries to acquire `required` bytes of execution memory and allocate a page for a LongArray. When the memory manager cannot provide a page large enough even after spilling, it frees any partial page, dumps memory usage, and throws OutOfMemoryError reporting how many bytes were requested vs. actually obtained.","triggerScenarios":"Calling allocateLongArray(required) on a MemoryConsumer (e.g. UnsafeInMemorySorter growth) when the DataNodeMemoryManager cannot grant `required` bytes of execution memory after spilling all consumers.","commonSituations":"Sorters/pointers arrays growing on datasets larger than available execution memory; too many concurrent tasks sharing a fixed memory pool; under-provisioned executor memory.","solutions":["Reduce the requested array size (smaller batches, cap sorter size) so it fits in available execution memory.","Increase execution memory for the process/task (allocator/memory-manager configuration).","Reduce concurrency so fewer consumers compete for the same execution memory pool.","Catch OutOfMemoryError and fall back to a disk-based (spilling) implementation instead of an in-memory long array."],"exampleFix":"// before\nLongArray arr = consumer.allocateLongArray(numRecords * 2);\n// after\nlong required = numRecords * 2 * 8L;\nif (required > availableExecutionMemory()) {\n  // shrink or spill instead\n  numRecords = maxRecordsThatFit(availableExecutionMemory());\n}\nLongArray arr = consumer.allocateLongArray(numRecords * 2);","handlingStrategy":"try-catch","validationCode":"long requiredBytes = numLongs * 8L;\nif (requiredBytes > memoryManager.getAvailableExecutionMemory()) {\n  numLongs = (int) (memoryManager.getAvailableExecutionMemory() / 8L);\n}","typeGuard":null,"tryCatchPattern":"try {\n  LongArray arr = consumer.allocateLongArray(numLongs);\n} catch (OutOfMemoryError e) {\n  // fall back to a spilling/disk-backed sorter\n  sorter = new ExternalSorter(...);\n}","preventionTips":["Size long arrays against available execution memory, not dataset size.","Cap sorter/array growth and spill earlier.","Reduce concurrent tasks sharing the memory pool.","Increase execution memory limits for large workloads."],"tags":["java","memory","out-of-memory","long-array","sorter"],"backgroundTag":"out-of-memory","analyzedSha":"65f8d8beb752f935752f2a0eec0ab017facab9ef","analyzedAt":"2026-09-11T00:12:21.696Z","contentChangedAt":"2026-09-11T00:12:21.696Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}