My app is an app that records information about clothes purchased.
It consists of a total of 3 TabBars that show the items sorted by purchase date, brand, and category.
All 3 views use the SectionedFetchRequest property wrapper.
The problem is that it takes up an unusually large amount of memory as the number of logged items increases. I'm using over 100MB of memory when the number of items goes beyond about 20, and up to 450MB or more when scrolling through the items, especially if each item contains an image.
Images are compressed using jpegData(compressionQuality: 0.3) as a Binary Data type and stored in CoreData.
Is there any way to minimize the memory used by the app? Am I using SectionedFetchRequest unnecessarily? (Brand Or Category Tab View -> DetailView)
Memory leaks can occur in SwiftUI apps that use CoreData, but they are not necessarily unusual. SwiftUI and CoreData are two powerful frameworks, and managing memory correctly when using them together is crucial to avoid leaks.
To help you diagnose and fix the memory leak in your SwiftUI app that uses CoreData, here are some common causes and potential solutions:
Retain cycles: Retain cycles occur when objects hold strong references to each other, preventing them from being deallocated. In the context of SwiftUI and CoreData, make sure you're not creating strong references to views or view models from your CoreData entities or managed object contexts. Instead, use weak or unowned references where appropriate.
Forgetting to unsubscribe from notifications: If you're observing notifications such as NSManagedObjectContextObjectsDidChangeNotification, make sure to remove your observers when they are no longer needed. Failure to unsubscribe from notifications can lead to objects being retained in memory even when they are no longer relevant.
Large fetch requests: If your app performs large CoreData fetch requests without proper batching or pagination, it can lead to excessive memory usage. Consider optimizing your fetch requests to fetch smaller batches of data at a time, especially if you're dealing with a large data set.
Using excessive memory in views: SwiftUI views can consume a significant amount of memory if not managed properly. Make sure you're not storing unnecessary data in your views or view models. Avoid keeping large arrays or other data structures directly in your views, and consider lazy-loading or paginating data to minimize memory usage.
Not using @FetchRequest correctly: SwiftUI provides the @FetchRequest property wrapper to simplify working with CoreData fetch requests. Ensure that you're using it correctly and efficiently. Make sure the fetch request is properly configured with predicates, sort descriptors, and other options to minimize the amount of data fetched from the persistent store.
Long-lived references to managed objects: Be cautious with long-lived references to managed objects, especially if you pass them between views or store them in view models. Consider using object IDs instead of directly passing or storing managed objects, and use the object ID to retrieve the object from the managed object context when needed.
Improper handling of CoreData contexts: Make sure you're using the appropriate types of CoreData contexts (e.g., main queue context, private queue context) and that you're using them on the correct queues. Perform CoreData operations on the appropriate queue to avoid potential concurrency issues and memory leaks.
Remember to use memory profiling tools like Instruments in Xcode to identify specific memory allocations and track down the source of the leaks. These tools can help you identify patterns and pinpoint areas of your code that may need attention.
If you're still unable to resolve the memory leak, consider posting a specific code snippet or providing more details about your implementation. That way, I can offer more targeted suggestions.
Thanks for your answer.
I noticed a significant increase in memory when retrieving an image from the List that I had stored in the core data, so when I saved the image, I saved the original image and the image I wanted to use for the thumbnail (CGSize(width:56, height:56)) so that I could minimize memory usage by showing the thumbnail image in the main List.
However, I implemented a stick HeaderView that uses a ScrollView + GeometryReader in a DetailView to show the image in a set proportion. I used the original image, and it consumes a lot of unnecessary energy, as the image keeps redrawing every time I scroll. (In the simulation, it stutters).
I'm trying to figure out how to solve this problem.
SPONSORED AppSweep by Guardsquare helps developers automate the mobile app security testing process with fast, free scans. By using AppSweep’s actionable recommendations, developers can improve the security posture of their apps in accordance with security standards like OWASP.
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.