Jump to content

Memory cleaner/leak plugger


Major999

Recommended Posts

First of all, I have virtually no experience with coding so I don't know if this is possible.

What I'm asking for is a mod that forcefully cleans the memory that KSP is using, to alleviate issues with RAM leaks. I know that there are apps that can do that for a whole computer, (FreeRamBooster on a mac for example) but I am unaware of a way to do this within an app.

So essentially I'm asking for a big red button that will collect all of the memory that has leaked into no-man's-land and make it useable again.

To be clear I don't want to reduce the initial RAM use of my KSP, merely reduce it incrementally while I am playing, to stop the RAM use from creeping up to that critical limit.

Any help would be great.

Link to comment
Share on other sites

Memory is occupied by objects and references to objects; but predominantly objects. An object is mostly anything that gets stored, so like, variables and whatnot. In order to free up memory you must destroy objects.

This means you must have a reference to an object and be permitted to cause it's destruction, but furthermore you must know that it is safe to be destroyed.

While you may be able to make a mod that reaches into KSP and deals with leaked objects (objects which have completed their lifespan tasks but have not been deleted because they were forgotten), and there is currently a situation where a lot of objects are being leaked quickly so that'd be kinda nice; as far as mods are concerned the mod itself should be fixed.

This is not something that can be done generically, it will need to be a custom coded solution for every leak.

Link to comment
Share on other sites

By the very nature of how an operating system works, you cannot simply "take" memory which an app requested... at the most you can nicely ASK it to give back memory. So, the application needs to support it... and even then, the application usually can only do this, for memory which it doesnt strictly need.

A memory leak usually occurs, when an application continiously requests new memory, while forgetting to "free" the memory it previously used. This might be difficult to understand to non-coders, so here's a very quick explanation of what "memory management" means: Say an application wants to read a textfile... do something with it... then read another textfile and again do something with it. Furthermore, for simplicity lets assume both textfiles are of the same size. Now, let's go through this step by step: The app requests memory, so that it can store the red textfile in it. Then it does its processing. What happens next?

It is about to read another textfile, so does it give back the memory for the previous textfile? After all, why give it back, only to then again request new memory again for the next textfile? Answer: For this to work, the app would have to "manage" its memory itself. That might be trivial in this example, but for anything more complex, it is a giant headache to keep track of everything. So, what instead happens is the application indeed gives back the memory, only to then right away ask for new memory again.

But what if said app forgets the step of returning memory? That's called a memory leak. Everything i thus far explained is how this works in C and other lowlevel langs. In languages like .NET, those kinds of memleaks do not happen at all - thats because the step of giving back memory, once something is no longer used, is done automatically... the coder doesn't even have to worry about it.

Or does he? It turns out, something very similiar can still happen - by forgetting to mark data as no longer needed. See, this automatic step of giving back mem, relies on this "garbage collector" knowing when something is no longer needed - after all, while data is still needed, you can't just give that memory back - the app would crash at best. Memleaks in HL langs like .NET happen when something is actually no longer needed, but said "garbage collector" has no way of knowing that's the case.

Now, if the application's own GC doesn't even know, how is the OS supposed to know? It doesn't. All it can do is terminate the app, then reclaim everything that was marked as the reserved for that app.

