Forum Archive

[Share code] Get available memory

lukaskollmer

Recently, the question came up how to get memory statistics.
I found an Objective-C example on StackOverflow and rewrote it using ctypes.

My gist: https://gist.github.com/lukaskollmer/a09c0278d2d224b9f4839a895ebb9988

Notes:
- There are 2 different total memory counts: "total" and "total (according to Cocoa)"
- Because the graphics card and the CPU share the same memory, the GPU us actually using a part of the RAM for it's graphics memory
- That's why the first total count is smaller. It's only the amount of memory available to the CPU, while the second total count contains both

Example:

chibill

Very cool this can be expanded to also show the processor count. With only one new line of code.

lukaskollmer

@chibill I know. You can use

NSProcessInfo.processInfo().activeProcessorCount()

Phuket2

Yes it's a balancing act. When do you stop. Is my biggest problem just in Python.

brumm

Great, thanks a lot. Btw. whats your max. free memory? I got values from 50MB to 300MB. After rebooting my iPad Air 1 e.g. I got 138MB...

lukaskollmer

@brumm I made some tests:

Normal usage
- free memory: 190MB
- used memory: 745MB

After cold reboot
- free memory: 170MB
- used memory: 950MB

After warm reboot
- free memory: 200MB
- used memory: 920MB

brumm

Okay, now I'm sure that I have too less memory 😞 (even if I get 522MB)

open shp+dbf ('free: ', <__NSCFString: 522,1 MB>)
shp+dbf readed ('free: ', )
then crash while writing it in a sqlite db

Trying to convert 80MB of data... with pyshp.

Phuket2

Guys, I have two comments. They are not directed at this post, more to share posts generally.

  1. I think a nice idea if you share code, you put at least your forum user name in the top of the file. Often I copy share code here , then some weeks later use it. Like to credit the person that shared it. Yes, I can go and copy the name in myself, but I sometimes forget too.

  2. Rather than writing results to the console, wouldn't it be better to show them using the dialogs module. It's easy, we can all do it. For a few extra minutes, gives it a lot more professional look.

Look, only suggestions. It's great people share code. Not trying to burden them with extra work. But it's amazing how much things are valued with a little bit of polish.

I haven't followed this way before. But I will try too. Also I will attempt to make it Pass pep8 and pyflakes

Phuket2

Guys, I am not trying to be smart or rude. But there is something to be said about the old Macintosh toolbox routines. The discipline they had back then really shaped the modern desktop interfaces.
I see articles about Python is just a scripting language, then I see other articles why python does not need to be thought of as a scripting language.

I was able to make robust commercial products with languages far inferior than Python 20 years ago. You could even say in my day given the processing power of the CPUs, back then c was inferior to today's Python. I know a lot of Python Libs these days are complied c code. But some how it's not that Important.

Yes, there were mission critical parts of programs. In the old days we would write c code and link it like a DDL , but macs had the same idea also. Of course if you really got stuck for performance, then you would resort to assembly language. Not fun. You also were checking for the existence of other chips, eg floating point processors. They were not built into the cpu back then.

My point is that history is not that bad and that Python is as powerful and structured as you make it. Well in my view.

If you took a stroll down memory lane, and looked at the old Macintosh API.
AboutBox
DialogBox
FontDialog
Native support for regions, polygons....
Resource Manager
TextEditor
Really, it just goes on and on

Well a little drunk now... But it goes on and on. I don't understand why I don't see the same discipline in Python. My honest guess is that most don't see it as real language. "Is only scripting". Well, sorry to say that's crap in my view. You can script which is totally valid or in my opinion you can write rich applications.

There are guys on the forum that write very serious applications for Pythonista. And they are great. I hope in the future I will be able to also. I just have to live long enough 😱 But I think the these guys are not phased about the language, they just use the best parts of the language and do a nike, aka 'just do it'

Sorry, it's been a rant. I am passionate. And from my heart want to see all you guys succeed.

i know I will wake up in the morning hating myself for posting this. But shit happens. I tend to embarrass my self multiple times a day.
i really should have hit the discard button 🙄

**

Phuket2

Thanks to all for not causing me further embarrassment on my stupid rant. I have deleted now. Oh well.....

jmv38

This works great to get total memory. Anyone knows how to get memory used by pythonista only? Thanks.

JonB

https://github.com/jsbain/objc_hacks/blob/master/memstatus.py

the virtual_size field shows the amount of memory used by pythonista .

wolf71

@JonB The Result :
{'system_time': 1.0, 'resident_size': 3041012939227142L, 'policy': 0, 'virtual_size': 551690955866505216L, 'suspend_count': 0, 'user_time': 0.0}

  1. How calc the Pythonista using memory? (just like 320.23MBytes) the virtual_size very large.
  2. How to get Pythonista CPU loading and Total CPU loading ?
JonB

Looks like this code does not work properly on 64 bit... i suspect the structure is not sized correctly..