Tencent/matrix · error · IllegalArgumentException

sizes should not be negative and maxSize should be 0 or…

Error message

sizes should not be negative and maxSize should be 0 or greater than minSize: min = <mMinTraceSize>, max = <mMaxTraceSize>

What it means

MemoryHook.onConfigure() validates the trace-size configuration before installing the memory hook. It throws IllegalArgumentException when mMinTraceSize is negative, or when mMaxTraceSize is non-zero but smaller than mMinTraceSize. A max of 0 is treated as 'unlimited', so only a positive max below min is rejected.

Solutions

  1. Read the message values: it prints the actual mMinTraceSize and mMaxTraceSize that failed validation — fix those inputs.
  2. Ensure min trace size is >= 0 and, if a max is set, max >= min (max = 0 means unlimited).
  3. Check for unit conversion bugs (KB vs bytes) or swapped min/max arguments at the configuration call site.
  4. Clamp dynamic/derived sizes defensively before passing them to the hook builder.

Example fix

// before
memoryHook.setTraceSize(-1024, 512); // min negative
// after
int min = Math.max(0, computedMin);
int max = (computedMax == 0) ? 0 : Math.max(min, computedMax);
memoryHook.setTraceSize(min, max);
Defensive patterns

Strategy: validation

Validate before calling

public static int[] safeTraceSizes(int min, int max) {
    if (min < 0) min = 0;
    if (max != 0 && max < min) max = (min == 0) ? 0 : min;
    return new int[]{min, max};
}

Try / catch

try {
    memoryHook.onConfigure();
} catch (IllegalArgumentException e) {
    MatrixLog.e(TAG, "invalid trace sizes, using defaults", e);
    memoryHook.setTraceSize(DEFAULT_MIN, DEFAULT_MAX);
}

Prevention

When it happens

Trigger: Building/configuring a MemoryHook (or the corresponding MatrixBuilder parameter) with setTraceSize / min/max trace size values where min < 0, or max != 0 && max < min, before calling install/commit.

Common situations: Passing a size in KB where the API expects bytes (yielding a tiny or negative value after unit conversion); copying sample config with max left smaller than a customized min; computing sizes dynamically from device memory and producing a negative number on failure; typo swapping min and max arguments.

Related errors


AI-assisted analysis of Tencent/matrix@3b8293bd65 (2026-09-08). Data as JSON: /api/errors/65e9d0287339b21a. Report an issue: GitHub.

Appendix: source

Thrown at matrix/matrix-android/matrix-hooks/src/main/java/com/tencent/matrix/hook/memory/MemoryHook.java:140

                .addHook(this)
                .commitHooks();
    }

    @NonNull
    @Override
    protected String getNativeLibraryName() {
        return "matrix-memoryhook";
    }

    @Override
    public boolean onConfigure() {
        if (mMemGuardInstalled) {
            MatrixLog.w(TAG, "MemGuard has been installed, skip MemoryHook install logic.");
            return false;
        }

        if (mMinTraceSize < 0 || (mMaxTraceSize != 0 && mMaxTraceSize < mMinTraceSize)) {
            throw new IllegalArgumentException("sizes should not be negative and maxSize should be " +
                    "0 or greater than minSize: min = " + mMinTraceSize + ", max = " + mMaxTraceSize);
        }

        MatrixLog.d(TAG, "enable mmap? " + mEnableMmap);
        enableMmapHookNative(mEnableMmap);

        setTracingAllocSizeRangeNative(mMinTraceSize, mMaxTraceSize);
        setStacktraceLogThresholdNative(mStacktraceLogThreshold);
        enableStacktraceNative(mEnableStacktrace);

        return true;
    }

    @Override
    protected boolean onHook(boolean enableDebug) {
        if (!mHookInstalled) {
            installHooksNative(mHookSoSet.toArray(new String[0]), mIgnoreSoSet.toArray(new String[0]), enableDebug);
            mHookInstalled = true;

View on GitHub (pinned to 3b8293bd65)