Malloc error, can someone explain why this is happening?

Discussion in 'Mac Programming' started by ausername, May 5, 2009.

  1. ausername macrumors newbie

    Joined:
    Feb 28, 2009
    #1
    I made an application that uses a lot of loops, and deals with MB's of text at a time. After the program has been running for about 10 seconds, it goes to "not responding." After about 2 minutes, I get this error:

    aTestApp(1550) malloc: *** mmap(size=2097152) failed (error code=12)
    *** error: can't allocate region
    *** set a breakpoint in malloc_error_break to debug
    aTestApp(1550) malloc: *** mmap(size=2097152) failed (error code=12)
    *** error: can't allocate region
    *** set a breakpoint in malloc_error_break to debug
    aTestApp(1550) malloc: *** mmap(size=2097152) failed (error code=12)
    *** error: can't allocate region
    *** set a breakpoint in malloc_error_break to debug

    The application has garbage collection on... I am also manually releasing everything myself. Is there an easy way around this error? Or what?

    Any help or info would be great, thanks. :)

    I am using Mac OS X 10.5.6,
    Xcode 3.1.2,
    The application is being compiled in 32 bit universal, I tried 32/64 universal.
     
  2. kpua macrumors 6502

    Joined:
    Jul 25, 2006
    #2
    It sounds like you are simply running out of memory so malloc is failing. Try running your app in Instruments to observe its allocation behaviors. You may not be freeing memory early enough or at all.
     
  3. ausername thread starter macrumors newbie

    Joined:
    Feb 28, 2009
    #3
    I tried using instruments

    The allocation is spiked at the top, I am reference counting every object, and like I said before, garbage collection is on. Also, I don't understand why it goes to "Not responding," when there is still 2GB of memory left.

    The program reads in about 1MB of text from a file, does some things with it, and releases it, and then repeats. The memory should not fill up like this, and even if I was not releasing anything, everything is automatically an auto-release object.
     
  4. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    If you have GC enabled, those autorelease and release methods don't do anything.

    You could try calling [[NSGarbageCollector defaultCollector] collectIfNeeded] (or collectExhaustively)
     
  5. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    I am very confused. You are malloc'ing, but then saying things are autoreleased. I don't really think it goes both ways. If you allocate some memory using malloc, the only way you're getting it back is free. For objects that are autoreleased, they won't go away until a pool gets drained/released. Maybe you should set up autorelease pools around areas of the code where a lot of objects are being created so they can be released sooner?

    Showing us code would definitely make this easier.

    -Lee

    EDIT: Not as familiar with GC, so look into kainjow's suggestion. The autorelease pool stuff sounds like it doesn't apply.
     
  6. kpua macrumors 6502

    Joined:
    Jul 25, 2006
    #6
    Sorry, I thought this was malloc failing, but it's actually mmap.

    How are you reading in your files? If you're mapping in all your files, you might not be properly unmapping them, so you eventually fill up the address space.

    If you're using mmap directly, then the fact that the app is GC is probably irrelevant. However, if you're reading them in with NSData's memory mapping option, or something like that, then kainjow's suggestion might help, if the NSDatas (or whatever) aren't being finalized to unmap the files. (Although, isn't the collector be running in another thread? Unless the loop is really tight and files are being mapped in very rapidly, the collector should still be able to collect the garbage NSDatas.)
     
  7. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #7
    You should probably show some code. It looks like you're leaking something.
     
  8. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #8
    Garbage collection doesn't help if you don't do it right. Garbage collection will not throw anything that can still be reached. Pointers to objects that are not used anymore need to be set to NULL, otherwise they can't be collected.
     
  9. adeslat macrumors newbie

    Joined:
    Jun 4, 2009
    #9
    truly an error

    I am running R on my new 8 GB Mac Book Pro and also getting this error -- I suppose it is an R problem the allocation of greater memory -->

    Best,
    Anne

    Details: Running on publicly available GEO data set GSE2118 --> and I get this error
    Error: cannot allocate vector of size 594.9 Mb
    R(2046,0xa07ee720) malloc: *** mmap(size=623751168) failed (error code=12)
    *** error: can't allocate region
    *** set a breakpoint in malloc_error_break to debug
    R(2046,0xa07ee720) malloc: *** mmap(size=623751168) failed (error code=12)
    *** error: can't allocate region
    *** set a breakpoint in malloc_error_break to debug
    R(2046,0xa07ee720) malloc: *** mmap(size=623751168) failed (error code=12)
    *** error: can't allocate region
    *** set a breakpoint in malloc_error_break to debug
    R(2046,0xa07ee720) malloc: *** mmap(size=623751168) failed (error code=12)
    *** error: can't allocate region
    *** set a breakpoint in malloc_error_break to debug

    It has a dim of about 12,000 rows by 12 columns... and its trying to allocate memory that exceeds what it can do... but I think it is a quirky R problem
     
  10. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #10
    Is that 2.5GB of memory, or am I reading it wrong.

    6237511688*4 bytes ~= 2.5GB. ??
     
  11. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #11
    I think the number reported by mmap is already in bytes. Half a gig is quite a chunk, but on an 8GB Pro (a 64bit machine) this should really not be a problem... Perhaps your VM got badly fragmented, and/or perhaps R is 32 bit only..?
     
  12. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle

Share This Page