Author Topic: Odd Memory Behaviour  (Read 121 times)

Nick_W

  • Full Member
  • ***
  • Posts: 215
    • View Profile
Odd Memory Behaviour
« on: August 17, 2016, 12:28:00 pm »
Hi,

I have observed some strange behavior with memory.

Now I realize that memory is managed by the GarbageCollector, which runs when memory is low. Not sure what is defined as "low", but if I have a small program, really doing nothing but publishing uptime and battery/memory levels, then memory gradually drops (2-300 bytes at a time) until it hits less than 1k or so, then there is a pause, and the the program continues with 15k free again (Garbage Collection activity I assume).

If I add in a new object (Temperature sensor) every event cycle - as a local variable, memory drops by 500 bytes or so every cycle, until it gets "low", then the module resets.

the output looks like this:

Code: [Select]
MCThings/000111BA/Count 23
MCThings/000111BA/Temperature 26.500000
MCThings/000111BA/Uptime 190
MCThings/000111BA/BatteryVoltage 2823
MCThings/000111BA/FreeMemory 1676
MCThings/000111BA/Count 25
MCThings/000111BA/Temperature 26.500000
MCThings/000111BA/Uptime 201
MCThings/000111BA/BatteryVoltage 2823
MCThings/000111BA/FreeMemory 976
MCThings/000111BA/Count 26
MCThings/000111BC/Uptime 12663
MCThings/000111BA/Temperature 26.500000
MCThings/000111BA/Uptime 211
MCThings/000111BA/BatteryVoltage 2823
MCThings/000111BA/FreeMemory 280
MCThings/000111BA/Count 27
MCThings/000111BA/Status Booted
MCThings/000111BA/Temperature 26.500000
MCThings/000111BA/Uptime 0
MCThings/000111BA/BatteryVoltage 2612
MCThings/000111BA/FreeMemory 15072
MCThings/000111BA/Count 6
MCThings/000111BA/Temperature 26.562500
MCThings/000111BA/Uptime 10
MCThings/000111BA/BatteryVoltage 2601
MCThings/000111BA/FreeMemory 14360
MCThings/000111BA/Count 7


Now this memory management system is weird (I'm used to local variables being released when out of scope), but it seems that the module is running out of memory before Garbage collection can free up some RAM - hence the reset.

There is no way to "Delete" or "free" an object, but I can manually run Garbage Collection.

Is this normal behavior? Do I need to manually run GarbageCollection when memory is low? this isn't the "automatic" memory management I was expecting...

Share on Facebook Share on Twitter


mc-John

  • Global Moderator
  • Full Member
  • *****
  • Posts: 212
    • View Profile
Re: Odd Memory Behaviour
« Reply #1 on: August 17, 2016, 01:52:00 pm »
Quote
Now this memory management system is weird (I'm used to local variables being released when out of scope), but it seems that the module is running out of memory before Garbage collection can free up some RAM - hence the reset.
If the system need to allocate memory it tests if there is enough memory available. If not it garbage collect (GC) and if there is no memory available after the GC it reset.

Quote
There is no way to "Delete" or "free" an object, but I can manually run Garbage Collection.
Manual GC is not required.

This could be a memory leak or a bug in the GC. Don't forget that at the end of an event all memory gets out of scope with the exception of shared variables.

Could you send us the code?
« Last Edit: August 17, 2016, 01:54:59 pm by mc-John »

Nick_W

  • Full Member
  • ***
  • Posts: 215
    • View Profile
Re: Odd Memory Behaviour
« Reply #2 on: August 17, 2016, 02:50:13 pm »
Just had to reboot my computer, made a test program (ie removed my extraneous stuff), and - doesn't do it anymore.

Memory counts down to about 200 bytes, then pause - starts off at 15k again, but no reset.

Can't explain it. I am still trying to get an I2C object to persist between events (with no luck), so maybe something I did there was causing the resets.

I'll let you know if I figure out what is going on.