Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

986
Views
Memory management issue - Message from debugger: Terminated due to memory issue

On the simulator I was noticing some lag, but it runs fine. I finished the feature and went to run it on my phone and it crashes saying:

Message from debugger: Terminated due to memory issue

I do have zombie objects turned off in the Schema (as I was reading it can give problems)

Doing some debugging I found the issue happens after I assigned the JSON downloaded from the api, so maybe the object is not being released? This is the first time I face something like this. Here's how the api request looks:

let fetchWalletBalances = Task(priority: .high) {
    try await fetchTokenBalances(user: user, userWallets: userWallets, verifiedTokensDictionary: verifiedTokensDictionary)
}
let tokensPlusCoinGeckoIds = try await fetchWalletBalances.value

At this point the memory shows on XCode like 1Gb already

enter image description here

then I assign it to do further calculations (all in my view model):

let walletsTokenData: OrderedDictionary<String, [TokenBalanceClassAModel]> = tokensPlusCoinGeckoIds.walletsTokenData

and here it crashes.

How can I release some memory or go about this issue?

EDIT:

After lots of debugging and trying some of the tools suggested by @Alexander, I found that the memory leak happens in the block below, it jumps from 33 Mb to 1.76 GB (I'm using Alamofire to do the api fetch):

func afRequest(url: URL) async throws -> Data {
    try await withUnsafeThrowingContinuation { continuation in
        // If another error try lowering the request error: (statusCode: 200..<300)
        // timeoutInterval is in seconds
        AF.request(url, method: .get, requestModifier: { $0.timeoutInterval = .infinity })
            .validate(statusCode: 200..<600)
            .responseData { response in
            
                if let data = response.data {
                    continuation.resume(returning: data)
                    return
                }
            
                if case let .failure(error) = response.result {
                    print("๐Ÿ›‘ Error on afRequest(): \(error)")
                    continuation.resume(throwing: error)
                    return
                }
            }
    }
}

The memory jumps at line: AF.request(url, method: .get, requestModifier: { $0.timeoutInterval = .infinity }).validate(statusCode: 200..<600).responseData { response in

The problem is that on every loop (see below) it keeps adding the memory from the previous iteration, so it's never clear.

for chain in chains {
    if(chain.chain_id != nil) {
        // at some point here I get 1.67 GB of memory, shouldn't at the end of the loop clear that memory?
        let decodedData = await tokenBalancesClassAViewModel.fetchTokenBalancesData(walletAddress: wallet.address, chain: chain.chain_id!)
        let data: TokenBalanceClassAModel = decodedData
        let chainId = data.data.chain_id

        if(chainId == nil) {
            shouldCacheBalances = false
            deleteCachedData()
            print("๐Ÿ›‘ Reloaded Chain: Nil. Balances won't be cached")
        } else {
            // Save into Single Address
            singleWalletData.append(data)
            print("๐Ÿ” Reloaded Chain: \(chainId!)")
        }
    }
}

What's happening there and how can I fix it?

over 4 years ago ยท Santiago Trujillo
1 answers
Answer question

0

I found the issue. The problem was in the view with this block during the ScrollView:

else if(updateAPIDataViewModel.allWalletsData.walletsData.isEmpty && updateAPIDataViewModel.isTotalPortfolioBalanceBeingCalculated && !walletDataStoreHandlerViewModel.userWallets.isEmpty) {
    ScrollView(.horizontal, showsIndicators: false) {
        ForEach(walletDataStoreHandlerViewModel.userWallets) { wallet in
            HStack {
                ForEach(0..<(wallet.chainsWithBalance?.count ?? 0)) { _ in
                    DummyWallet()
                            .redacted(reason: .placeholder)
                }
            }
        }
    }
}
over 4 years ago ยท Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!