scene transition causes memory leak - swift

I'm making a simple game using Xcode 10.1 IDE and swift 4.2
I've designed the main menu system so that each pages UI is presented through a separate scene.
Each time a new scene is loaded I get approximately 0.1 MB increase in memory usage. Not much but I don't want to start scaling the game with this issue.
Memory leak when presenting SpriteKit scenes
Memory problems when switching between scenes SpriteKit
Tab-based SpriteKit Apps and Scene Caching
I've had a good look through reference material and online. I've checked for Retain Cycles, through the following: Inserted deinit statements at the end of all scenes and object classes used. They are all called correctly. Profiled the app to look looking for zombie objects and leaks, with nothing obvious shown in the profiler results when running.
Does anyone have any idea to what causes memory leaks or caching on scene transitions and ways to prevent this?
I'm stumped scaling the game now seems like the wrong thing to do as the issue will probably compound as complexity increases.

After further testing I found the issue is with attaching sound actions through scene editor.
Attaching the actions through code gives stable memory use, with no increase of 0.1MB through scene transition.
Its only a partial answer but if any one else comes across this and has further info please post

Related

How to remove level 1 from memory when replaying it?

I am programming a game using scenekit that has different levels, that are different files (uiviewcontrollers) (level1.swift, level2.swift...) When you complete a level, you can replay it. What i have noticed, is when you start the game, it uses about 33 mb memory. Now when you press a replay button which presents the level 1 viewcontroller again, the game uses about 60 mb of memory.
So the question is, Is there a way i could somehow remove the level 1 when i present it again? Something like:
Level1. RemoveFromMemory
Self.presentViewController(level1)?
Those sounds like symptoms of a memory leak. You could cause this by keeping multiple strong references to the scene assets. You could also cause it by writing a retain cycle: parts of the scene hold strong references to each other, so even when you release the scene it can't let itself be deallocated.
Run your game using the Leaks Instrument. The WWDC videos on Instruments are helpful. And the Memory Graph Debugger in Xcode 8 will help you a ton.

SpriteKit scene with low fps on start

My problem is that my GameScene starts with about double the nodes and draws count and 40 fps for several seconds. This problem appears only on my iPad (mini retina) while on my iPhone (5) the game runs smoothly from the start although the nodes are still a lot more than what it should be
This 40fps problem is an issue within the iOS frameworks. It's created by some kind of throttling (by iOS).
Perhaps this throttling was designed to give a more consistent experience for an app struggling to maintain 60fps. But nobody knows.
Apple has never commented on it.
It became prevalent throughout the rollout of iOS 9 in Scene Kit, Sprite Kit and Metal. But has been seen within OpenGL ES locked projects, too.
However it was noticed in previous versions of iOS, too. Particularly within apps/games using CADisplayLink.
//Don't worry about those asking for code, you're right in your assumption that something is wrong that's not pertinent to your code.
Here's a deeper examination of a similar issue (probably from the same route cause) within Scene Kit: Inconsistent SceneKit framerate
Also got a problem with low FPS at the start (when using spritekit and uikit together )
I used xib with SKView and for me the solution was to add a dependency to the scene in the 'attribute inspector'
like at the picture below:

cocos2d using gobs of memory

I'm having the absolute worst time with running out of memory in my game with cocos2d.
I have 4 scenes. two vanish from memory when I switch scenes using
[[CCDirector sharedDirector]replaceScene:sceneWithTransition]
I've tried push/pop and it makes the problem about 10x worse.
2 of the scenes however are like a virus. The conditions under which I can remove them from memory do not exist.
I am using the ARC alterations. I was hoping they would help, they did not.
I've overridden the cleanup method to make sure I get rid of references to everything in the scenes. The first one is the game play scene, and I can imagine how it might be possible that I've missed something that is somehow holding on to the scene. However the other one is the settings scene. It has 5 parts. they all get tossed and yet the scene will not purge. before converting to ARC the app was unusable it crashed so often and when it wasn't crashing it was running out of memory. After ARC it lasts about 4x longer than before but still runs out of memory regularly. Crashes however are almost non-existant.
I suppose first off, what is the correct way to switch between scenes?
Second, how does one remove something from memory when using arch because object = nil does nothing. All of my other programs using ARC run like dreams. None of them use the cocos2d engine. I'd not seen a memory error since the introduction of arc until I made the mistake of converting my quartz2d game in to cocos2d. The only reason I don't o back to a platform that actually functions is that I really like the effects possible in cocos that I simply cannot make work in quartz.
It seems that cocos2d had some issues with ARC that got fixed only recently.
I don't know if this has to do with your memory problems, but you might check which cocos2d version you are using.
Anyway, it seems that cocos2d will fully support ARC only from version 2.0. So, you might be better off not using ARC and getting your memory management right by properly using release. As far as I know, cocos2d has no memory problem, so you should be able to accomplish it.
push/pop from what I understand use a "stack" model thus memory is not erased when new scenes are pushed or popped, which would explain why that gets worse when using this method.
one way you can help save memory, especially during a transition, is to transition to a loading scene and then from there transition to the actual scene you want. This is because during a normal transition, both scenes occupy memory simultaneously for at least 1 frame, whereas a cheap loading scene will instead take each of their places, reducing the horrible memory spike that often occurs when transitioning between two intense scenes.

