2

We have software written in Python, which uses JPype to call Java, which performs various resource heavy calculations / report building. We originally assigned 800mb of heap space when starting the JVM. The java side is fully multithreaded and will work with whatever resources are available to it.

jvmArgs = ["-Djava.class.path=" + classpath, "-Xmx800M"]
jpype.startJVM(u"java\\jre8\\bin\\client\\jvm.dll", *jvmArgs)

This worked well until we tested on Windows XP for our legacy clients. The new machines are Win 7 64-bit with 4GB of RAM, whereas the old ones are Win XP 32-bit with only 2 GB of ram.

The issue is that JPype causes our application to ungracefully and silently crash if we allocate too much memory. A try catch doesn't even get triggered on the statement above.

I'm wondering if there's a way to use java from command line to determine how much memory we can allocate on a computer. We can check if it's 32-bit or 64-bit which helps, but we need to make sure they aren't running other programs taking up heap space on the JVM. If they are, our application will crash.

Reader's Digest: We'd like to allocate 500mb of heap space when initializing the JVM, but can't be sure of how much space is currently being used. If we allocate too much, the entire application silently crashes.

We use the following JPype: 0.5.4.2 Python: 2.7 Java: 1.8 or 1.7 (64-bit or 32-bit)

Thanks.

4

1 回答 1

1

JVM 消耗的内存包括 2 个主要区域:

  1. 堆内存
  2. 非堆内存 -元空间本地方法堆栈pc 寄存器、直接字节缓冲区、套接字、jni 分配内存、线程堆栈

虽然将用于堆内存的最大大小是已知且可配置的,但无法完全控制非堆内存的大小。
JVM 使用的本机内存大小将受您使用的线程数、正在加载的类数量和缓冲区的使用(I/O 的使用)的影响。
您可以通过设置 MaxMetaspaceSize (-XX:MaxMetaspaceSize) 来限制元空间的大小。您可以通过限制线程数和设置线程堆栈大小 (-Xss) 来控制用于线程堆栈的内存量。假设您没有本机内存泄漏,正在加载的类的数量是稳定的(没有过度使用动态代理和字节码生成)并且正在使用的线程数量是已知的 - 您可以推测您的应用程序需要多少内存通过监视 JVM 在一段时间内使用的整体内存来运行。执行此操作时,请确保在 JVM 启动时分配整个堆。

于 2014-11-26T19:42:25.943 回答