簡體   English   中英

[String]常量上的內存泄漏

[英]Memory leak on [String] constant

我有一個非常簡單的Swift類,它只有一個靜態方法,該方法分配3個字符串數組,並通過從這些數組中追加元素來創建隨機字符串。

這里的代碼:

public static func generateText() -> String {

   let phraseComponent1 = [
        "Line 1,",
        "Line2,",
        "Line3,",
        "Line4,",
        "Line5,",
        "Line6,"]

   let phraseComponent2 = [
        "Line 1,",
        "Line2,",
        "Line3,",
        "Line4,",
        "Line5,",
        "Line6,"]

   let phraseComponent3 = [
        "Line 1,",
        "Line2,",
        "Line3,",
        "Line4,",
        "Line5,",
        "Line6,"]

    let componentIndex1 = Int(arc4random_uniform(UInt32(phraseComponent1.count)))
    let componentIndex2 = Int(arc4random_uniform(UInt32(phraseComponent2.count)))
    let componentIndex3 = Int(arc4random_uniform(UInt32(phraseComponent3.count)))

    let phrase1 = phraseComponent1[componentIndex1]
    let phrase2 = phraseComponent2[componentIndex2]
    let phrase3 = phraseComponent3[componentIndex3]

    return "\(phrase1) \(phrase2) \(phrase3)"
}

您可能會在屏幕快照中看到此代碼正在第一個陣列上生成內存泄漏: 在此處輸入圖片說明

有人可以告訴我原因嗎? 以及如何解決這個問題

我按照@werediver提到的那樣做,添加了一個巨大的循環來調用此方法,並且堆沒有移動,也沒有泄漏的數量,所以正如他所提到的,這是一種誤導性的診斷。 無需采取任何措施。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM