So I have this app that takes 2x9500 lines of data from 2 txt files and parses them into realm.
For that purpose, I loop through those lines like this:
func parseToRealm() {
// each of these files have 9500+ lines of data
// (basically dictionaries with word definitions)
let graphicsFileContents = readFile_Graphics()
let dictFileContents = readFile_Dict()
// check if counts of two source files match
if (graphicsFileContents.count == dictFileContents.count && graphicsFileContents.count > 1 && dictFileContents.count > 1) {
var i = 0
// make empty array of characters
var characterArr = [Characters()]
// loop through two files to get all chars
for jsonString in graphicsFileContents {
// parse data from string into json
let dataFromString = jsonString.data(using: .utf8)
let singleCharJson = try? JSON(data: dataFromString!)
// parse stuff from file1
// ... deleted lines for legal reasons
// DICT information
let dictDataFromString = dictFileContents[i].data(using: .utf8)
let singleDictJson = try? JSON(data: dictDataFromString!)
// parse stuff from that dictionary
// ... deleted lines for legal reasons
characterArr.append(Character)
// Every x characters, write them into DB
if (i % 150 == 0 || i == graphicsFileContents.count){
realmActions.writeCharsToRealm(characterArr: characterArr)
print("Writing \(i)-\(i + 150)")
// reset array to safe memory
characterArr = [Characters()]
}
i+=1
} // end loop file contents
}else{
print ("two files have different counts of lines. aborting...")
}
}
Explaining the code above:
- first I put all lines of the txt files into a variable
- then I loop through each line
- I parse each line into an array
- after parsing 150 lines into that array, I send them to realm
- repeat until done
Problem:
- I use way to much memory.
- I start at like 40, then I load the files.
- Then it jumps to 80ish
- As soon as the arrays get filled, it constantly grows until 399MB.
- Sidenote: before making steps of 150 items/time, the app would crush at 650mb memory on my iphone 6
- It takes at least two minutes.
The help I need:
- Most importantly: Where can I see where/on which variables the memory is used?
- Secondary: How to make it faster?
Edit: Showing test results. Seems I have a lot of allocations...