Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why my app leaks memory ? How to avoid memory leakage?

I'm writing an OPC client so I use the Python OpenOPC library.

The problem is each time I'm reading a list of OPC items, my app consume memory.

For example, the following code consume about 100ko at each iteration :

#!/usr/bin/python
# -*- coding: utf-8 -*-

import OpenOPC
import time
import gc

gc.set_debug(gc.DEBUG_LEAK)

client = OpenOPC.client()
while True:
    client.connect('CODESYS.OPC.DA')
    dataList = client.list("PLC2.Application.GVL.*")
    res = client.read(dataList)
    client.close()
    print gc.collect()
    print gc.garbage

    time.sleep(2)

and the garbage collector returns :

0
[]

The memory is released when I close the app.

So I don't understand why my app leaks memory and how avoid this.

Have you some ideas ? Thanks

like image 450
Loïc G. Avatar asked Oct 21 '22 23:10

Loïc G.


1 Answers

Found a solution by using the group argument of the read() function :

#!/usr/bin/python
# -*- coding: utf-8 -*-

import OpenOPC

client = OpenOPC.client()
client.connect('CODESYS.OPC.DA')
tags = client.list("PLC2.Application.GVL.*")
while True:
    res = client.read(tags, group='MyGroup')
client.close()
like image 173
Loïc G. Avatar answered Oct 28 '22 21:10

Loïc G.