32位机器加载大型ARXML文件时出现Java StackOverflowError
Looking at your error stack trace and code, this is a classic stack overflow caused by circular dependencies during EPackage initialization in the Eclipse Modeling Framework (EMF). This issue hits the limits of the smaller default stack size in 32-bit JVMs much faster than in 64-bit environments.
What's Happening Under the Hood
Your stack trace shows a repeating cycle of calls like this:
at org.eclipse.emf.ecore.impl.EPackageRegistryImpl.getEPackage(EPackageRegistryImpl.java:127) at autosar40.diagnosticextract.dem.diagnosticoperationcycle.impl.DiagnosticoperationcyclePackageImpl.init(DiagnosticoperationcyclePackageImpl.java:1342) at autosar40.diagnosticextract.dem.diagnosticoperationcycle.DiagnosticoperationcyclePackage.<clinit>(DiagnosticoperationcyclePackage.java:82)
This means multiple AUTOSAR EPackages are referencing each other during their static initialization. On a 32-bit JVM, the default stack size (usually around 512KB) isn't large enough to handle this recursive initialization chain, leading directly to the StackOverflowError.
Practical Solutions to Try
Switch to a 64-bit JVM (most reliable fix): 64-bit JVMs come with a larger default stack size (typically 1MB or more), which is almost always enough to handle these initialization cycles. If your environment allows this, this is the quickest and least complex fix.
Increase JVM stack size (if stuck on 32-bit): You can adjust the stack size using the
-XssJVM parameter. Try setting it to-Xss1m(1 megabyte) or-Xss2m(2 megabytes). Note that on 32-bit systems, you can't set this value too high without eating into available heap memory, so start with small increments and test.Manually initialize EPackages upfront: If the above options aren't feasible, you can break the circular dependency by triggering the initialization of the affected packages before loading the ARXML file. Add this code right before your existing loading logic:
// Initialize dependent packages explicitly to avoid recursive initialization during load autosar40.diagnosticextract.dem.diagnosticaging.DiagnosticagingPackage.eINSTANCE; autosar40.diagnosticextract.dem.diagnosticoperationcycle.DiagnosticoperationcyclePackage.eINSTANCE; autosar40.diagnosticextract.dem.diagnosticevent.DiagnosticeventPackage.eINSTANCE; // Proceed with your original ARXML loading code ifile.create(inputStreamObj, true, null); ModelLoadManager.INSTANCE.loadFile(ifile, false, null);This pre-initializes the packages so their static setup doesn't happen recursively during the file load process.
Testing Steps
- First, try adjusting the
-Xssparameter and rerun your code to see if the stack overflow disappears. - If that fails, switch to a 64-bit JDK and test again.
- Only if both of the above aren't possible, implement the manual EPackage initialization approach.
内容的提问来源于stack exchange,提问作者sc560

