Memory leak using jdbc-mysql in rules

I am still in the transition from 2.5 to 3.x. The progresses are slow but constant, as I have many rules, items and scripts.

Up to now, especially one script is creating instantly a memory leak and crashing openhab:

rule "Smartmeter update"
when
    Time cron "0 */5 * * * ?" or
	Item System_Start received command ON
then
    // Berechnungen Hausanschluss
    var Number lastHourIn = (Smartmeter_Wh_In.deltaSince(now.minusHours(1),"jdbc").state as DecimalType).intValue
    if (lastHourIn < 0) {
        lastHourIn = lastHourIn + (Smartmeter_Wh_In.maximumSince(now.minusHours(1),"jdbc").state as DecimalType).intValue
    }
    Smartmeter_Wh_In_1h.postUpdate(lastHourIn)

    var Number lastHourOut = Smartmeter_Wh_Out.deltaSince(now.minusHours(1),"jdbc").state as DecimalType).intValue
    if (lastHourOut < 0) {
        lastHourOut = lastHourOut + (Smartmeter_Wh_Out.maximumSince(now.minusHours(1),"jdbc").state as DecimalType).intValue
    }
    Smartmeter_Wh_Out_1h.postUpdate(lastHourOut)

    var Number last24hIn = Smartmeter_Wh_In.deltaSince(now.minusHours(24),"jdbc").state as DecimalType).intValue
    if (last24hIn < 0) {
        last24hIn = last24hIn + (Smartmeter_Wh_In.maximumSince(now.minusHours(24),"jdbc").state as DecimalType).intValue
    }
    Smartmeter_Wh_In_24h.postUpdate(last24hIn)

    var Number last24hOut = Smartmeter_Wh_Out.deltaSince(now.minusHours(24),"jdbc").state as DecimalType).intValue
    if (last24hOut < 0) {
        last24hOut = last24hOut + (Smartmeter_Wh_Out.maximumSince(now.minusHours(24),"jdbc").state as DecimalType).intValue
    }
    Smartmeter_Wh_Out_24h.postUpdate(last24hOut)

    // Berechnungen Wärmepumpe
    var Number lastHourIn = (SmartmeterWP_Wh_In.deltaSince(now.minusHours(1),"jdbc").state as DecimalType).intValue
    if (lastHourIn < 0) {
        lastHourIn = lastHourIn + (SmartmeterWP_Wh_In.maximumSince(now.minusHours(1),"jdbc").state as DecimalType).intValue
    }
    SmartmeterWP_Wh_In_1h.postUpdate(lastHourIn)

    var Number lastHourOut = (SmartmeterWP_Wh_Out.deltaSince(now.minusHours(1),"jdbc").state as DecimalType).intValue
    if (lastHourOut < 0) {
        lastHourOut = lastHourOut + (SmartmeterWP_Wh_Out.maximumSince(now.minusHours(1),"jdbc").state as DecimalType).intValue
    }
    SmartmeterWP_Wh_Out_1h.postUpdate(lastHourOut)

    var Number last24hIn = (SmartmeterWP_Wh_In.deltaSince(now.minusHours(24),"jdbc").state as DecimalType).intValue
    if (last24hIn < 0) {
        last24hIn = last24hIn + (SmartmeterWP_Wh_In.maximumSince(now.minusHours(24),"jdbc").state as DecimalType).intValue
    }
    SmartmeterWP_Wh_In_24h.postUpdate(last24hIn)

    var Number last24hOut = (SmartmeterWP_Wh_Out.deltaSince(now.minusHours(24),"jdbc").state as DecimalType).intValue
    if (last24hOut < 0) {
        last24hOut = last24hOut + (SmartmeterWP_Wh_Out.maximumSince(now.minusHours(24),"jdbc").state as DecimalType).intValue
    }
    SmartmeterWP_Wh_Out_24h.postUpdate(last24hOut)

    // Berechnungen Hausanschluss Total
    if ((Smartmeter_Wh_In.state as DecimalType).intValue < (Smartmeter_Wh_In_Last.state as DecimalType).intValue) {
        Smartmeter_Wh_In_Correct.postUpdate((Smartmeter_Wh_In.maximumSince(now.minusHours(24),"jdbc").state as DecimalType).intValue)
    }
    Smartmeter_Wh_In_Last.postUpdate((Smartmeter_Wh_In.state as DecimalType).intValue)
    Smartmeter_Wh_In_All.postUpdate((Smartmeter_Wh_In.state as DecimalType).intValue + (Smartmeter_Wh_In_Correct.state as DecimalType).intValue)

    if ((Smartmeter_Wh_Out.state as DecimalType).intValue < (Smartmeter_Wh_Out_Last.state as DecimalType).intValue) {
        Smartmeter_Wh_Out_Correct.postUpdate((Smartmeter_Wh_Out.maximumSince(now.minusHours(24),"jdbc").state as DecimalType).intValue)
    }
    Smartmeter_Wh_Out_Last.postUpdate((Smartmeter_Wh_Out.state as DecimalType).intValue)
    Smartmeter_Wh_Out_All.postUpdate((Smartmeter_Wh_Out.state as DecimalType).intValue + (Smartmeter_Wh_Out_Correct.state as DecimalType).intValue)
    
    // Berechnungen Wärmepumpe Total
    if ((SmartmeterWP_Wh_In.state as DecimalType).intValue < (SmartmeterWP_Wh_In_Last.state as DecimalType).intValue) {
        SmartmeterWP_Wh_In_Correct.postUpdate((SmartmeterWP_Wh_In.maximumSince(now.minusHours(24),"jdbc").state as DecimalType).intValue)
    }
    SmartmeterWP_Wh_In_Last.postUpdate((SmartmeterWP_Wh_In.state as DecimalType).intValue)
    SmartmeterWP_Wh_In_All.postUpdate((SmartmeterWP_Wh_In.state as DecimalType).intValue + (SmartmeterWP_Wh_In_Correct.state as DecimalType).intValue)
    
    if ((SmartmeterWP_Wh_Out.state as DecimalType).intValue < (SmartmeterWP_Wh_Out_Last.state as DecimalType).intValue) {
        SmartmeterWP_Wh_Out_Correct.postUpdate((SmartmeterWP_Wh_Out.maximumSince(now.minusHours(24),"jdbc").state as DecimalType).intValue)
    }
    SmartmeterWP_Wh_Out_Last.postUpdate((SmartmeterWP_Wh_Out.state as DecimalType).intValue)
    SmartmeterWP_Wh_Out_All.postUpdate((SmartmeterWP_Wh_Out.state as DecimalType).intValue + (SmartmeterWP_Wh_Out_Correct.state as DecimalType).intValue)
