6

I'm running some Java code through Python's pyjnius package, version 1.2.0. There are several Python processes that each call the same Java class with different inputs, so each one initializes its own JVM. The problem I'm having is a memory issue, but in researching that, I found that the options in the running JVMs aren't the same as what I'm setting in my Python code.

I'm setting the options with:

jnius_config.add_options('-Xms512m', '-Xmx1280m')

After the Java code is running I run jps from a command line and I get the following output:

C:\>jps -lvm
25140  -Xmx1280m -Xmx1280m
24684  -Xmx1280m -Xmx1280m

I tried different arguments to add_options, and jps always reports two copies of the second argument for each of the JVMs. I also tried using set_options and got the same results.

I checked that the options are set in the pyjnius module itself using jnius_config.get_options() before and after adding options.

print("Initial options:", jnius_config.get_options())
jnius_config.add_options('-Xms512m', '-Xmx1280m')
print("Configured options:", jnius_config.get_options())

It reports the same values that I set.

Initial options: []
Configured options: ['-Xms512m', '-Xmx1280m']

But the JVMs still seem to be running with the wrong options set when I check them in jps. Other running JVMs report different options, so I don't think this is a problem in jps, since only the processes run through pyjnius are giving me back the wrong values.

Does anyone know if this is a bug in pyjnius? Is there another way to check the options a JVM was launched with?


I wasn't sure if I should trust the values reported by jps or pyjnius, but I see the following lines in an error log created when running my Python script:

VM Arguments:
jvm_args: -Xmx1280m -Xmx1280m
java_command: <unknown>

It looks pretty conclusive that pyjnius is setting the wrong options.


I experimented a bit with the initial memory size option and found that this problem only exists when the initial memory size is -Xms100m or greater. When I set the initial value to -Xms99m or lower, jps reports the same values that I set.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
  • Can you add some more info, what happens when you add `print(jnius_config.get_options())` before and after your command `jnius_config.add_options('-Xms512m', '-Xmx1280m')`? – ands Apr 27 '20 at 23:16
  • Hi @ands. I added those lines and a few other details to the question. – Bill the Lizard Apr 28 '20 at 12:36
  • 1
    the doc says: JVM options need to be set before import jnius is called, as they cannot be changed after the VM starts up. –  Apr 28 '20 at 13:22
  • @Voodoo I am setting the options in a separate initialization function before I import jnius. – Bill the Lizard Apr 28 '20 at 13:42
  • Can you post whole error log? Also, I am not really familiar with pyjnius and JVM, but can you run this program directly in command line using arguments `-Xms512m -Xmx1280m`? – ands Apr 29 '20 at 13:02
  • @ands Sorry, I can't post the whole log. It's a JVM "out of memory" error log. I think the part that I posted is the only bit relevant to this particular issue. - I can run the program from the command line using those arguments and it runs fine, but it's part of a much larger process that's launched from an in-house Python framework, so executing it from pyjnius is a must. – Bill the Lizard Apr 29 '20 at 19:05

1 Answers1

0

Maybe you should try to provide a simple minimal-reproducible-example which can be used to reproduce the behavior.

Following quick check was working as expected.

used software versions

$ python -V
Python 3.8.2
$ java -version
java version "1.8.0_202"
  • create a fresh Python venv and execute pip install pyjnius==1.2.0
  • check installed packages version

    $ pip freeze
    Cython==0.29.17
    pyjnius==1.2.0
    six==1.14.0
    
  • create a minimal example minimal.py

    import jnius_config
    
    print("Initial options:", jnius_config.get_options())
    jnius_config.add_options('-Xms128m', '-Xmx256m')
    print("Configured options:", jnius_config.get_options())
    
    from jnius import autoclass
    Thread = autoclass('java.lang.Thread')
    Thread.sleep(60_000)
    
    System = autoclass('java.lang.System')
    System.out.println('Hello World')
    
  • start the example two times

    session 1 $ python minimal.py 
    Initial options: []
    Configured options: ['-Xms128m', '-Xmx256m']
    
    session 2 $ python minimal.py 
    Initial options: []
    Configured options: ['-Xms128m', '-Xmx256m']
    
  • check running Java processes

    $ jps -v
    191594  -Xms128m -Xmx256m
    191615  -Xms128m -Xmx256m
    
SubOptimal
  • 22,518
  • 3
  • 53
  • 69