chrisdutz commented on issue #1935:
URL: https://github.com/apache/plc4x/issues/1935#issuecomment-2660871396

   Ok ... so I think I've solved the mystery ... 
   
   You were using the OPM the wrong way. However I don't blame you, I 
intuitively would have done it the same way.
   
   It seems that `PlcEntityManager.read` is a shortcut to read all values of an 
OPM Entity, but it doesn't keep the entity "live", but disconnects after 
reading the values and returns a "dead" entity.
   
   If you however call `PlcEntityManager.connect` you get a "live" entity back 
and whenever you call the getter of one of the fields, the OPM will 
transparently fetch the data in the background. You can fine tune, if you 
access properties often and don't want each access to result in a PLC read, 
then you can provide a `cacheDurationMillis` in the `@PlcTag` annotation. 
   
   So if I change your code to the following code, I no longer see the memory 
usage increase:
   
   ```
    @Test
       void shouldRead() throws PlcConnectionException {
           MockDevice mockDevice = Mockito.mock(MockDevice.class);
           DefaultPlcDriverManager driverManager = new 
DefaultPlcDriverManager();
           MockConnection connection = (MockConnection) 
driverManager.getConnection("mock:test");
           when(mockDevice.read(any())).thenAnswer(invocation -> {
               System.out.println("Reading");
               return new DefaultPlcResponseItem<>(PlcResponseCode.OK, new 
PlcSTRING("1")) {};
           });
           connection.setDevice(mockDevice);
   
           PlcEntityManager entityManager = new PlcEntityManager(driverManager);
   
           try {
               FooRxEntity entity = entityManager.connect(FooRxEntity.class, 
"mock:test");
               while (true) {
                   try {
                       System.out.println(entity.getBf102Setpoint());
                       Thread.sleep(10);
                   } catch (InterruptedException e) {
                       throw new RuntimeException(e);
                   }
               }
           } catch (OPMException e) {
           }
       }
   ```
   I am currently updating the documentation on the OPM.
   
   
![Image](https://github.com/user-attachments/assets/27543631-2c0f-436f-849a-45ed27105079)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to