0

By default, if i include the WiFi library, the object WiFi will be registered globally. How can i disable this behavior without changing the library file itself? (removing the definition "extern ...") Or is there a way to delete the objects right after the inclusion? I just want to create the object manually when needed to save memory.

2
  • 1
    it shouldn't be in build result, if it is not used
    – Juraj
    Nov 3, 2019 at 20:16
  • you can check the .map file for code excluded and code included in the final build
    – Juraj
    Nov 6, 2019 at 13:10

1 Answer 1

0

I am not sure, if it would save enought memory to be worth the problems (it is just few bytes, buffers are created anyway), but lets say so. Then you need modify the library to not creating the object, as it is allocated statically and cannot be removed later (and reclaim its memory). To protect all other projects it would be probably best to use special #define to disable it only in memory critical projects - so the Wire.h should contain something like

#ifndef EXCLUDE_WIRE
extern TwoWire Wire;
#endif

and your program (critical.ino) should start with

#define EXCLUDE_WIRE
#include <Wire.h>

and then somewhere in loop / other function use

TwoWire Wire;

as local variable. If you need to call other functions with it, then you have to send its address as a parameter.

Anyway you will not save nearly any memory, as nearly all variables in TwoWire ((and namely buffers) are static and so class variables and so are common for all instacies of TwoWire and allocated automatically if there is (possibly used) any instance of TwoWire anywhere in your program.

So it would be better to create your own library to use the same functionality, but make sure, that all such variables are instance variables and properly created and released at the right time. This would also allowe for changing the buffer sizes, if needed.

As it is under LGPL 2.0+ you are legally allowed to do that (provided you fullfill all licence conditions).

4
  • Thanks.I already thought i've to go that way. Even if this makes much more work, i can also strip down everything i don't need. My sketch space is is also running on limit ;)
    – elebb
    Nov 4, 2019 at 9:18
  • Note that if you don't use the global Wire variable, it will be stripped away by the linker and will not use any memory. There is strictly no point in modifying the library source to remove it. Nov 6, 2019 at 8:51
  • @elebb: You may be able to save some memory by stripping unused functionality from the library methods that you do use, and by removing unused data members. You will not, however, save anything by removing unused methods or global variables: the linker already does that for you. Essentially, the Arduino IDE is already applying this optimization technique. Nov 6, 2019 at 9:28
  • If I understood it correctly, that the intended use was to have NOT one global Wire object all the time, but create it in case you need to use it and destroy it afterwards to save RAM, while other part of program will in other time use that same RAM for something else and then free it. Just the same way, as the same byte in RAM is used as part of local integer variable in one function and in other time as say local char variable of totally unrelated function, in other time it could be part of return address of third function, while in mean time it is just part of free RAM for stack.
    – gilhad
    Nov 6, 2019 at 14:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.