iPhone Opengl game with ads == fps problem?

I have a game that runs fine as is (around 30fps), but fps went down the drain when I tried to implement ads. I tried Greystripe and iAds but with same result (iAds were maybe bit worse). Average fps is almost same, but there are huge spikes all the times (1-2 spikes per second) and game is unplayable.
I guess it is because ad is in another view. I read somewhere that opengl apps on iphone don't like having another views with them, but there is plenty of games with ads on app store. How do they do it?
My implementation should be ok. I did everything as documentation and samples told me. I have my opengl view and ad view as subviews in app window, adview being in front of opengl view and thus covering part of it. Could this be the problem? Is it better to make opengl view smaller to left space for ad so they don't overlap? Do you have any other ideas what could be wrong?
Lope, I've created a gist at this link with a singleton "AdManager" class I wrote to handle iAds using cocos2d. Cocos2d sits on top of OpenGL, of course, and I've found that this code doesn't affect FPS even for relatively complicated games.
You'll have to modify this a bit to work with your application, changing out the cocos2d calls, etc, but this will give you asynchronous loading of iAds, which should help the FPS issue.
To use this class, include its header and call
[[AdManager sharedManager] attachAdToView:self.view];
wherever you need iAds. The ads will remain hidden until an ad loads, at which time they'll pop up at the top of screen. (The class works for iOS 4.0, 4.1 and 4.2).
Also, I should add that I have cocos2d running inside of an overall UIViewController that I call "Cocos2DController". When I attach the ads to a cocos2d view, I'm using
[[AdManager sharedManager] attachAdToView:[[CCDirector sharedDirector] openGLView]];
Best of luck!
We can hit and miss with apple's choices, but go for the sure thing and implement the ads in other parts to be appealing and not intrusive. It will be better for the framerate, and for you.
Try downloading the ads in a seperate, low priority, thread. You can, thus, nsure that the ads loading does not take too much CPU time. With a bit of CPU synchronisation you can make sure you don't try to display the new ad until it is completely ready to display. Sure it will suck some CPU time away from what you are trying to do but set your priorities right and it should only suck time when you are busy doing nothing.
Please excuse the thread necro'ing here, but I've used Stack Overflow a lot to help me through the problems I've had during coding, and thought my experience might be useful to someone in the future.
My simple cocos2d game ran with decent FPS (rarely changed the FPS display at all) until I implemented AdWhirl (integrating AdMob + iAd only). It would then run OK for the first few iterations, but upon upon the 9th or 10th scene refresh (single screen game, time in each scene < 5 seconds on average) the FPS would dive to ~20FPS, and drop again each time the scene refreshed.
Turns out, in my n00biness (this may be particular to me :) ), I was calling the scene from within itself. That is, once the actions had finished, the last action was to call the main scene again (a lazy way of rebuilding the scene for the user to have another go). This init'd the views and view controllers I had inserted to handle the AdWhirl ads all over again, and not only did I have a memory leak, I had 10+ view controllers all trying to request and service ads from AdWhirl. Once I got a clue and took that self-referring loop out, all was good.

iPhone: Steadily increasing memory usage in apps using touches

I've observed similar behavior in an Apple example app and a game I am working on. In the game, the behavior is eventually causing the app to crash due to running out of memory. The example app is Touches.
At any point when touches are being tracked, which is when you're moving one of the objects around in Touches, and pretty much any time a touch is down in my game, memory usage goes up steadily, for as long as you continue moving the touch around. Once the touch sequence completes, the memory usage does not go back down. I've gotten Touches, which starts off using less than half a meg, up to about 4MB net allocations with a few minutes playing around. That memory is never deallocated.
So my question is: why does this memory never get deallocated? Am I fundamentally misunderstanding something? Is this a framework flaw? I've read some about issues with the accelerometer and touches leaking, but I'm not using the accelerometer at all in this game.
I apologize if this seems TOO elementary, but...
There's no chance you have NSZombiesEnabled set to YES, do you?
That would prevent the deallocation of any objects whatsoever, and if you did allocate new ones, then EVENTUALLY the app will run out of memory.
Use instruments to find what instances are not being deallocated. I had a similar problem, and all it came down to was that I was calling retain, but not release (due to a logic error).
Im in a very similar situation. In fact, if i just let me app run without touching anything (all it does is run an NSTimer to update the display). And i notice the memory slowly but steadily increasing in usage.
If there are memory leaks its not obvious. Because ive seen memory leaks popup whenever it detects one.
So does that mean there are undetectable memory leaks?