0

I have a Java app that imports another jar as a library and calls its main method as shown below. But someApp is a very large process and constantly throws an OutOfMemoryError. No matter what I set my Java apps heap size to, someApp does not seem to share the allocated memory.

try {
    someApp.main(args);
} catch (Exception ex) {
}

How do I get someApp to allocate more heap space? Can I use processBuilder? What do I do?

Thanks.

Jakir00
  • 2,023
  • 4
  • 20
  • 32

4 Answers4

3

As it stands at the moment, you're merely calling a class from another application within your own Java process. This is exactly the same as you'd do for calling a "library method" (the term has no technical difference, you're simply invoking a method on an object of a class that can be resolved by your classloader).

So right now, someApp is running in the same JVM as your own application, and will share its maximum heap size. This can be increase with the JVM argument -Xmx (e.g. -Xmx2048m for a 2GB max heap), though it sounds like you're doing this already withotu success.

It would be possible to launch someApp in a separate Java process, which would allow you to configure separate JVM arguments and thus give it a separate heap size.

However, I don't think this is going to help much. If you're unable to get this application to run in the same JVM, regardless of your heap limit, there's nothing that would suggest it would work in a difference JVM. For example, if you're running with a 2.5GB heap and still running out of memory, running your own app with a 0.5GB heap and spawning a separate JVM with 2GB heap will not solve the problem, as something is still running out of memory. (In fact, separate memory pools make an OOME slightly more likely since there are two distinct chunks of free space, whereas in the former case both applications can benefit from the same pool of free space).

I suggest you verify that your heap sizes really are being picked up (connecting via JMX using JConsole or JVisualVM will quickly let you see how big the max heap size is). If you're really still running out of memory with large heaps, it sounds like someApp has a memory leak (or a requirement for an even larger heap). Capturing a heap dump in this case, with the JVM argument -XX:+HeapDumpOnOutOfMemoryError, will allow you to inspect the heap with an external tool and determine what's filling the memory.

Hopefully you've simply failed to increase the heap size correctly, as if the application really is failing with a large heap there are no simple solutions.

Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228
  • Now that I think about it, this only happens when running on Windows. I use launch4j to wrap the jar around an exe. I might not be specifying the heap size correctly in that. – Jakir00 Jan 26 '11 at 20:22
1

Unless someApp itself is building a new process, this will already be in the same process as your calling code, so it should be affected by whatever heap configuration you've set when starting up the JVM.

Have you kept track of how much memory the process is actually taking?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

This doesn't make sense, unless you're running into OS limitations on how much memory you can allocate to a single Java process on your OS (see Java maximum memory on Windows XP)

Short of that, the way you're invoking someApp, it acts as a regular library. The main method acts like any other method.

Have you tried debugging the OutOfMemoryError? There may be something obscure that the app doesn't like about being invoked from your application...

Community
  • 1
  • 1
ykaganovich
  • 14,736
  • 8
  • 59
  • 96
1

If the jar you are importing is authored by you and could be more efficient, then modify it. It sounds like the problem you are having is loading in a shotty package. If this is a 3rd party package and you are allowed to modify it, poke around in the code and find where there might be limitations, change it, and rebuild it.