java-native-access/jna · error · IllegalArgumentException

Allocation size must be greater than zero

Error message

Allocation size must be greater than zero

What it means

The Memory constructor throws this IllegalArgumentException when asked to allocate zero or negative bytes. Native memory allocation must have a positive size; JNA also tracks such allocations for cleanup, so a non-positive size is rejected up front.

Source

Thrown at src/com/sun/jna/Memory.java:115

        @Override
        protected void boundsCheck(long off, long sz) {
            Memory.this.boundsCheck(this.peer - Memory.this.peer + off, sz);
        }
        @Override
        public String toString() {
            return super.toString() + " (shared from " + Memory.this.toString() + ")";
        }
    }

    /**
     * Allocate space in the native heap via a call to C's <code>malloc</code>.
     *
     * @param size number of <em>bytes</em> of space to allocate
     */
    public Memory(long size) {
        this.size = size;
        if (size <= 0) {
            throw new IllegalArgumentException("Allocation size must be greater than zero");
        }
        peer = malloc(size);
        if (peer == 0)
            throw new OutOfMemoryError("Cannot allocate " + size + " bytes");

        allocatedMemory.put(peer, new WeakReference<>(this));
        cleanable = Cleaner.getCleaner().register(this, new MemoryDisposer(peer));
    }

    protected Memory() {
        super();
        cleanable = null;
    }

    /** Provide a view of this memory using the given offset as the base address.  The
     * returned {@link Pointer} will have a size equal to that of the original
     * minus the offset.
     * @throws IndexOutOfBoundsException if the requested memory is outside

View on GitHub (pinned to d036ad9781)

Solutions

  1. Ensure size > 0 before constructing; skip allocation entirely when size == 0.
  2. Call structureCacheRuntimeInfo()/calculateSize(true) or set fields so Structure.size() is computed before passing it.
  3. Clamp or validate computed sizes with a guard: if (n <= 0) throw or return early.

Example fix

// before
Memory buf = new Memory(data.length - 1); // could be 0/negative
// after
if (data.length > 0) { Memory buf = new Memory(data.length); }
Defensive patterns

Strategy: validation

Validate before calling

static Memory safeMemory(long size) {
  if (size <= 0) throw new IllegalArgumentException("Refusing Memory allocation of " + size + " bytes");
  return new Memory(size);
}

Try / catch

try {
  Memory m = new Memory(struct.size());
} catch (IllegalArgumentException e) {
  if (e.getMessage().startsWith("Allocation size must be greater than zero")) {
    throw new IllegalStateException("Structure size was 0; populate fields / call calculateSize(true) first", e);
  }
  throw e;
}

Prevention

When it happens

Trigger: new Memory(0), new Memory(-1), or size computed from an expression that evaluated to <= 0 (e.g. struct size of an empty/uninitialized Structure).

Common situations: Calling new Memory(struct.size()) before the Structure's fields are set (size still 0); arithmetic on lengths where an empty input yields 0; off-by-one producing negative sizes.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of java-native-access/jna@d036ad9781 (2026-09-12). Data as JSON: /api/errors/da5cfc262c0821a8. Report an issue: GitHub.