Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 620065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:43:23+00:00 2026-05-13T18:43:23+00:00

While looking for something else in header files and documentation, I recently discovered the

  • 0

While looking for something else in header files and documentation, I recently discovered the following function prototype in <objc/objc-auto.h>:

void *objc_memmove_collectable(void *dst, const void *src, size_t size);

It’s in the middle of a group of functions following a comment that says “Write barriers. Used by the compiler”. These functions are used to inform the garbage collector that memory it manages has been modified so it can scan for references and not accidentally reclaim memory that should be rooted by a strong reference it doesn’t know about. I know that such using __strong appropriately will nearly always cause the compiler to insert the correct function, but I’ve also heard Apple engineers say there are extremely rare cases when you must call such functions directly. (I believe it’s when a file is not compiled as Objective-C, such as C code that writes into GC-managed memory, but I’m not positive.)

To wit, Apple’s Objective-C Runtime Release Notes for Mac OS X v10.5 document states the following about this function: (last paragraph under the first heading)

“When copying memory in bulk into a garbage collected block you must use the API objc_memmove_collectable(void *dst, const void *src, size_t size).”

The function seems geared towards moving items from non-GC memory into GC memory, and email discussion archives seem to suggest that the purpose is to only trigger a single write barrier for a large block copy. (For example, 1000 individual writes with a write barrier for each, versus 1 bulk copy with a single write barrier for the entire memory region being written to.) This is one instance when it must be used, but the docs don’t say anything about when it shouldn’t (or needn’t) be used.

For example, I have a block of memory that I allocated with NSAllocateCollectable() and NSScannedOption, and I use it as a dynamically-expanding circular buffer. If the buffer becomes full, I double its size using NSReallocateCollectable() and NSScannedOption. The portion that wraps around (between the first slot in the array and the last object in the buffer) is copied/moved to the start of the second half of the array. I then bzero() the slots where the data was copied from to avoid over-rooting the moved objects. (See lines 460-467 in this file. Yes, the code works as is — it’s fully unit tested and I haven’t seen any crashes since I added the __strong attribute some time ago.)

Questions:

  • When is it necessary to use objc_memmove_collectable() instead of memmove() or memcpy()?
  • For example, what if the source and destination are both GC-managed memory? (My memory is declared as __strong id *array; so I’d guess that the compiler inserts the barrier.)
  • If it isn’t necessary, will using it help/hinder GC performance? (For example, does it hold any sort of lock, or help the GC avoid having to scan manually?) Is it considered good/poor style?

Edit: Since memcpy and memmove don’t interact with the GC at all, I was wondering why I haven’t seen any crashes from the memory being collected from underneath me. My best guess at this point is that since bzero doesn’t tell the GC anything either, the collector doesn’t find out about the zeroed memory and the moved data until the next time it scans that whole memory block. If the collector is still counting the now-zeroed references as roots, and doesn’t yet count the new memory locations, it would explain why the values don’t get prematurely collected. Does this sound right?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T18:43:24+00:00Added an answer on May 13, 2026 at 6:43 pm

    I’m sure @bbum will come in with a more explanatory and definitive answer, but here’s my understanding, which may not be 100% accurate.

    As the header comment states, whenever you write data in bulk (i.e., with memcpy or memmove, not with =) to a scanned, GC-allocated buffer, you should always use objc_memmove_collectable to create the write barrier. The source of the data doesn’t matter. memcpy doesn’t have the smarts to know if you copy from scanned, GC-allocated memory to other scanned, GC-allocated memory.

    I’m not sure if GC will function incorrectly without a write barrier, but it certainly would perform better. Write barriers are hints to the collector saying ‘Hey! I might have written a pointer here. Can you check it for me?’. Without those, the collector would be forced to do full scans all the time.

    Additionally, a single objc_memmove_collectable() is a whole lot more efficient than a bunch of implicitly write-barriered = assignments, even if you completely disregard the cost of actually writing the memory. Only one write barrier is made instead of N.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 450k
  • Answers 450k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try the following code: using(SPSite site = new SPSite(url)) using(SPWeb… May 15, 2026 at 8:36 pm
  • Editorial Team
    Editorial Team added an answer Put a border around a <tbody> instead, and make the… May 15, 2026 at 8:36 pm
  • Editorial Team
    Editorial Team added an answer Ok. So I found the solution for anyone else that… May 15, 2026 at 8:36 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.