Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


Recommended Posts

Posted

Hi,

 

I have seen that my RAM usage is slowly growing every day.

Im sure its something from my side (Not due to HC3) but i cant figure out what.

 

Any suggestions?

 

BR

  • 1 month later...
  • Topic Author
  • Posted (edited)

    Still having some issues with this..

    It usually grows quite quickly up to 85% and then it slows down down (takes a couple of days before it reaches 86%)

    Right now i have 22 scenes, 15 QAs, 103 Z-wave devices. 

     

    What is your RAM usage?

     

    Just so im not chasing ghosts..

     

    BR

    Edited by 747
    Posted

    Please login or register to see this image.

    /monthly_2026_02/image.png.66dc6665bf92a778bd2e7ab647b90fd8.png" />:))))))))))))))))))))))))))))))))))))))))))))))))))))))))))

     

    Posted

    Thats doesn't say anything. It depends on how much "used space" there is. You will probably see that will be the halve of those 96%, at least, I hope for your sake :-) 

  • Topic Author
  • Posted
    16 hours ago, petro_cz said:

    Please login or register to see this link.

    :))))))))))))))))))))))))))))))))))))))))))))))))))))))))))

     

    Ok.. Maybe i shouldnt complain then..

    Does your system work as expected when you have this high RAM usage?

    Does it grow even further that that?

     

    BR

    Posted

    I have about 47% ram usage or so. And the systeem works great. 
     

    What is your ram usage?

     

    Do you experience lag or something? 

    Posted

    In my case with 3 z-wave devices, 45 quick apps and 10 cameras:

    Please login or register to see this attachment.

    Not installed latest beta.

  • Topic Author
  • Posted

    Wow, 47% and 51%.. Directly after a reboot im at 77%..

     

    In some cases i experience some lagging, e.g, when locking the front door i call a scene that will turn off all lights in the house (Usually just takes 2-3s) and if i go to the garage meanwhile and trigger the motion sensor, it sometimes takes a couple of seconds for the lights to turn on.

     

    So small things yes, but if you have around 50% RAM usage then im thinking im doing something wrong..

     

    BR

     

    Posted (edited)

    Actually in my case is 51% free, therefore 49% used. This is reached after two weeks from last reboot.

    Usually after reboot it goes to 43% used RAM and by several weeks grows to 51% used RAM. Till now it was never more than 52% used RAM.

     

    Still, as I mentioned, I have only 3 z-wave devices included (1 door/window sensor and 2 Fibaro plugs for testing) on my HC3. My quick apps use garbage collector to free used memory regularly.

     

    This proved to be useful on my HC2 too. With 116 z-wave devices, 106 scenes and 152 Virtual devices free RAM never drops bellow 41% (RAM is expanded to 2Gb, while original is 1Gb). CPU usage on the other hand is at its peak on some events causing some acceptable lagging during that short time.

    Edited by Sankotronic
    Posted
    6 hours ago, 747 said:

    Ok.. Maybe i shouldnt complain then..

    Does your system work as expected when you have this high RAM usage?

    Does it grow even further that that?

     

    BR

    Yes, sometimes there is a delay, but it works. Of course, it would be nice if the memory were larger, but that is determined by the hardware configuration. We need a new version!!

  • Topic Author
  • Posted (edited)
    1 hour ago, Sankotronic said:

    Actually in my case is 51% free, therefore 49% used. This is reached after two weeks from last reboot.

    Usually after reboot it goes to 43% used RAM and by several weeks grows to 51% used RAM. Till now it was never more than 52% used RAM.

     

    Still, as I mentioned, I have only 3 z-wave devices included (1 door/window sensor and 2 Fibaro plugs for testing) on my HC3. My quick apps use garbage collector to free used memory regularly.

     

    This proved to be useful on my HC2 too. With 116 z-wave devices, 106 scenes and 152 Virtual devices free RAM never drops bellow 41% (RAM is expanded to 2Gb, while original is 1Gb). CPU usage on the other hand is at its peak on some events causing some acceptable lagging during that short time.

    Hi @Sankotronic,

     

    Do you have some info about the garbage collector that you use?

    Sounds like it would be a good thing to use..

     

    Also, should i use more QAs rather than Scenes?

    Edited by 747
    Posted

    btw @747 it is also useful to check the z-wave diagnostics for z-wave traffic. If a z-wave device is reporting a lot, you can also experience delay. 

    • Like 1
    Posted

    Hi @747,

     

    To make story short you can find more about garbage collector here: 

    Please login or register to see this link.

     

    and how to call it inside code here: 

    Please login or register to see this link.

     

     

    Since Lua performs automatic memory management, normally we do not need to worry about it.

     

    But during making UHAS for HC2 I noticed that forcing garbage collector to clean memory helped somehow since I always get some used memory free, by checking used memory before cleaning and after cleaning:

    Please login or register to see this image.

    /monthly_2026_02/GCOMWglobalvar.png.43dfd21d6e543c1473576718be65f32e.png" />

     

    Above debug shows used memory before and after GC (garbage collector) run after quick app is initialized and global variables are checked.

     

    After each call to OMW server is done next GC run is done with following result:

    GCOMWafterservercall.png.e7f62093aaca21296ff0e08526266051.png

     

    There is no noticeable impact on resources and time needed for GC to do the cleaning.

     

    Besides using GC, I also always destroy data tables when not needed any more. For example after data tables named "data" or "forecast" received from OMW servers are not needed then I add code:

    Please login or register to see this code.

    and this is done before calling GC.

     

    I'm sure and hoping that @jgab will give you more accurate information about this and correct me. I can't say that I understand this correctly, it is just what I see as a result from using GC inside my code and how my HC2 is preforming in last 6 years since it is running my UHAS for HC2.

     

    And here is the function that I use for GC where parameter d is boolean true or false:

    Please login or register to see this code.

     

    For most of the temporary data I declare local variables inside the function where is needed and in case of table I destroy it with nil before exiting function code.

    All data that is important and should be available in the entire code I add to the quick app structure with self.

    usage of global variables is reduced to the minimum and I mostly use quick app internal storage. Only user editable data is stored in quick app variables and those that can be fetched from another quick app.

     

     

     

    • Like 1
  • Topic Author
  • Posted

    Thanks @Sankotronic!

     

    I will give it a try and see if it works good for me as well.

     

    BR

    Posted (edited)

    This topic is close to me and I have spent some time trying to understand it properly. 

    So I will be glad to share some thoughts.

     

    Modern programming languages use automatic memory management (unlike e.g. C), but this does not relieve the programmer from being careful, especially in systems with such limited resources as Fibaro.

    In Lua, additionally, once an object is allocated it does not change its address - at least in the Fibaro implementation, and I make use of this fact (this is different, as far as I know, in some Java VMs, where the GC performs memory compaction). 

     

    I recently counted the lines of code running on my HC - about 36,000 in ~20+ QA (I have 47% used RAM and it's constant. At start - ~40%). Imagine this is never sleeping, allocating memory, freeing it “at will” (or not freeing it at all), across many processes that are often interdependent. After some time, despite the GC working, the memory allocator will have increasing difficulty finding a free block of memory of sufficient size. This may be one of the reasons for the “slowdown” of the controller when memory becomes fragmented and - additionally - nearly full. Often the simplest action is a sanitary restart of the controller to recover occupied resources - nothing wrong with it.

     

    A separate issue is the quality of scripts and their impact on memory. When you experience a gradual decrease in available RAM, it may be worth taking a closer look at your scripts.
    I think the practices described by @Sankotronic are good ones. The only thing I would avoid is calling the GC too often.

     

    There are literally a hundred and one ways to degrade performance through RAM usage in Lua.

    As an example, consider strings - immutable objects, where every modification creates a new object.
    If you do:

    Please login or register to see this code.

    this does not “append” to the existing string - a new string is created, and the old one becomes garbage (to be collected by the GC), provided there are no remaining references to it.

    A typical anti-pattern would be:

    Please login or register to see this code.

    which creates n intermediate objects. 

    It is both more memory-efficient and faster to build strings in a table and use table.concat(...).

    If we have:

    Please login or register to see this code.

    the string "abc" will not disappear from memory, because there is still  reference to it via b.
    Therefore, assigning nil is not always sufficient. At the other hand - local objects ends it's life at function exit, no need to set it to nil.

    Other example that comes to mind is a cache that holds an object even though somewhere else we do object = nil:

    Please login or register to see this code.

    But if you do first line like this, I think the object (value) in cache will be destroyed and you should see "false" at the end:

    Please login or register to see this code.

     

    And so on…

    Edited by Łukasz997
    • Like 2
    • Thanks 1
    Posted

    Hi @Łukasz997,

     

    thanks for more details regarding how LUA allocate memory depending on the references in the code. 

    44 minutes ago, Łukasz997 said:

    The only thing I would avoid is calling the GC too often.

    I totally agree. In my case GC is called only once per each run of the loop if there is a loop running to fetch data from outside world and then show that data on the labels or child devices.

    During coding of UHAS for HC2 I noticed that it helps if large data tables are cleaned with nil before GC is called within the code. At least that is what LUA memory usage shows before and after GC is called:

    Please login or register to see this code.

     

    Regarding RAM memory fragmentation I believe that this also depends on the OS on which embedded system is running. For this reason I use UHAS System controller that actually reboots only UHAS (stopping all processes and then restarting them in a predefined sequence) that clears memory and solves "slowdown" of the system. Still, such action was not much needed with my HC2 since it is running UHAS.

     

    • Like 2
    • Thanks 1
    Posted (edited)

    After dozen of months working with Lua, I am confident about one of its interesting characteristics: it is easy to get started, but mastering Lua at an expert level is really hard. And I'm still far from that.

    *

    If you do not experience memory leaks in a program as large as UHAS, it seems that it is well written.
     

    Edited by Łukasz997
    • Like 3
    Posted

    Hi @Łukasz997,

     

    I'm far from being expert for LUA too. Still learning after 10+ years.

     

    BTW UHAS for HC2 was rewritten several times until I get it running.

     

    For Yubii UHAS I made library of functions and universal code for quick apps, like skeleton code which I use to make new quick apps.

    Everything needed is already in that code, so for new quick app I only need to add code specific to that quick app.

    This speeds up process of making quick apps leaving me more time for testing.

     

    I believe that @jgab plua and VSCode for offline coding can really help with quick app coding and speed up the process, but after all this years working with Zerobrane Studio it is very difficult for me to make that change. I was so happy when I realized that ZBS still work on my new iMac mini M4 and macOS Tahoe 26.2 :-D 

     

    Anyway, I first do coding. Make complete code in ZBS without testing and copy/pasting to HC3. When I feel that code is done then I test it.

    I like how ZBS shows code making it easy to see broken loops or if..then..ends. Also, since it is using different underlying for global and local variables it is easy to see if there is something not declared properly. Thanks to that in most cases my code runs either on first test, or I have to correct few typos. :-) 

     

    Not to mention that I get used to ZBS LUA syntax coloring that is exactly same as it is on HC2. I looked at that for years and I just can't get it in any other IDE, specially in VSCode. I know that it is possible to setup almost any small detail in VSCode, but that takes lots of time which I'm still not ready to invest, not to mention that it is Microsoft (Microshit) product ;-) 

     

    • Like 1
    Posted

    Dear @Sankotronic,

    The discussion is drifting to side topics, which I’m glad about. But if you like, I can give a few more examples related to memory. I think they may be useful in practice.

    *

    Your description of how you build a program is interesting. I believe that 10 years of experience is more important than knowing all possible programming techniques. In addition - talent and passion. I would not overestimate theoretical knowledge. Let us imagine a musician who instantly senses a false note without having the sheet music. It seems to me that an experienced programmer is able to point to an error in a 1000-line piece of code at once, without any analysis and with accuracy close to 100%. The condition is that they know the code and understand its logic well. Why? For me - because he see a structure not a lines of code.

     

    As for VSC - I definitely work better in it than in ZBS. I encourage you to take that step. I believe you will gain a lot in terms of comfort. And in speed - if that matters. You lose nothing, you can install VSC alongside ZBS. As for configuration, it can be somewhat complicated because you are responsible for every parameter. But if something does not go well, ChatGPT will be helpful here. It knows much about VSC + OSX. I hope ChatGPT can do config with identical color scheme in VSC as in ZBS. It is well suited for such specific and finite tasks. And there really is no point in learning it - that would simply be cluttering the mind.

    • Like 1

    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.

    ×
    ×
    • Create New...