-
-
Notifications
You must be signed in to change notification settings - Fork 3
misc: Optimize logging statements memory usage. #73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
e799e2e
to
3e5dc60
Compare
Thanks for reducing the dynamic memory usage of logging. I think it is not necessary to guard for static strings like because static strings are in the read only memory segment anyway. It only is necessary for f-strings which are concatenated dynamically based on parts like |
I'm not entirely sure but I think that's only true if the module is frozen when running on MicroPython, but it's not normally frozen, it's loaded in memory and parsed, compiled etc.. but either way most of the strings are fstrings, at least the ones that get printed at every step, so the overhead is minimal. Note in some cases a check is going to be done anyway, for example multiple debug statements, here an entire loop is skipped: if log_enabled(logging.DEBUG):
logging.debug("Pushing records to Arduino IoT cloud:")
for record in self.senmlpack._data:
logging.debug(f" ==> record: {record.name} value: {str(record.value)[:48]}...") |
You are right - I was thinking of C/C++ Arduino code and string handling. I still need to remember myself that I am now using python. |
For non-fstrings it's probably passed by a reference (pointer) to the string, it's maybe worth asking about, but since those are just a few lines that get executed once per connection, I will remove the check for them. |
@Bodobolero They do seem to consume memory: import gc
import logging
logging.basicConfig(
datefmt="%H:%M:%S",
format="%(asctime)s.%(msecs)03d %(message)s",
level=logging.INFO,
)
gc.collect()
logging.info("RTC time set from NTP.")
logging.info("RTC time set from NTP.")
logging.info("RTC time set from NTP.")
logging.info("RTC time set from NTP.")
mem = gc.mem_free()
gc.collect()
print(gc.mem_free() - mem) Prints 1840 and it grows if you add more statements. |
No actually they don't if log level is say |
fcfd0ff
to
a4afb86
Compare
a4afb86
to
ca0ec69
Compare
Updated an merged. |
No description provided.