I Am working on making my code more memory efficient and getting rid of memory leaks and i go to fix dome logging of battery metrics and narrowed it down to vexes functions for the battery can some one fix that?
There is no memory leak as we do not allocate any memory for functions like that.
All Brain.Battery.Voltage() is doing is returning a number.
Can you help with why it puts 16 bytes to the heap in a loop then?
here’s the code:
def \__init_\_(self):
self.smartport=self.Smartport()
self.threewire=self.Threewire()
self.system=self.System()
\# set for variables id.
self.variables={}
self.valueid=0
self.battery_voltage=0
self.battery_current=0
self.battery_capacity=0
self.battery_watts=0
self.battery_voltage_monitoring=0
self.battery_current_monitoring=0
self.battery_capacity_monitoring=0
self.battery_watts_monitoring=0
self.ctrl_name={}
self.watts:int=0
self.axis={}
self.button_objs={}
self.button_names = \[
"A", "B", "X", "Y",
"UP", "DOWN", "LEFT", "RIGHT",
"L1", "L2", "R1", "R2",
\]
self.button_values = {}
def battery(self) -> None:
"""
Capture for the brains battery.
Args:
None
"""
self.battery_voltage=brain.battery.voltage(VOLT)
self.battery_current=brain.battery.current(CurrentUnits.AMP)
self.battery_capacity=brain.battery.capacity()
self.battery_watts=self.battery_current \* self.battery_voltage
\# Battery monitoring for voltage, capacity, and current.
if self.battery_voltage>=12:
if self.battery_voltage_monitoring==1 or self.battery_voltage_monitoring==2:
log.add("DB0", "%s"%(self.battery_voltage))
self.battery_voltage_monitoring=0
elif self.battery_voltage<12:
if self.battery_voltage_monitoring==0 or self.battery_voltage_monitoring==1:
log.add("WB0", "%s"%(self.battery_voltage))
self.battery_voltage_monitoring=2
elif self.battery_voltage<11:
if self.battery_voltage_monitoring==0 or self.battery_voltage_monitoring==2:
log.add("EB0", "%s"%(self.battery_voltage))
self.battery_voltage_monitoring=1
if self.battery_capacity>=50:
if self.battery_capacity_monitoring!=self.battery_capacity:
log.add("DB3", "%s"%(self.battery_capacity))
self.battery_capacity_monitoring=self.battery_capacity
elif self.battery_capacity<50:
if self.battery_capacity_monitoring!=self.battery_capacity:
log.add("WB1", "%s"%(self.battery_capacity))
self.battery_capacity_monitoring=self.battery_capacity
elif self.battery_capacity<25:
if self.battery_capacity_monitoring!=self.battery_capacity:
log.add("EB1", "%s"%(self.battery_capacity))
self.battery_capacity_monitoring=self.battery_capacity
if self.battery_current<=10:
if self.battery_current_monitoring==1 or self.battery_current_monitoring==2:
log.add("DB1", "%s"%(self.battery_current))
self.battery_current_monitoring=0
elif self.battery_current>10:
if self.battery_current_monitoring==0 or self.battery_current_monitoring==1:
log.add("WB2", "%s"%(self.battery_current))
self.battery_current_monitoring=2
elif self.battery_current>15:
if self.battery_current_monitoring==0 or self.battery_current_monitoring==2:
log.add("EB2", "%s"%(self.battery_current))
self.battery_current_monitoring=1
if self.battery_watts<=150:
if self.battery_watts_monitoring==1 or self.battery_watts_monitoring==2:
log.add("DB2", "%s"%(self.battery_watts))
self.battery_watts_monitoring=0
elif self.battery_watts>150:
if self.battery_watts_monitoring==0 or self.battery_watts_monitoring==1:
log.add("WB3", "%s"%(self.battery_watts))
self.battery_watts_monitoring=2
elif self.battery_watts>200:
if self.battery_watts_monitoring==0 or self.battery_watts_monitoring==3:
log.add("EB3", "%s"%(self.battery_watts))
self.battery_watts_monitoring=1
ok, didn’t realize it was Python, assumed you were using C++.
Many Python functions will create a new object that gets returned, in the case of battery voltage it will be a floating point object. Once that object is done with, for example the variable it was assigned to is overwritten, that object will be marked as ready for garbage collection. GC will happen some time later, usually when memory for allocation gets low, not something you should worry about.
I know now to worry much about it because of the GC but would just calling the function rather than putting them in a variable fix it?(The reason for this is because i want/need the logging portion of the code to be as light weight as posible)
nope, the function, as will many others, allocates a float object to return.
If you are worrying about performance at this level, switch to using C++.
I’ve been wanting to for a while but the rest of my team can’t do C++