lovelace-card-mod: Does card-mod.js have a memory leak?
My Home Assistant browser has been running out of memory for months. After trying to fix this for half a year or longer, I found this topic: https://community.home-assistant.io/t/out-of-memory-error-in-browser/704262
I uninstalled card-mod, refreshed with Ctrl+F5 and the problem is finally fixed. I make lots of use of auto-entities (awesome, appreciate your work Thomas). But I actually had nothing using card-mod on any of my dashboards, it was merely installed as visible in the Developer Tools console.
I also had it installed as a frontend module:
frontend:
extra_module_url:
- /hacsfiles/lovelace-card-mod/card-mod.js
In configuration.yaml
It was not a browser extension causing it either, as I tested first in Incognito (with only a couple still running) but then recently in Chrome’s “Guest” mode with 0 extensions running, and the problem persisted. Now it finally seems solved with card-mod removed. No slowdowns. I’ve made no other changes.
Could still be a coincidence/something-else, or a HA bug, or a Chrome bug, or who knows what!
(I had v3.4.3.)
Oh and the major symptom of this issue before it eventually runs out of memory and crashes the browser tab, is things become extremely slow on the Dashboard. (Which might match other bug reports.)
I have my HA tab open 100% of the time on a dedicated monitor, always visible (never a “background tab”), so that might make me experience this issue sooner than others… though this doesn’t seem like an uncommon setup? It takes about a day (or two) to become noticeably slow to respond, and crashes after a week-ish if I don’t refresh.
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 5
- Comments: 27 (5 by maintainers)
Likely due to same issue as found in Browser Mod. Same helper code
hass_base_el()which usespromise.race(). Underlying issue well explained HERE.For card-mod, the feature where this code call is used repeatedly is binding to css templates. Also for initial theme load but only used one off there so not a leak. So if you don’t use templates in card-mod styles then you are unlikely to encouter the issue. If you use templates in card-mod styles then memory will be consumed over time.
I will submit a PR for card-mod once the Browser Mod commit has been reveiwed.
I have reveiwed this issue and descriptions from various users. In each case I can see use of templates or the use of Material theme prior to V4 which used card-mod and templates unless using no-mod version. So I am quite confident this will be the main source of memory leak issue.
All usages of the buggy function have been removed in 3.4.5. That should fix some things.
Sidenote: in the early days of card-mod I spent DAYS to add workaround after workaround specifically to make things work with
vertical-stack-in-card. Then I finally decided to just not support it. Things have probably changed in the last few years, but I recommend usingstack-in-cardinstead.confirm memory leak since 3.4.3 (included). 250mb memory usage under 3.4.2. more than 1.5gb memory usage from 3.4.3 up to 3.5.0… then chrome kill tab with oom error.
Duly noted and moved to a file so it does not affect browsing, apologies as I wasn’t aware it was possible to do so.
The rest still stands, I tested it and when reducing the size of the dashboard the issue did not manifest as clearly as with the full code. I tried to do some basic benchmarking and to contribute the request for a minimal code example which does not use Mushroom theming, since nobody had done so until then. Thanks to @thomasloven for noting that
vertical-stack-in-cardis not supported, was not aware of this and will try to remove it to see if it solves it even in older versions.That being said, I think the tone in your post @Mariusthvdb is not helpful and there are better ways of getting your point across. I’m honestly just trying to help.
I noticed that my home assistant dashboard was regularly crashing. This would result in the dashboard reloading on my wall tablets, once every 30 or so minutes. I attached an android debugger and saw that the webview process was being killed. I reproduced the same issue in wallpanel.xyz, google chrome (android) and google chrome (linux-x64). I dove into the browser memory debugger (which for reference, I have no experience with, so I could be reading it totally wrong). It seems like card mod is keeping copies of all card changes in RAM. I have two cameras on my dashboard, and it appeared like it was storing every frame from the cameras in RAM (lots of /api/camera_proxy/…). It would consume more and more RAM, until 4GB, then crash the tab. When it did, I’d get this on stdout for google chrome:
After removing card-mod, my memory usage has remained stable at ~150MB for a few hours so far. I have attached a screenshot from the memory debugger, I was leaking strings at around 1MB/sec constantly, and they mostly seemed to have card-mod in the object as shown. I haven’t dug any further, but hopefully this is helpful 😃
dashboard.txt
So after doing some testing, the dashboard included in the file is the smallest dashboard which I’m getting this issue with later versions. This is basically 3 sections with different column layouts, 2 of which are copied as they are for the room selectors which are only shown for different users (convoluted I know, but it does work well).
The test I’ve done is to set up part of my dashboard, use Firefox profiling to capture the memory usage and to go into edit mode, as that really seems to spike memory usage.
If I remove the last section, the memory still balloons but it is still controllable, but with all the sections, it just keeps climbing (image below had a max of ~1.2GB of usage, but I’ve seen it go to over 4GB)
With version 3.3.1, I still get some climbs (max ~500MB), but it appears to clear itself much better (these spikes I think are from when I click the edit button, which takes a while to show up).
The cards which I am applying card-mod to are custom:vertical-stack-in-card, markdown, clock and heading cards. I am using mushroom and other cards but they are not modified by card-mod.
Let me know if there is some additional info you want/need or some other tests, I am not a frontend dev so not sure what could be useful here
There is anecdotal evidence around (reddit etc) that just loading card mod causes this memory leak issue. I run HA in edge and chrome side by side and I see both the CPU churning at 10%+ for the tabs and the memory continuously climbing on both browsers. Am happy to play around and dig out data if someone can point me to what would be useful to help resolve this.
Same here. Tab with HA became unresponsive after a while. Removed card-mod and everything is working smooth again.
For what it’s worth, my example above only uses browser-mod for the following:
These are all completely static, ie. there are no template variables inside any of them, and was getting the issue. I do use templates in eg. mushroom cards, but these are not used by card-mod.
Now I have done the following:
vertical-stack-in-cardwithstack-in-cardThe good news - the situation is definitely better and no longer balloons endlessly, so thank a lot for the effort and advice since it is much improved.
The bad is that with card-mod installed, it still consumes more than I would expect for the few changes I’m making, especially in edit mode:
When I enter edit mode in my dashboard, the comparison between with/without card-mod is 3x memory usage when card-mod is installed (also added as a frontend module via the configuration.yaml), and a noticeable sluggishness in every action taken in edit mode.
With card-mod:
Without card-mod:
Outside of edit mode, the max memory is about the same, but with card-mod it definitely has a harder time reducing memory usage. The test below was done by clicking through my various area cards in the same order each time, without card-mod it tops off at ~140MB but falls to under 20MB after some time, with it installed it oscillates but it still sometimes increases to around 180MB after the same time.
With card-mod:
Without card-mod:
It’s definitely possible all of this is due to some interactions with other HACS addons or something else, if anybody has ideas on how to debug what could be happening (or if this is just expected) I can give it a try.
Definitely no themes (not my thing).
Yes, Jinja templates inside style section of card-mod, which may also be in any theme you are using.
I am pretty sure there is a leak with 3.4.3 as well. I recently swapped a wall tablet out from a fire tablet to a leveno and since this is a more powerfull device I decided to use lovelace (vs homehabit that i had on the fire tablet). Anyway after an hour or two the tablet would be back on the home screen (i tried with home assistant app and kiosk). I also always leave my dashboard open on my laptop in a chrome tab and after a few days that also crashed (oh snap). Anyway I just thought it was my crappy device until i saw this thread. So i downgraded to 3.4.0 and bamn, the home assistant app has been running for days without a single crash.
edit i dont have mushroom cards installed. I checked the memory usage in chrome dev but could’nt really see where it was having an issue but on my laptop the memory for the one HA tab was 210mb (fluctuating but always came back to around that) on 3.4.0 but on 3.4.3 after 10 mins it was at 750mb. Not sure what the max mem per tab is in chrome but once it hit it i got the Oh snap on every tab…
I also believe that card-mod is causing a problem here.
I notice a lot of memory consumption here too:
As well as in the browser console after some time (when tab is open) the following error messages:
Uncaught (in promise) {code: 'not_found', message: 'Subscription not found.'} card-mod.js:1 Uncaught (in promise) y @ card-mod.js:1 s @ card-mod.js:1After a certain time, the dashboard becomes slow and some functions, such as closing the bubble card pop-up, no longer work.