Jump to content

Shared variables creating memory leak


Recommended Posts

Thought I'd pass this along and see if anyone can reproduce with different versions of LabVIEW. Appreciate it if anyone has seen this and has a fix.

I'm using shared variables to communicate between applications (1:N). I'd been seeing some memory creep that was inconsistent and somewhat bizarre. Eventually managed to track it down to that I'm programmatically opening a connection to a shared variable in one loop, then reading the value in a different loop (the different loops have to do with reconnecting on connection loss and startup). There is a functional global used to pass the variable to the second loop. The Read Variable primitive deallocates all but 4 bytes of memory for the previous loop handle and then allocates memory for a new handle on each iteration of the while loop, hence creating a leak. This behavior does not occur if there is only one loop where there is an open, while loop with a read, and a close.

Main.vi demonstrates the issue. Main 2.vi is more like the NI example.

I've got service request #7728859 with NI going, but I think I got the guy's first day.

LabVIEW 2015 SP1 32-bit on Win7 64-bit. Shared Variables memory leak.zip

Link to post
Share on other sites
6 hours ago, Tim_S said:

Eventually managed to track it down to that I'm programmatically opening a connection to a shared variable in one loop, then reading the value in a different loop (the different loops have to do with reconnecting on connection loss and startup). There is a functional global used to pass the variable to the second loop. The Read Variable primitive deallocates all but 4 bytes of memory for the previous loop handle and then allocates memory for a new handle on each iteration of the while loop, hence creating a leak. This behavior does not occur if there is only one loop where there is an open, while loop with a read, and a close.

I don't have a fix for the leak (and I haven't investigated it in detail), but I have an alternative architecture for auto-connecting comms.

Instead of opening/closing in one loop and passing the variable reference to another loop, is it feasible to keep everything in one loop using a state machine? The states could be:

  • Offline (the only state that's allowed to quit the loop)
  • Connecting (contains the Open Variable node)
  • Online (contains the Read Variable node)
  • Disconnecting (contains the Close Variable node)

Button clicks, panel close events, and read errors can be used to trigger state transitions.

Link to post
Share on other sites

I don't know if I'm looking at the same as you, and I haven't investigated either, but I don't see leaks. LV17 32bit Win (where else do you have SV?) 10. I suppose allocation and deallocation sizes might depend on the variable content too, don't they?

My trace has many more events, are you somehow filtering them? I don't fully understand your throttling of the second loop based on timeout of an occurrence, but there you know better, maybe it has to do with your architecture at large.

Untitled Project 1.lvproj.det

DETT_2018-01-11_10-20-15.png

Link to post
Share on other sites
11 hours ago, JKSH said:

I don't have a fix for the leak (and I haven't investigated it in detail), but I have an alternative architecture for auto-connecting comms.

Instead of opening/closing in one loop and passing the variable reference to another loop, is it feasible to keep everything in one loop using a state machine? The states could be:

...

If I have to go back and refactor then I'll have something closer to that. The code is a communication library that gets used by anything trying to talk to an application. The Initialize launches a VI that opens and maintains the connection and then individual VIs get used to read the shared variables (thus the code using the library only reads what is needs). There's certainly other ways to do this, but it's worked well except for the memory leak.

Link to post
Share on other sites
5 hours ago, ensegre said:

I don't know if I'm looking at the same as you, and I haven't investigated either, but I don't see leaks. LV17 32bit Win (where else do you have SV?) 10. I suppose allocation and deallocation sizes might depend on the variable content too, don't they?

My trace has many more events, are you somehow filtering them? I don't fully understand your throttling of the second loop based on timeout of an occurrence, but there you know better, maybe it has to do with your architecture at large.

Untitled Project 1.lvproj.det

Sorry, should have mentioned I set the capture settings to have a memory threshold of 500 bytes. There are a lot of typically uninteresting allocations/deallocations occurring that spam the capture otherwise.

The memory allocated for a shared variable handle I would expect to be the same for a particular data type independent of the contents, though I've not dived this deep into the bowels of shared variables before.

The use of the occurrence is (an old) way to throttle loop rate and control parallel loop termination; used to see it quite a bit before events were added to LabVIEW. The bottom loop keeps running until the top loop ends at which point the occurrence is set. The timeout of the occurrence acts the same as a Wait (ms) of the same time.

Link to post
Share on other sites
3 hours ago, Tim_S said:

Sorry, should have mentioned I set the capture settings to have a memory threshold of 500 bytes.

If I put such a threshold I see no memory events at all. Is you trace dependent on some particular data already present in your SV? Otherwise, it may be that the issue is solved in LV2017.

Edit: Ok, with more data in the string I see this (a different sequence of memory operations in fact):

Capture.PNG

But does this indicate a leak? I note the free of 4 bytes, which maybe you squelched with the threshold.

Edited by ensegre
Link to post
Share on other sites
20 hours ago, ensegre said:

But does this indicate a leak? I note the free of 4 bytes, which maybe you squelched with the threshold

