Skip to content

[SR-5472] Swift 4.0 Memory leak on Linuxies using URLSession? #3831

Closed
@johnno1962

Description

@johnno1962
Previous ID SR-5472
Radar None
Original Reporter @johnno1962
Type Bug
Status Closed
Resolution Done
Environment

Linux 16.04, Swift built from master 2017-07-16

Additional Detail from JIRA
Votes 0
Component/s Foundation
Labels Bug, 4.0Regression, Linux
Assignee None
Priority Medium

md5: e055efa0a71c9a5f3c321efec7fe7e4e

Issue Description:

Hi, I’ve been looking at the Linux variant or Swift and found what could be a memory leak when using URLSession. If I place the code below in a file and execute using the REPL, the recorded memory continually increases on Linux by about roughly the amount of the Data received from the website. Memory also increases indefinitely on macOS but the effect is not quite so pronounced. This seems to be a regression relative to Swift 3.1.

import Foundation
import Dispatch

let url = URL( string: "http://www.bbc.co.uk/news" )!
let session = URLSession(configuration: .default)
let request = URLRequest(url: url)
var memory = rusage()

DispatchQueue.global().async {
    while true {
    sleep(1)
    session.dataTask(with: request) {
                   (data, response, error) in
        // NSLog( "Fetch \(data?.count ?? -1) \(String(describing: response)) \(String(describing: error))")
        if data == nil {
            return
        }
        getrusage(0, &memory)
        print( "Done \(memory.ru_maxrss)" )
    } .resume()
    }
}

RunLoop.main.run()

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions