# DropCharge causing memory leak

**URL:** <https://forums.kodeco.com/t/dropcharge-causing-memory-leak/13203>\
**Category:** 2D iOS & tvOS Games by Tutorials\
**Created:** [April 12, 2016, 10:10am UTC](https://forums.kodeco.com/t/dropcharge-causing-memory-leak/13203 "2016-04-12T10:10:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tom\_shen](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/tom_shen/32/81494_2.png) [@tom\_shen](https://forums.kodeco.com/u/tom_shen)\
**Post date:** [April 12, 2016, 10:10am UTC](https://forums.kodeco.com/t/dropcharge-causing-memory-leak/13203/1 "2016-04-12T10:10:36Z")

</div>

I make the DropCharge game by following the book. After I finish, I go ahead and check the memory pane of the Debug Navigator, I found out that the memory is constantly going higher and higher every time, and when the game is over, pressing restart to open a new scene doesn’t free up any memory. Seems like something is making a retain cycle. Any idea or running into similar issue? Thanks.

---

<div class="post-metadata">

**Author:** ![aeberbach](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/aeberbach/32/81494_2.png) [@aeberbach](https://forums.kodeco.com/u/aeberbach)\
**Post date:** [April 14, 2016, 2:04am UTC](https://forums.kodeco.com/t/dropcharge-causing-memory-leak/13203/2 "2016-04-14T02:04:48Z")

</div>

While you’re in the simulator you will be emulating the 3D hardware in the phone and even on a fast Mac that can cause some serious performance issues. Give it a try on the actual hardware… I was amazed at how slow and how much memory was being consumed when trying some SpriteKit programming on the simulator, in fact I thought my app had serious issues where in fact it was fine.

---

<div class="post-metadata">

**Author:** ![ivanchenhz](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/ivanchenhz/32/67380_2.png) [@ivanchenhz](https://forums.kodeco.com/u/ivanchenhz)\
**Post date:** [April 21, 2016, 7:31am UTC](https://forums.kodeco.com/t/dropcharge-causing-memory-leak/13203/3 "2016-04-21T07:31:27Z")

</div>

There is memory leak because we don’t remove the off screen background node and the foreground overlays.

Found it yesterday and fixed like this:

```
func isOffScreen(node: SKNode) -> Bool {
    var isOffScreen = false
    
    let cameraPos = self.getCameraPosition()
    if node.position.y + self.backHeight < cameraPos.y {
        isOffScreen = true
    }
    
    return isOffScreen
}

func removeOffScreenBackgroundNode() {
    bgNode.enumerateChildNodesWithName("Overlay") { (node, _) -> Void in
        if self.isOffScreen(node) {
            node.removeFromParent()
        }
    }
}

func removeOffscreenOverlayNode() {
    for node in fgNode.children {
        if isOffScreen(node) {
            node.removeFromParent()
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![mokhet](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/mokhet/32/102075_2.png) [@mokhet](https://forums.kodeco.com/u/mokhet)\
**Post date:** [June 14, 2020, 10:23am UTC](https://forums.kodeco.com/t/dropcharge-causing-memory-leak/13203/4 "2020-06-14T10:23:31Z")

</div>

As of june 2020, swift 5 iOS 13.5, I have updated the method “updateLevel” based on @ivanchenhz solution. When overlays are added in the scene, check every “Overlay” nodes out of camera and remove them. I am quite new to swift so please let me know if this is the wrong way to “fix” the issue.

First, let’s see the issue :

- set to true view.showsFPS and view.showsNodeCount in GameViewController.swift
- set an insane amount of gain in setPlayerVelocity(\_: ) to very quickly add new layers

```auto
func setPlayerVelocity(_ amount:CGFloat) 
{
 let gain: CGFloat = 100
 player.physicsBody!.velocity.dy = max(player.physicsBody!.velocity.dy, amount * gain)
}

```

Quickly, the amount of nodes is above 10K, 15K, 20K, too much K and FPS goes from 60fps to almost 0

Now, let’s remove nodes not needed anymore since they will never be visible again

```auto
func updateLevel() 
{
 let cameraPos = camera!.position
 if cameraPos.y > levelPositionY - size.height 
 {
  createBackgroundOverlay()
  while lastOverlayPosition.y < levelPositionY 
  { 
   addRandomForegroundOverlay() 
  }

  // Start fix here, remove off screen nodes
  enumerateChildNodes(withName: "//Overlay")
  {
   [weak self] node, _ in
   guard let self = self else { return }

   let cameraPos = self.camera!.position
   if node.position.y + self.backgroundOverlayHeight < cameraPos.y
   {
    node.removeFromParent()
   }
  }
 // End fix 
 }
}

```

Now, the nodes count stay in an acceptable range 150…250 and FPS stay at 60fps

I hope it can be of any help.

---

<div class="post-metadata">

**Author:** ![mokhet](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/mokhet/32/102075_2.png) [@mokhet](https://forums.kodeco.com/u/mokhet)\
**Post date:** [June 19, 2020, 4:07pm UTC](https://forums.kodeco.com/t/dropcharge-causing-memory-leak/13203/5 "2020-06-19T16:07:41Z")

</div>

And when you read further the latest book version (as of june 2020), you have the required code to address the issue at the end of chapter 17 section IV. I might have read the whole chapter before trying to fix the issue by myself 😛

```auto
// remove old foreground nodes...
for fgChild in fgNode.children {
  let nodePos = fgNode.convert(fgChild.position, to: self)
  if !isNodeVisible(fgChild, positionY: nodePos.y) {
    fgChild.removeFromParent()
  }
}

```

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [July 7, 2020, 7:04pm UTC](https://forums.kodeco.com/t/dropcharge-causing-memory-leak/13203/6 "2020-07-07T19:04:57Z")

</div>

@mokhet Thank you for sharing this - much appreciated!

---

<div class="post-metadata">

**Author:** ![edinlipka](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/edinlipka/32/81494_2.png) [@edinlipka](https://forums.kodeco.com/u/edinlipka)\
**Post date:** [May 10, 2021, 7:15am UTC](https://forums.kodeco.com/t/dropcharge-causing-memory-leak/13203/7 "2021-05-10T07:15:14Z")

</div>

Hi…I’m additionally encoutering this equivalent memory spill. For my situation CGDataProviderCreateWithCopyOfData is made everytime I take pictures with my photograph application. This article will at that point endure in memory after I take various photographs until I get an out of memory blunder. In the event that I utilize the “Holes” Xcode Instrumentation apparatus, it focuses to AVCapturePhotoOutput - init similar to the wellspring of the memory spill. I’m utilizing xcode 12 beta 3/4 however I’ve likewise given this a shot a couple of different adaptations of iOS and I get a similar outcome (ie iOS 10.2, 11.4, 12 b3). Would you be able to if it’s not too much trouble, direct me toward a guide that would help in settling this.