Drat, you're right... Today I've had an IT-pushed Windows update and reboot that has changed the behavior to closer to your screenshot. I was able to eliminate the memory leak by completely eliminating all of the Read Variables in my application, so I know something's related.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Similar Content

    • By Ram Prakash
      Can anyone please tell what a DVR [ Data value reference ] is ? I want to know at what situation it will be used and what are the advantages we get by using DVR. I am really confused in this topic . If someone has any code in which they have worked with DVRs. kindly share it to me.
       
      Thank you.
    • By torekp
      DLL functions or shared variables?  Or something else?
      I have a Labview 2014-64 executable (or I can build a DLL) that runs one piece of equipment, the X-ray.  The other engineer has a large CVI Labwindows 2015 + MS Visual Studio 2012 (C++) executable that runs everything else.  I want the Labview code to be a slave of the CVI code, accepting commands to turn X-ray On or Off, reporting failures, and the like.  Translating the X-ray code into C++ would be possible in principle, but not fun.
      Shared variables look easy, but I'm kinda scared of them.  I would define all the shared variables in my LV code, since I'm more familiar with LV, then use them in both.  There's a thread in here called "Shared Variable Woes" so maybe I should be scared.  In the alternative, I tried building a proof-of-concept DLL in Labview, and calling its functions in CVI/C++, and it works, but it's kinda clunky.  (I'm attaching it below in case you want to play, or advise.)
      Your advice would be appreciated.
      XrayDLL.zip
    • By ensegre
      More out of curiosity than of hope: has anybody any idea why SVs are almost unsupported on linux? By almost I mean that controls and indicators cannot be bound to shared variables, and that shared variables cannot be programmatically created and looked up. I know that SVs hosted on windows can be accessed in linux LV using datasocket nodes, but that is all it gets. And it has been said that datasocket is despicable. What are the missing pieces that make SV windows-only?
      I didn't find much in the canonical places, so I posted a dumb zero-kudos attracting idea.
    • By PJM_labview
      Hi
      We have an application where we need to have a custom PCIe board transfer data to the PC using DMA.
      We are able to get this to work using NI-VISA Driver Wizard for PXI/PCI board.
      The recommended approach is to have VISA allocate memory on the host (PC) and have the PCIe board write to it (as seen below).

      While this approach works well, the memory allocated by VISA for this task is quite limited (~ around 1-2MB) and we would like to expand this to tens of MB.
      Note: The documentation (and help available on the web) regarding these advanced VISA function (for instance "VISA Move out 32" and "VISA Move In 32") is parse. If someone has some deep knowledge regarding theses, please feel free to share how we could allocate more memory.
      Since we are not able to allocate more memory using the VISA function at this time, we investigate doing the same operation using the LabVIEW Memory Manager Functions which allow us to allocate much larger memory block.
      Below is the resulting code.

      Unfortunately while we can validate that reading and writing to memory using this work well outside the context of DMA transfer, doing a DMA transfer do NOT work (although the board think it did and the computer is not crashing).
      We are wondering why this is not working and would welcome any feedback.
      Note: the DMA transfer implemented on the board requires contiguous memory for it to be successful. I believe that the LabVIEW Memory Manager Functions do allocate continuous memory, but correct me if I am wrong.
      To troubleshoot this, I did allocate memory using the LabVIEW memory manager function and try to read it back using VISA and I got a "wrong offset" error (Note: This test may not be significant)
      Another data point; while the documentation for DSNewPtr do not talk about DMA transfer, the one for DSNewAlignedHandle does. Experiment using LV memory manager Handles has not got us anywhere either.
      We would welcome any feedback regarding either approach and about the LabVIEW Memory Manager Functions capabilities in that use case.
      Thanks in advance.
      PJM
      Note: We are using LabVIEW 2014 if that matter at all.
       
    • By Steen Schmidt
      Hi,
       
      I've decided to take the temperature on a known issue, that lvclass files retain knowledge of some of their old content after it's deleted.
       
      Proof
      For some reason I'm not allowed to upload lvclass files, so I'll describe it instead (using LV2014SP1):
       
      1) Create a new class and save it on disk as class1.lvclass. No member data nor methods, file size on disk is 8 kB.
       
      2) Add one piece of significantly sized (to easier see the issue) member data, I added a 1000x100 array of DBL (with random default data in it). Save the class again, and now class1.lvclass is 4604 kB (why so much, should be around 1000 kB?).
       
      3) Delete all member data again and resave the class. File size on disk is now 1171 kB, I'd have expected 8 kB.
       
      4) I can't ever get rid of that extra data in the lvclass file, not even when I "save as" to create a similar class.
       
      Questions
      A) What's the reason behind this issue?
       
      B) Is there any way to really delete stuff from a class file, or is the only way to recreate every class from scratch if you want something truly gone?
       
      C) Is there a list (perhaps internal to NI) of which problems this issue causes? Here I'm talking about stuff like this and numerous other threads about class data suddenly not being updated or member data or methods not being called correctly with DD.
       
      Cheers,
      Steen
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.