【问题标题】:JPype won't compile properly when run the code twice两次运行代码时JPype无法正确编译
【发布时间】:2018-10-04 10:16:09
【问题描述】:

我的代码:

jarLocation = "C:/Users/LahiruGunawardhana/Desktop/New folder/info/infodynamics.jar"
# Start the JVM (add the "-Xmx" option with say 1024M if you get crashes due to not enough memory space)

jpype.startJVM(jpype.getDefaultJVMPath(), "-ea", "-Djava.class.path=" + jarLocation,"-Xmx256m")

teCalcClass = jpype.JPackage("infodynamics.measures.discrete").TransferEntropyCalculatorDiscrete
teCalc = teCalcClass(2,1)
teCalc.initialise()

noOfEdges=0
for i in range(0,N-1):
  for j in range(i+1,N):
   # print(i,"-->",j)
    if(nodes_connectivity(j,i) | nodes_connectivity(i,j)):

     sss = [int(i) for i in SsourceArray]
     ddd = [int(i) for i in DdestArray]
     teCalc.addObservations(sss, ddd)
     e=teCalc.computeAverageLocalOfObservations()

jpype.shutdownJVM()

我只为这段代码获取一次值。当我第二次运行此代码时,我收到一条错误消息:

_jpype.startup(jvm, tuple(args), True)

OSError: JVM is already started

不知何故,当我为每个步骤重新启动 python 控制台时,它会给出值。

【问题讨论】:

    标签: python anaconda jpype


    【解决方案1】:

    startJVM() 之前检查 isJVMStarted() 所以它会是这样的:

    if jpype.isJVMStarted():
        return
    
    jpype.startJVM(jpype.getDefaultJVMPath(), "-ea", "-Djava.class.path=" + jarLocation,"-Xmx256m")
    

    因此,如果您运行两次并且 JVM 正在运行,则此代码将避免再次对其进行初始化

    【讨论】:

    • 谢谢。如何在我的代码中使用选项“-Xmx”,因为多次运行代码时由于内存不足而导致崩溃。
    • @LahiruGunawardana -Xmx 指定最大堆大小,所以现在你有了“-Xmx256m”。这意味着您最多有 256MB。尝试使用“-Xmx512m”(512MB)、“-Xmx1024m”(1024MB)或“-Xmx2048m”(2048MB)来增加数字。
    • 所以代码是:jpype.startJVM(jpype.getDefaultJVMPath(), "-ea","-Xmx1024m" ,"-Djava.class.path=" + jarLocation)
    猜你喜欢
    • 2013-02-08
    • 2016-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多