vm keeps increasing when in a rigorous loop

  • Hi,

    The code is like:

    - (void)myMethod
    {
    .........

    while (flag) {
      UInt32 temp;
      ..... and lot of other ints etc.
      if (someCondition == 1) {
      UInt8 *buff = (UInt8*)calloc(1, sizeof(aStruct));
      if (someOtherCondition == 1) {
        [self addPointerToATree:buff];
      }
      else
        free(buff);
      }
    }
    }

    The while loop runs very rigorously without having much delay in it.
    Memory for buff is only needed when certain condition is met.
    I have seen that even when this condition is not met and no memory is allocated, the VM keeps growing steadily as seen in Activity Monitor.
    So the only thing contributing to it would be the various ints etc.
    But the app is GC enabled (required), so the VM size should reset to some level every few seconds, but doesn't.
    The loop does many thousands of iterations per second.

    Any idea on how to limit this usage?
    Shall  help.
    I have a auto release pool drained at the end of every iteration in a loop in some other method but that doesn't seem to have much effect.

    Wishes,
    Nick
  • On 8 Jul 2012, at 1:06 PM, Nick Rogers wrote:

    > But the app is GC enabled (required), so the VM size should reset to some level every few seconds, but doesn't.

    Revise your mental model of garbage collection. It runs periodically on a thread and doesn't collect all garbage at every sweep, so you have no way of knowing when it will catch your allocations. So far as you are concerned, it is nondeterministic. And if that -addPointerToATree: method goes into Cocoa at all, you should assume Cocoa is allocating and rooting memory in ways you don't have control over.

    Also, chasing VM size in Activity Monitor is a fool's game, as futile as watching retain counts. The VM size simply counts the total span of addresses that your code has _touched_, not how much RAM is active at the moment. The closest thing you should be looking at is private memory, and even that isn't 100% reliable. Use Instruments to determine whether you have an actual leak.

    — F
  • On Jul 8, 2012, at 11:06 AM, Nick Rogers wrote:

    > I have seen that even when this condition is not met and no memory is allocated, the VM keeps growing steadily as seen in Activity Monitor.
    > So the only thing contributing to it would be the various ints etc.

    Use the 'heap' tool, or Instruments, to see what's actually being allocated in the heap. Don't just guess.

    —Jens
previous month july 2012 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today