What those "RAM boosters" and similiar apps do, have nothing to do with memory leaks. Usually, what they do is to empty/reduce caches (which after all aren't strictly needed), or to swap data out into the pagefile.

Link to comment
Share on other sites

  • 2 weeks later...

The memory leak seems to have little to do with retained data about objects in play. I have always noticed with KSP, even with stock, that when I launch a ship that memory use goes up and even when I revert to the assembly building this memory is not freed up. When you do this a few more times the extra memory used can creep up to 200mb or more - play for a few hours and it can be close to half a gigabyte of ram that is 'stolen'. Frequently this leads to a crash when I have several mods in use. Save the game and restart and the game runs fine for a while until this happens again.

Link to comment
Share on other sites

The memory leak seems to have little to do with retained data about objects in play. I have always noticed with KSP, even with stock, that when I launch a ship that memory use goes up and even when I revert to the assembly building this memory is not freed up. When you do this a few more times the extra memory used can creep up to 200mb or more - play for a few hours and it can be close to half a gigabyte of ram that is 'stolen'. Frequently this leads to a crash when I have several mods in use. Save the game and restart and the game runs fine for a while until this happens again.

That's exactly what retained objects is. In scene change KSP creates a bunch of objects and then leaves them there taking up space when they're no longer needed; and because it does this every scene change, the more times you change scenes the faster you will crash. As well KSP creats a hell of a lot of objects that do get disposed of correctly during scene change, which is why memory spikes during scene change and then comes down to a bit more than it used to be, which makes it even easier for KSP to crash.

Link to comment
Share on other sites

Given enough system RAM, you can run two instances of KSP simultaneously and have one sitting "on stand-by". When KSP crashes you quickly switch to the second one and load the savegame, without wasting time on waiting for the application to launch again. Once the game loads, you fire-up another KSP and let it boot up in the background. I've been doing that for a week or two and had no issues. Just make sure you leave your "stand-by" KSP idle on the menu screen, else you risk borking your savegames. You can also try lowering the priority of the process to conserve CPU time or suspending it entirely (google pssuspend.exe from sysinternals/microsoft) but Windows may spam you with "Program is not responding" messages.

Remember though that you need a computer with a memory capable of handling two instances of KSP. My heavily modded KSP loads at 2.8 Gig working set and the crash occurs at 3.7 Gig. Thus, 3.7 + 2.8 + the rest for OS stuff...

Edited by Zorbaq
Link to comment
Share on other sites

Given enough system RAM, you can run two instances of KSP simultaneously and have one sitting "on stand-by". When KSP crashes you quickly switch to the second one and load the savegame, without wasting time on waiting for the application to launch again. Once the game loads, you fire-up another KSP and let it boot up in the background. I've been doing that for a week or two and had no issues. Just make sure you leave your "stand-by" KSP idle on the menu screen, else you risk borking your savegames. You can also try lowering the priority of the process to conserve CPU time or suspending it entirely (google pssuspend.exe from sysinternals/microsoft) but Windows may spam you with "Program is not responding" messages.

Remember though that you need a computer with a memory capable of handling two instances of KSP. My heavily modded KSP loads at 2.8 Gig working set and the crash occurs at 3.7 Gig. Thus, 3.7 + 2.8 + the rest for OS stuff...

most useful post, thank you for that its a great idea, my ksp loads up to 3 gig (and that is not even with all the mods i want to try out!) so i have pretty much around half an hour at max before i have to kick up another instance, but i never thought about starting it twice :D hope one time they get this sorted out, or better said unity engine gets it sorted -.-

Link to comment
Share on other sites

If you play on windows, I suggest you use OpenGL with KSP and 32 Bit and use the ActiveTextureManagement Mod. You can play pretty much uninterrupted for hours then. I crashed once in the last week, using 65 Mods. Also the ActiveTextureManagement Mod helps in a variety of ways not only by converting textures in a format that uses less memory footprint but also is most compatible with Unity.

Link to comment
Share on other sites

Fair warning: OpenGL doesn't work well for everyone, and it's not supported since it's a Unity feature.

You'll need to run KSP using two command line flags: -force-opengl and -nopopup. The second prevents KSP from using true fullscreen, preventing an OpenGL-specific related to the current version of Unity.

Link to comment
Share on other sites

thanks, i will try this out!

hmmm, already got it working, instead of being around 3gb of ram usage i have now only 1.4gb, lets see if it stays stable. would be soo cool <3

funny sideeffect of this: i finally learned how to deactivate that windows attach at the top of the screen so the titlebar is always visible. but no more, finally! :D

Edited by Armarnis
Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...