ivnsch / SwiftCharts

Easy to use and highly customizable charts library for iOS
Apache License 2.0
2.53k stars 410 forks source link

CoreAnimation: failed to allocate 5285840 bytes #341

Closed iOSDevGarg closed 6 years ago

iOSDevGarg commented 6 years ago

Hi, I had used this awesome Chart in Project and sometimes getting error as Described in Title,

I have added some Sort of minor code about points only , My app Flow is 8 UISliders Connected with different Handlers and each Handler on release of UISlider Calls a Func that create a Cubic chart and Those returned Charts.View is Added In my subviews

Issue Persists When I Continuously use app and update Slider Values , I got this error printed

"CoreAnimation: failed to allocate 5285840 bytes" "CoreAnimation: failed to allocate 7333952 bytes" "CoreAnimation: failed to allocate 5285840 bytes"

and I am using one Global instance of var chart : Chart? and updating Returned chart in it only and clearing it at beginning of Func and Adding it again after removing old added chartView in my Subview

Used Code --

 func createCubicChart(chartPointss: [(Double,Double)], chartPointss1: [(Double,Double)], GreenDotPoints: [(Double,Double)])
{

    self.chart?.view.removeFromSuperview()

    let staticChartYAxisPoints = ForYAxisPoints.map{ChartPoint(x: ChartAxisValueDouble($0.0), y: ChartAxisValueDouble($0.1))}

    //setting default according to screen size
    let labelSettings = ChartLabelSettings(font: ExamplesDefaults.labelFont)
    let labelSettings1 = ChartLabelSettings(font: ExamplesDefaults.fontWithSize(Env.iPad ? 14 : 16))

    //add points to chart
    let chartPoints = chartPointss.map{ChartPoint(x: ChartAxisValueDouble($0.0), y: ChartAxisValueDouble($0.1))}

    //Assign X and Y values
    let xValues = ChartAxisValuesStaticGenerator.generateXAxisValuesWithChartPoints(chartPoints, minSegmentCount: (Double(chartPoints.count) + 1), maxSegmentCount: (Double(chartPoints.count + 4)), multiple: 0.1, axisValueGenerator: {ChartAxisValueDouble($0 - 0.10, labelSettings: labelSettings)}, addPaddingSegmentIfEdge: false)
    var yValues = ChartAxisValuesStaticGenerator.generateYAxisValuesWithChartPoints(staticChartYAxisPoints, minSegmentCount: 3, maxSegmentCount: 3, multiple: 2, axisValueGenerator: {ChartAxisValueDouble($0, labelSettings: labelSettings)}, addPaddingSegmentIfEdge: false)

    let xModel = ChartAxisModel(axisValues: xValues, axisTitleLabel: ChartAxisLabel(text: "FQ", settings: labelSettings1))
    let yModel = ChartAxisModel(axisValues: yValues, axisTitleLabel: ChartAxisLabel(text: "Calories", settings: labelSettings1.defaultVertical()))

    //chart settings like zooming , pan , or not
    var chartSettings = ExamplesDefaults.chartSettings
    chartSettings.axisStrokeWidth = 1

    //Add axis layers
    let coordsSpace = ChartCoordsSpaceLeftBottomSingleAxis(chartSettings: chartSettings, chartFrame: CGRect(x: self.chartVieww.frame.origin.x, y: self.chartVieww.frame.origin.y, width: self.chartVieww.frame.size.width, height: self.chartVieww.frame.size.height), xModel: xModel, yModel: yModel)
    let (xAxisLayer, yAxisLayer, innerFrame) = (coordsSpace.xAxisLayer, coordsSpace.yAxisLayer,coordsSpace.chartInnerFrame )

    //Line model connects two points
    let lineModel = ChartLineModel(chartPoints: chartPoints, lineColor: UIColor.black, lineWidth: 1, animDuration: 1, animDelay: 0)

    //CubicLinePathGenerator ------ Necessary
    let chartPointsLineLayer = ChartPointsLineLayer(xAxis: xAxisLayer.axis, yAxis: yAxisLayer.axis, lineModels: [lineModel], pathGenerator: CatmullPathGenerator())        

    let settings = ChartGuideLinesDottedLayerSettings(linesColor: UIColor.black, linesWidth: ExamplesDefaults.guidelinesWidth)
    let guidelinesLayer = ChartGuideLinesDottedLayer(xAxisLayer: xAxisLayer, yAxisLayer: yAxisLayer, settings: settings)

    //Setting circle dot of dots
    let chartPointsCircleLayer = ChartPointsViewsLayer(xAxis: xAxisLayer.axis, yAxis: yAxisLayer.axis, chartPoints: chartPoints, viewGenerator: circleViewGenerator, displayDelay: 0, delayBetweenItems: 0.05, mode: .translate)

    //Chart Layers
    let chart = Chart(
        frame: chartVieww.frame,
        innerFrame: innerFrame,
        settings: chartSettings,
        layers: [
            xAxisLayer,
            yAxisLayer,
            guidelinesLayer,
            chartPointsLineLayer,
            chartPointsCircleLayer,
            ]
    )

    //add subviews
    self.chartVieww.addSubview(chart.view)
    self.chart = chart
}

}

Leaks While I move slider screen shot 2018-03-31 at 10 53 35 am

Note: Sometime That core Animation error logs sometimes not But Views Disappear and application hangs Memory on which app hangs - When Memory Usage went above 160 MB Approx(Not to large) How I Can solve this Issue , Please Help