0

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?

Screenhot of memory usage: 400mb memory usage. what the...

Edit: Showing test results. Seems I have a lot of allocations... Test results

ItsMeDom
  • 540
  • 3
  • 5
  • 18
  • “Where can I see where/on which variables the memory is used?” With Instruments. – matt Feb 18 '19 at 04:13
  • “How to make it faster?” Use Instruments to find the slow part. – matt Feb 18 '19 at 04:14
  • See [Is it necessary to use autoreleasepool in a Swift program?](https://stackoverflow.com/questions/25860942/is-it-necessary-to-use-autoreleasepool-in-a-swift-program) – rmaddy Feb 18 '19 at 04:15
  • Edited question to include screenshot of test results... Looks like I got a lot of "total number of repetitions of a particular allocation." from: https://developer.apple.com/library/archive/technotes/tn2434/_index.html. – ItsMeDom Feb 18 '19 at 04:28
  • Now it seems, JSON eats all the memory... – ItsMeDom Mar 04 '19 at 09:41

0 Answers0