Memory usage in makewebrequest

I have a widget which is running out of memory on makewebrequest calls, the widget gets its data in a dictionary from a JSON via makewebrequest. On previous versions I was getting an out of memory response but worked around this by splitting it into 2 separate smaller requests but now it seems to now fall on the second one now as out of memory (-403) every time.

The first request gets a file of about 5k in size, from the simulator by the time it's converted into a dictionary and passed to my onReceive method the memory usage has risen by about 20k.  Once it's parsed it's easy through the data it makes the second request but this one fails. I can't seem to get it to free up the memory from the first response before making the second request. I've tried setting the dictionary to null but I assume it's still referenced somewhere as the garbage collector doesn't seem to free it up, and I can't work out a way to wait for the memory to be released before sending the second request in a clean way

How much memory should these calls and dictionaries be taking up? Is there a way I'm missing to let the garbage collector know I'm done with the data object and to release the allocated memory so I can go ahead with the next request?

Cheers.

  • There is no garbage collector. All non-primitive objects (Boolean, Char, Number, and Float are primitive) are reference counted. When the last reference to the object goes out of scope or is set to null the object is cleaned up. If the memory associated with the response data is not being released back to the system, it is because there is a reference to the data preventing it from being deallocated.

    When you say that the first request gets a 5KB file, you have to remember that is a flat file. In a flat file, there is very little or no overhead (In a JSON file, an empty JSON Object is represented by curly braces, which is exactly 2 bytes, but if you load that into memory it is a Toybox.Lang.Dictionary which has overhead. Same for JSON Arrays which are represented as Toybox.Lang.Array. If the JSON payload has many small JSON Object/Array, the overhead will eclipse the actual data size.

    If at all possible, you will want to try to reduce any unnecessary data being received by your application. If the web service you're accessing provides a way to filter out some of the data in the response, you should consider using it if possible. If something like this isn't available, you may need to write a front-end service that allows you to do this.

    If you provide some code, I'd be happy to look at it to see if I can find why the response memory is not being released to the system. You can post the code here, or PM me a link and I'll take a peek when I have some time.

  • Maybe not the answer you'd like, but I have found that implementing your own proxy server gives you much more control and makes your CIQ apps more stable.

  • Thanks, that's what I assumed was happening, the system function which calls my onReceive method must still have a reference to the dictionary open so it is not released. So I can't make the second request from that callback function I need to somehow trigger the second request after onReceive returns and a little wait for the memory to be freed up. I'll have another look at tonight and see if I can work it out.

  • A proxy would solve the problem as the API returns a lot of information and most of it is not relevant to my app, unfortunately I think it would violate the terms of use of the API I'm using.