2016-04-25 71 views
0

我有一个非常简单的Swift类,它只有一个静态方法,此方法分配3个字符串数组,并通过追加这些数组中的元素来创建一个随机字符串。内存泄漏[String]常量

下面的代码:

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)" 
} 

它发生此代码生成第一阵列上的内存泄漏,你可以在截图中看到: enter image description here

有人能告诉我一个理由它?以及如何解决这个问题

+1

如果你用这个方法数十亿次抛弃返回值并且耗尽自动释放池,你会看到堆增长吗?如果没有,这是一个误导性的诊断输出,你不应该担心它。 – werediver

回答

0

我做了@werediver的提及,增加了一个巨大的循环来调用这个方法,并且堆没有移动,也没有泄漏的数量,所以他提到这是一个误导性的诊断。无需采取任何行动。