1

I am developing an app that is crashing due to an excess of memory usage. I would like to know the amount of memory that is being used by each active thread so that I can decide which allocated or drawn in screen elements release or remove from view. Is there a way to obtain it?

I have tried using mach.h library but with mach_task_self() I can only access the memory used by the whole application.

Thanks in advance

Miguel Isla
  • 1,379
  • 14
  • 25
  • Do you get any memory warning? My suggest is that you use instruments to see how much memory you allocate, and detect possible leaks, then see if you're able to write the code in a way that allocates less memory. – Ramy Al Zuhouri Jan 11 '13 at 12:53
  • 2
    Threads don't *own* memory, Processes do... – trojanfoe Jan 11 '13 at 13:00
  • Yes, I am receiving memory warnings before the application crashes. We already have used Instruments and there are no leaks. We are working hard to rewrite the code in a more convenient way. Thank you for your answer. – Miguel Isla Jan 11 '13 at 13:02
  • You should start freeing the memory when you receive a memory warning. – Ramy Al Zuhouri Jan 11 '13 at 14:02

2 Answers2

3

I think what you want is logMemUsage().

You can check the Answer from this Question : Watching memory usage in iOS

I think you can get something from this Documentation also : Understanding and Analyzing iOS Application Crash Reports

If you want to check Memory Usage While Application is Running then use Instruments. :

Using Instruments you can check how much memory your app is using. In Xcode4, use 'Profile' build, choose Leaks, then click the Library button in the toolbar and add the Memory Monitor instrument.

If you really don't want to use Instruments then you can use Custom Class UIDeviceAdditions : Get current Memory usage

Hope it's Enough.

Community
  • 1
  • 1
Bhavin
  • 27,155
  • 11
  • 55
  • 94
  • Thank you for answering. I will have a look at it and answer again if it is useful or nor. Instruments only gives the total memory used by the application but not how it is distributed between the different active threads (Or at least I do not know how to do it) – Miguel Isla Jan 11 '13 at 13:06
  • Yes, I have gone through the first link and there is used the same `mach_task_self()`that I use. The memory I get this way is the total memory used by the app but I do not think it can be used for what I am looking for. Anyway, thanks for answering and for the links. – Miguel Isla Jan 11 '13 at 13:44
  • 1
    If you use logMemUsage before and after your thread , and if you subtract afterMemoryUsage from beforeMemoryUsage then i think you can get the memory consumed in that particular thread. – Bhavin Jan 11 '13 at 13:48
  • 1
    I do not think it would work either. Imagine you have one thread running and you start another one. You know the memory that is being used before and after you start but if both threads keep using more and more memory and you ask again for the memory used, you do not know to which thread associate the memory that has recently been created. I do not know if I have explained myself or not... – Miguel Isla Jan 11 '13 at 14:04
1

You can't because threads share the heap. Threads are created with a 512KB stack space, with memory pages allocated as needed. Other than that, there is no memory per thread value stored anywhere.

Jano
  • 62,815
  • 21
  • 164
  • 192