Get available memory
I am looking for a simple way to check a memory leak, and therefore would like to check the available memory regularly.
Is there a function to check available memory?
lukaskollmer posted a link to code to get memory statistics in this topic:
JonB has another link to code to get memory memory statistics in the same topic. (Although, the current ending comment JonB put there is, "Looks like this code does not work properly on 64 bit... i suspect the structure is not sized correctly." - but if the first code doesn't help, you might want to use JonB's code as a starting point.
I am not sure any of that will help you detect a leak though.
Python uses reference-counted objects and garbage collection. There might be a way to force garbage collection, but even if "del" as used to delete an object, and there was some special way to force immediate garbage collection, while the memory will be freed so that Pythonista can reallocate it for another purpose, the memory won't necessarily be freed to the operating system.
You could use "multiprocessing" and do memory intensive work in a subprocess, and when that process closes, the memory will be returned to the operating system.
Perhaps someone more familiar with Pythonista's internals can give a better answer. My guess is that what I wrote is correct.
gc.collect()can be used to manually run the garbage collector. (I do that sometimes when I'm working on a script and want to get rid of all objects from the last run. My iPad is a bit old and sometimes has memory issues in Pythonista.)
multiprocessingis not an option in Pythonista, because iOS doesn't allow apps to spawn additional processes.
@upwart , I remember I had a conversation about memory leaks before on the forum. Here is the link to the forum question I had and the discussion. It was 2 years ago, I am sure its still valid today. If I was worried about a memory leak today in a function or a class, I would try searching for a decorator that might be able to be helpful. I am not sure this could be done in a decorator, but the possibility that it could be occurs to me. In the old days in c we did a lot of stress testing to search for memory leaks. I.e running tests for a day or so, in those days with little memory it quickly become apparent if you had a memory leak. Aka crash.
Edit: if you have a lot of memory. You could possibly create a big dummy object in memory to simulate less memory. Sorry only problem is I have no idea how iOS handles memory. I.e can it swap it out to disk etc...