开发者

Can I find the library that created a named OSMallocTag?

开发者 https://www.devze.com 2023-01-31 05:23 出处:网络
I use the allocations instrument to measure heap usage in my iOS app. I find that a significant amount of memory is allocated in a region with the tag name \"Memory Tag 70\", and I would like to know

I use the allocations instrument to measure heap usage in my iOS app. I find that a significant amount of memory is allocated in a region with the tag name "Memory Tag 70", and I would like to know who is responsible for that so I can chase down:

  • whether it's appropriate for me to try and do anything about this memory
  • if so, what I should do about it (i.e. what code is allocating objects in that region).开发者_如何学Python

So, can I trace where a call to OSMalloc_tagAlloc() with particular arguments is coming from? I'm willing to accept that I may only be able to do so when running in the simulator, not on the device. But even if that is the case, how would I go about it? Can I get dtrace to show me the tag names, if so can I launch my app in the simulator via dtrace -c? How?


In instruments you can switch between various displays by selecting the popup that sits in the divider between the top and bottom section of the window. For "Allocations" you can switch it to show "Call Trees" (I think it defaults to "Statistics").

This should give you call stacks for all allocations. I'm however not sure if it will trace all the way from kernel to user space.


For me, this tag was related to large UIImages I had allocated. I did some investigation into how you could get more information about this and have some (perhaps) useful things to suggest.

I believe that the tags you're interested in are the ones passed via the flags argument of vm_allocate and similar, not OSMalloc_tagAlloc(). The iOS 3.1 release notes mention the <mach/vm_statistics.h> and <mach/vm_map.h> header in connection with the VM instrument.

I think that the tag is passed via the vm_allocate flags parameter as per vm_statistics.h's VM_FLAGS_ALIAS_MASK and following #defines. (They're called "aliases" here.) This means you should be able to make a dtrace script that probes for, say, vm_allocate, and extracts the tag from the flags parameter. For example:

sudo dtrace -n 'fbt:mach_kernel:vm_allocate:entry /pid==12345/ { printf("%d", (arg3 & 0xFF000000) >> 24); }'

You can use Instruments to make a dtrace instrument and run against the iOS simulator via "Build New Instrument..." in the "Instrument" menu, or you can use use a command-line dtrace script and include a /pid == 123456/ predicate for your running app.

Unfortunately, I was unsuccessful in finding the correct probe to find these allocations. When inspecting the appropriate argN variable, the flags always seems to have 0 in the tag/alia portion. I tried, for example, fbt:mach_kernel:vm_allocate as above, fbt:mach_kernel:mach_vm_allocate, fbt:mach_kernel:vm_map_enter, etc. Perhaps these allocations are going through some other avenue? I don't know much about the kernel's memory allocation system.

So, I'm not exactly sure where these tags are being passed to the kernel, but I hope this helps you track it down.

0

精彩评论

暂无评论...
验证码 换一张
取 消