50

Since iOS 14 my iOS app prints this error message to the console (As the first message).

AppName(5088,0x1064438c0) malloc: nano zone abandoned due to inability to preallocate reserved vm space.

I couldn't find any other similar issues when searching on Google and I don't have any idea what's causing this. I'm running on an iPhone XS Max, iOS 14.0.1, Swift 5.

  • 4
    Same. No clue what might be the cause... –  Oct 09 '20 at 03:10
  • Same. iOS 14.4 Xcode 12.5 beta 3 – Shengchalover Apr 14 '21 at 09:20
  • Same Error message on Apple M1, Monterey, Xcode 13.1 (13A1030d). To duplicate, make a new Project, using Game template. Run it. Log reports about ten sanitizerBug -related bugs. Seems to hobble the thread sanitizer (but I'm just learning thread sanitizer). – Allen King Oct 27 '21 at 14:42
  • this link maybe usefull: https://stackoverflow.com/questions/69861144/get-an-error-as-a-out40780-0x1130af600-malloc-nano-zone-abandoned-due-to-in – Yu Jiaao Nov 14 '21 at 01:48

5 Answers5

32

After digging into Apple's libmalloc source code, I found the offending function nano_malloc. You can view the code at Apple's open source site here.

As you can see from the code, nano_malloc call nano_preallocate_band_vm to pre-allocate a certain amount of heap memory (for optimization purpose, I guess). If the kernel does not return an address at exactly NANOZONE_SIGNATURE (0x6 << 44), nano_preallocate_band_vm report failure ==> nano_init print out the message.

Unlike Linux community, Apple does not comment on their internal implementations, so I do not have any authoritative answer. But here is my hypothesis:

  1. Libmalloc preallocate memory for optimization purpose only ==> Failed to do so does not affect the result of any program.
  2. Address/Thread sanitizers allocate their data structures before libmalloc does ==> libmalloc can not acquire a memory chunk at the exact address as it expect ==> failed to pre-allocate.

Regardless, as the source code shows, Apple give you 2 ways to turn off this warnings:

  1. Undef NANO_PREALLOCATE_BAND_VM (only viable if you compile libmalloc from source)
  2. Set the environment variable MallocNanoZone to 0.

I did try the 2nd option and success.

FYI, It's not Xcode or Swift's specific problem. I compiled C/C++ code with Address Sanitizer clang -fsanitize=address -o main main.c, the program emit exactly the same warning and the same solution worked.

My tools' version for future reference:

$ clang --version
Apple clang version 13.0.0 (clang-1300.0.29.3)
Target: x86_64-apple-darwin21.1.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

$ sw_vers
ProductName:    macOS
ProductVersion: 12.0.1
BuildVersion:   21A559
mibu
  • 1,303
  • 11
  • 14
  • All the code if your .zprofile is empty `touch ~/.zprofile`, `open ~/.zprofile`, `export MallocNanoZone='0'` – A.Ametov Jan 15 '23 at 16:05
2

Try going to: set active scheme (next to the play and stop button) -> edit scheme -> diagnostics, then turn on "Thread Sanitizer." With Thread Sanitizer on, the error will be caught and you will receive a warning that is more descriptive in the debugger. If the description is a Swift access race, then you should use a Thread Safe Barrier in your code.

murp
  • 403
  • 5
  • 10
  • Thanks for your reply. I tried this (noticed that Thread Sanitizer only works for the Simulator also) and I don't get any Threading issues as they did in this video, that's how Thread Sanitizer works right? https://www.youtube.com/watch?v=_ds7CGc_kjE – stackoverflowlivesmatter Oct 02 '20 at 11:04
  • Yes it should point out any threading issues... however, in my experience I have had the Thread Sanitizer not point out any issues when I should have been running into issues while reading and writing data to the same datasource while using a concurrent queue... But best of luck! If it is a threading issue these unfortunately can be pretty difficult to debug :( – murp Oct 03 '20 at 16:39
  • 4
    Doesn't answer the question asked – hhanesand Dec 29 '20 at 20:14
1

Turn off Thread Sanitizer in Scheme

I noticed this error would appear and disappear with the project Scheme's Diagnostic option under Runtime Sanitization: Thread Sanitizer Uncheck Thread Sanitizer

Mark Moeykens
  • 15,915
  • 6
  • 63
  • 62
0

I don't know how you built your code but in my case, all I did is add a do try catch in my code. Something from this:

static func fetchAlbumWithAsyncURLSession() async throws -> [Album] {
    guard let url = URL(string: "https://itunes.apple.com/search?term=taylor+swift&entity=album") else {
        throw AlbumsFetcherError.invalidURL
    }
    let (data, _) = try await URLSession.shared.data(from: url)
    let iTunesResult = try JSONDecoder().decode(ITunesResult.self, from: data)
    return iTunesResult.results
}

To something like this:

static func fetchAlbumWithAsyncURLSession() async throws -> [Album] {
    guard let url = URL(string: "https://itunes.apple.com/search?term=taylor+swift&entity=album") else {
        throw AlbumsFetcherError.invalidURL
    }
    do {
        let (data, _) = try await URLSession.shared.data(from: url)
        let iTunesResult = try JSONDecoder().decode(ITunesResult.self, from: data)
        return iTunesResult.results
    } catch {
        print("Request failed with error: \(error)")
        return []
    }
}

And then I got the error :D

Best regards!

Andres Paladines
  • 1,142
  • 14
  • 19
0

In my case, I had too much going on in the main thread. I pushed a heavy task to the back of the queue and the warning went away.

Before

.onAppear() {
    player.play()
}

After

.onAppear() {
    DispatchQueue.main.async {
        player.play()
    }
}
bobby123uk
  • 892
  • 4
  • 17