{"record":{"id":"9a1c6c1cc6212fac","repo":"openjdk/jdk","slug":"java-lang-instrument-javaagent-allocation-failur","errorCode":null,"errorMessage":"java.lang.instrument/-javaagent: allocation failure.\\n","messagePattern":"java\\.lang\\.instrument/-javaagent: allocation failure\\.\\\\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/java.instrument/share/native/libinstrument/InvocationAdapter.c","lineNumber":277,"sourceCode":"        free(jarfile);\n    }\n    if (options != NULL) free(options);\n\n    switch (initerror) {\n    case JPLIS_INIT_ERROR_NONE:\n      result = JNI_OK;\n      break;\n    case JPLIS_INIT_ERROR_CANNOT_CREATE_NATIVE_AGENT:\n      result = JNI_ERR;\n      fprintf(stderr, \"java.lang.instrument/-javaagent: cannot create native agent.\\n\");\n      break;\n    case JPLIS_INIT_ERROR_FAILURE:\n      result = JNI_ERR;\n      fprintf(stderr, \"java.lang.instrument/-javaagent: initialization of native agent failed.\\n\");\n      break;\n    case JPLIS_INIT_ERROR_ALLOCATION_FAILURE:\n      result = JNI_ERR;\n      fprintf(stderr, \"java.lang.instrument/-javaagent: allocation failure.\\n\");\n      break;\n    case JPLIS_INIT_ERROR_AGENT_CLASS_NOT_SPECIFIED:\n      result = JNI_ERR;\n      fprintf(stderr, \"-javaagent: agent class not specified.\\n\");\n      break;\n    default:\n      result = JNI_ERR;\n      fprintf(stderr, \"java.lang.instrument/-javaagent: unknown error\\n\");\n      break;\n    }\n    return result;\n}\n\n/*\n * Agent_OnAttach returns a jint. 0/JNI_OK indicates success and non-0\n * indicates an error. To allow the attach mechanism throw an\n * AgentInitializationException with a reasonable exception message we define\n * a few specific errors here.","sourceCodeStart":259,"sourceCodeEnd":295,"githubUrl":"https://github.com/openjdk/jdk/blob/88dfb74bbeefcf2b0aa11835183bcd949998fc8f/src/java.instrument/share/native/libinstrument/InvocationAdapter.c#L259-L295","documentation":"Emitted by the java.lang.instrument native library when agent initialization fails because a native memory allocation (malloc) failed while setting up the JPLIS agent (e.g. allocating the JPLISAgent structure or its buffers). The agent's Agent_OnLoad returns JNI_ERR, which aborts JVM startup for a -javaagent launched from the command line. It is mapped from JPLIS_INIT_ERROR_ALLOCATION_FAILURE.","triggerScenarios":"JVM launch with -javaagent (or native agent load via Agent_OnLoad) while the process is out of memory: container memory limits hit, ulimit -v exceeded, very large heap leaving no native space, or a huge number of agents/classpath entries.","commonSituations":"Running instrumented JVMs in Kubernetes/containers with tight memory caps; 32-bit JVMs with aggressive heap sizes; fork-heavy processes that momentarily exhaust address space.","solutions":["Raise the container/pod memory limit or system RAM available to the JVM process","Reduce -Xmx/-Xms so more native (off-heap) memory remains for the agent and JVM internals","Check ulimit -v (virtual memory) and raise it if it is capped","Load fewer agents / drop unneeded -javaagent entries to reduce native allocations at startup","Run with jcmd VM.native_memory or NMT to identify native consumers if the problem persists"],"exampleFix":"# before\njava -Xmx4g -javaagent:app-agent.jar -jar app.jar   # 32-bit JVM, native OOM\n# after (leave native headroom / use 64-bit)\njava -Xmx1g -javaagent:app-agent.jar -jar app.jar","handlingStrategy":"validation","validationCode":"// before launching the JVM, check available native headroom\nRuntime rt = Runtime.getRuntime();\nlong maxHeap = Long.parseLong(System.getProperty(\"maxHeap\", \"0\"));\nif (inContainer() && containerMemLimit() - maxHeap < 256L*1024*1024) {\n    throw new IllegalStateException(\"insufficient native memory headroom for agents/JVM internals\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always reserve native memory headroom: keep -Xmx at most ~75% of the container limit","Monitor process RSS vs container limit with alerts before OOM conditions occur","Smoke-test the exact -javaagent combination in a memory-limited staging environment"],"tags":["java-instrument","native","out-of-memory","jvm-startup","c"],"backgroundTag":null,"analyzedSha":"88dfb74bbeefcf2b0aa11835183bcd949998fc8f","analyzedAt":"2026-08-14T11:45:09.665Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}