end

I think it is the integration of ‘jdbc-mysql’ requests

Any help would be appreciated

There has been work to do with rules and leaks, so .x version is important here.

Your rule certainly hammers your persistence. What leads you to think it is the culprit here? What symptoms do you see?

There are some race conditions in your rule that you may have missed. (These would affect “normal” results, not your leak issue)

In the abstract, Smartmeter_Wh_In_Correct.postUpdate() is followed by a calculation involving Smartmeter_Wh_In_Correct.state
Postupdate is asynchronous, it is fired off onto openHABs events bus and your rule does not stop and wait for it to take effect. When you examine the Item state a line or two later, the update may or may not yet have taken effect, You might get “old” or “new” state, it’s indeterminate.

The circumvention in a rule is to use the new state that you already know, not read the Item.
In your case, fetch the Item state. Decide to modify your local variable,or not, and postUpdate back to your Item. Then use the local variable in your calculation.

Thank you for the input. Meanwhile I found a main issue. I was using the ‘old’ mysql database from openhab 2.5 and somehow the table to item allocation messed up. Using a fresh database solved my main issue and at least the script is now running (with modifications for the issues you mentioned)

By the way, I am running 3.1M2, as startup with 3.0.1 is not working (memory leak)

Here is the discussion on the leak found

Listen to Rossko’s advice

use the Implicit Variables… look here