KSP-KOS / KOS

Fully programmable autopilot mod for KSP. Originally By Nivekk
Other
697 stars 230 forks source link

Potential memory leak with Lexicons #1960

Open aidygus opened 7 years ago

aidygus commented 7 years ago

I'm working on a script in one of my repos (astar rover) and making heavy usage of lexicons and lists. I've noticed that when executing the script KOS is chewing through huge chunks of memory which is not being released.

Normally my Dev KSP runs around 2.2 gig but after 7 or 8 executes of this script it's sitting up around 13 Gig. Most I can run this script is 10 times before I have to kill the process to free memory again.

Normal memory usage on starting After running the script for the 5th time

Most of these lexicons don't get more than a couple of hundred elements in size and I've added a quit feature to clear them out before exiting in case that was causing an issue.

Dunbaratu commented 7 years ago

This issue's title says lexicons are the problem but the code provided in the repository you linked to contains no lexicons as far as I can tell. It contains a lot of lists, but no lexicons.

Is the title incorrect (should it really be about lists not lexicons) or is the example code incorrect (wrong link maybe)?

aidygus commented 7 years ago

Try the feature branch. You'll be looking at the master.

The file in question is astar2.ks

hvacengi commented 7 years ago

I haven't done anything to try and run it, but could you provide some additional information about how you are running this script? Are you running rover.ks 7 or 8 times, or just astar2.ks? How fast are you running each instance, and how long do you wait to see if the memory is freed? When you say you created a quit feature, what do you mean?

Have you tried isolating the problem to just issues with lexicons, eliminating the rest of your script? (I know that the point is that it runs in a complicated script, but the more isolated/simplified the reproduction steps are the easier it will be for us to check the behavior)

EDIT: Also, does rebooting the processor help clear memory?

aidygus commented 7 years ago

I start the process with runpath("/asrover/rover.ks"). and launch the astar algorithm through that. It can be ran independently as well by specifying LAT/LAN values or a Waypoint name.

Within the Rover script are options to call astar, either using the cursor keys to move the indicated vector to where I want to go and hit return to execute the search or if in a contract situation press w and corresponding number for the waypoint.

The first 4 or 5 times it runs normally but after the 6th time there is a noticeable pause before the scripts start which gets longer and longer after each run attempt. The amount of RAM KSP uses increases from a few meg to gigs at a time by the 10th run. I watched as KSP was sitting at 9 gig then running the script it lept up to 13 and this is when the slowdown happens as I've got 16 gig in my machine and it starts to page the excess memory.

Pressing the End key while searching for a path will initiate a cleardown of the Lexicons and exit both scripts.

Rebooting the core doesn't clear the memory.

I left KSP running with no scripts for about 10 minutes after exiting and the memory does not get reclaimed.

Using the LIST method in the original astar.ks does not have this memory issue.

The only mods on the Dev installation are KOS, KRASH and Engineer Redux installed with ckan.

aidygus commented 7 years ago

I've done some further testing with this.

I created this script which creates the same amount of Lexicons and fills them with roughly the same amount of data.

I managed to run the script about 30 times without seeing the memory leak occur.

In the case of my rover script I'm calling it from within another script so I created another simple one with the same type of Until runmode = -1 loop and called the above script over and over again.

Still it manged to run without the memory leak.

So I ran it again but I CTRL + C exited out and after roughly 8 runs I began to see the memory increase.

So it appears that when exiting scripts being called by other scripts via CTRL + C it's making the memory get snagged.

hvacengi commented 7 years ago

That's an interesting detail. I'll take a look at how we handle clearing memory when leaving the program context. We must be failing to clear variables some how.

ElWanderer commented 7 years ago

I ran into enormous memory usage the other day, but assumed it was KSP leaking. On reading this thread I wondered if it is actually kOS. The circumstances are different (I do use a few lexicons and lists, but nothing excessive), but it does involve a script being stopped in an unusual way.

The script launches a rocket into orbit then makes a quicksave. It then performs a few manoeuvres to deorbit, lands and (if the craft has fallen off a cliff and exploded) finishes by reloading the quicksave to try again with a different set of manoeuvres (increasing the apoapsis in steps). After a few hours of testing this way, I noticed KSP was taking up about 10GB of memory (compared to my usual 2-3GB). My script does about 20 runs, but on that occasion I think I had tested it two times over, plus a partial run. If a KUNIVERSE:QUICKLOAD() is triggered, does kOS get the chance to clean up after itself?

Dunbaratu commented 7 years ago

@aidygus - I've been trying to recreate this problem tonight and I seem to be unable to do it with your lexicontest script. I am calling it from another script in a loop, as you said you were doing, and whether I let it die nicely, or whether I control-C, and no matter how many times I run it, I can't seem to get it to cause a memory leak. I do a debugdump() to see if anything was leftover on the stack (which is what I assumed would be the cause) and it's not leaving junk behind (either with control-C or with normal exit).

My task manager does not show the memory footprint of Kerbal Space Program getting larger as you describe.

I'm having a very hard time making this problem happen in front of my eyes so I'm at wit's end trying to figure out how to re-create it.

I tried with the public release of kOS just to make sure it wasn't something we'd fixed in the meantime. There seems to be nothing I can do that will make the problem happen.

Dunbaratu commented 7 years ago

I'll leave this open for now so people can still comment on it and try it again after we get our next release out, but I have been unable to reproduce the problem when I test it out, so I don't know how to fix it.

Dunbaratu commented 7 years ago

The recent release kOS v1.1.3.0 contains the fix in pull request #2109, which might be the problem that caused this issue originally. @aidygus can you test this again and see if it still happens with the newest release we just made last night? I'd like to close down this issue, but first I need to be 100% certain we fixed it. Since we never could reproduce it exactly it's hard to tell.

ddejohn commented 5 years ago

@aidygus what IPU is your script running at? I've recently encountered similar behavior with a function of mine that temporarily set the IPU to max, then timewarped and performed some relatively expensive function calls which involved (albeit small) lexicons containing delegates. The combination of max IPU and timewarping was causing my game (which normally runs around 90+ fps) to crawl to ~10 fps. I'm not sure if the lexicons actually have anything to do with it.

This is obviously anecdotal, as I am not versed in under-the-hood stuff. I just wanted to mention it in case it helps.