Jump to content

[KSP 1.7.2 incl all DLC's] KSP Memory Allocation Fix


Recommended Posts

6 hours ago, Jognt said:

Lazy question: Any plans to turn this into a KSP plugin?

I'm too lazy to go install AHK again

 

I did make a functional writeup in  C#,and tried to use TAC's plugin template to build it as a plugin,but i couldn't get VisualStudio working with KSP, but here's the C# code needed if anyone can build it,... with appropriate modifications obviously, as it's meant to run indefinitely every N seconds.

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

public class MemoryManagement
{
	[System.Runtime.InteropServices.DllImport("kernel32.dll")]
	private static extern int SetProcessWorkingSetSize(IntPtr process, int minimumWorkingSetSize, int maximumWorkingSetSize);
	
	public static void Main()
	{
		System.Threading.Thread.Sleep(5000);
		try {
			GC.Collect();
			GC.WaitForPendingFinalizers();
			if ((Environment.OSVersion.Platform == PlatformID.Win32NT)) {
				SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle, -1, -1);
				Console.Beep();
				System.Threading.Thread.Sleep(5000);
			}
		} catch (Exception ex) {
			//Console.WriteLine(ex.ToString);
		}
	}
}

 

 

 

 

Edited by 0x00
Link to comment
Share on other sites

Simple question:

How is this intended to be used?
Together with MemGraph / HeapPadder?
Without it?

Or ask in a different way:

How does this affect garbage collection and stuttering?

 

For the time being, I created this small Windows command batch - it assumes that the files of KSP MemoryAllocationFix and this batch itself are in the KSP root folder:

KSP x64 with KSP MemoryAllocationFix.cmd

@echo off
cd /d "%~dp0"
cls
start "KSP x64 with KSP MemoryAllocationFix" /NORMAL "KSP MemoryAllocationFix.exe"
KSP_x64.exe -force-glcore
taskkill /IM "KSP MemoryAllocationFix.exe"

As you can see, I launch the game with -force-glcore parameter, change to your liking.

But you even can make the "KSP MemoryAllocationFix.exe" start with Windows and let it run all the time :sticktongue:

Edited by Gordon Dry
Link to comment
Share on other sites

Feedback:

I used it without MemGraph / HeapPadder.

After splashdown I press ESC and wait - and wait ... there is no UI incoming.

Game responses fast, I can ALT-TAB quickly, back in, pressing ESC - nothing. Hmm.

 

Edit:

ALT-F12 - nothing.

Spamming stuff like this now:

IndexOutOfRangeException: Array index is out of range.
  at System.Collections.Generic.Stack`1[System.Collections.Generic.List`1[UnityEngine.Canvas]].Pop () [0x00000] in <filename unknown>:0 
  at UnityEngine.UI.ObjectPool`1[System.Collections.Generic.List`1[UnityEngine.Canvas]].Get () [0x00000] in <filename unknown>:0 
  at UnityEngine.UI.ListPool`1[UnityEngine.Canvas].Get () [0x00000] in <filename unknown>:0 
  at UnityEngine.UI.Graphic.CacheCanvas () [0x00000] in <filename unknown>:0 
  at UnityEngine.UI.Graphic.get_canvas () [0x00000] in <filename unknown>:0 
  at UnityEngine.UI.Graphic.GetPixelAdjustedRect () [0x00000] in <filename unknown>:0 
  at UnityEngine.UI.Image.GenerateSlicedSprite (UnityEngine.UI.VertexHelper toFill) [0x00000] in <filename unknown>:0 
  at UnityEngine.UI.Image.OnPopulateMesh (UnityEngine.UI.VertexHelper toFill) [0x00000] in <filename unknown>:0 
  at UnityEngine.UI.Graphic.DoMeshGeneration () [0x00000] in <filename unknown>:0 
  at UnityEngine.UI.Graphic.UpdateGeometry () [0x00000] in <filename unknown>:0 
  at UnityEngine.UI.Graphic.Rebuild (CanvasUpdate update) [0x00000] in <filename unknown>:0 
  at UnityEngine.UI.CanvasUpdateRegistry.PerformUpdate () [0x00000] in <filename unknown>:0 
UnityEngine.DebugLogHandler:Internal_LogException(Exception, Object)
UnityEngine.DebugLogHandler:LogException(Exception, Object)
UnityEngine.Logger:LogException(Exception, Object)
UnityEngine.Debug:LogException(Exception, Object)
UnityEngine.UI.CanvasUpdateRegistry:PerformUpdate()
UnityEngine.Canvas:SendWillRenderCanvases()
 
(Filename:  Line: -1)

IndexOutOfRangeException: Array index is out of range.
  at System.Collections.Generic.Stack`1[System.Collections.Generic.List`1[UnityEngine.Canvas]].Pop () [0x00000] in <filename unknown>:0 
  at UnityEngine.UI.ObjectPool`1[System.Collections.Generic.List`1[UnityEngine.Canvas]].Get () [0x00000] in <filename unknown>:0 
  at UnityEngine.UI.ListPool`1[UnityEngine.Canvas].Get () [0x00000] in <filename unknown>:0 
  at UnityEngine.UI.Graphic.CacheCanvas () [0x00000] in <filename unknown>:0 
  at UnityEngine.UI.Graphic.get_canvas () [0x00000] in <filename unknown>:0 
  at UnityEngine.UI.Graphic.OnDisable () [0x00000] in <filename unknown>:0 
  at UnityEngine.UI.MaskableGraphic.OnDisable () [0x00000] in <filename unknown>:0 
UnityEngine.GameObject:SetActive(Boolean)
PopupDialog:Dismiss(Boolean)
PopupDialog:Dismiss()
PauseMenu:Close()
PauseMenu:Update()
 
(Filename:  Line: -1)

Full log:
https://www.dropbox.com/s/zka8yc4qzr2boqz/output_log.txt and stuff 2019-07-26-01.zip?dl=1

Edited by Gordon Dry
Link to comment
Share on other sites

13 hours ago, Gordon Dry said:

Simple question:

How is this intended to be used?
Together with MemGraph / HeapPadder?
Without it?

Or ask in a different way:

How does this affect garbage collection and stuttering?

 

Scroll up a bit, it’s been explained. Should not be incompatible with each other. 

6 hours ago, Gordon Dry said:

Feedback:

I used it without MemGraph / HeapPadder.

After splashdown I press ESC and wait - and wait ... there is no UI incoming.

Game responses fast, I can ALT-TAB quickly, back in, pressing ESC - nothing. Hmm.

 

Edit:

ALT-F12 - nothing.

Spamming stuff like this now:

Full log:
https://www.dropbox.com/s/zka8yc4qzr2boqz/output_log.txt and stuff 2019-07-26-01.zip?dl=1

Try it with a clean install without launch Params?

Edited by Jognt
Link to comment
Share on other sites

13 hours ago, Gordon Dry said:

Feedback:

I used it without MemGraph / HeapPadder.

After splashdown I press ESC and wait - and wait ... there is no UI incoming.

Game responses fast, I can ALT-TAB quickly, back in, pressing ESC - nothing. Hmm.

Cause of that error is most likely something else,an actual mod running within ksp. So perhaps attempt to run the exact modset in which you encountered this error with/without the script & let me know if in indeed the error occurs with the script but not without given the same modset.

And if the error is indeed only present with the script running,please post the list of mods you're running.

20 hours ago, Gordon Dry said:

Simple question:

How is this intended to be used?
Together with MemGraph / HeapPadder?
Without it?

Or ask in a different way:

How does this affect garbage collection and stuttering?

 

As others have alluded, there should be no conflict,although i don't recommend manually padding heaps(as opposed to simply having the mod running) while using the script, as they have entirely different purposes. Heap padder abstractly speaking increases ksp's memory pool to reduce garbage collection frequency,requiring more available memory, this script unallocates unused memory to windows & other applications for use when there's less available memory, GC shouldn't increase as a result of this,i.e no stuttering,only slow down incurred is during startup, scene switching(VAB to SHP & so on) and alt tabbing(if tabbed out for long period of time).

 

 

Edited by 0x00
Link to comment
Share on other sites

6 hours ago, 0x00 said:

let me know if in indeed the error occurs with the script but not without given the same modset

As I added KSP MemoryAllocationFix.exe in middle of a running game save I can tell you that this specific issue does not happen without it.
I would not have posted about it here then.

 

6 hours ago, 0x00 said:

please post the list of mods you're running

Spoiler

Non-DLL mods added (:FOR[xxx]):
  zzzz_PorkjetStock
  zzzz_PorkjetStock2
  Ablative-Airbrake
  aaa_AJE
  zzzzzz-B9PartSwitch
  CoatlAerospace
  EngineGroupContoller
  EngineLight
  KerbalFoundries
  zzzKerbalism
  RP-0-Kerbalism
  xxxRP1
  RP-0
  MandatoryRCSPartPack
  zzzzNFConstruction
  ProceduralFairings-FE
  zPFFE
  zzzProceduralFairingsRetext
  RealActiveRadiators
  zzzRealFuels
  aaa
  zRealFuelsVolumes
  RealismOverhaul,DMagic
  zRORescaleDragCubes
  zzzRealismOverhaul
  ROSolar
  zzzElectricityUnits
  zzzTagCleanup
  ROCOmmsCleanup
  zzRO-RemoveSpec
  RealismOverhaulEngines
  RealismOverhaulEnginesPost
  RealPlume
  zzRealPlume
  zaPFRemass
  RO-RCS
  RemoteTech-Antennas
  RealismOverhaulRT
  RealismOverhaulRC
  RealismOverhaulTACLS
  zzzAfterUniversalStorage
  RealISRU
  zRealPlume
  zzzzStationParts
  zzReStockPlus
  ReStockPlus
  3Skylab
  1Skylab
  4Skylab
  2Skylab
  ROCapsules
  zzRocketSoundEnhancement
  zzzzzROEngines
  zzPostRealPlumeROEngines
  zzzRP-0
  xxxRP0
  xxxRP-0
  zzzzzzzzzzzzzzzzzzzzzzzRP0
  ProceduralAvionics
  ZZZZZ-RP0Tree
  RSSExpansion
  zTestFlight
  ZZZZ_GD_LS_MFTRF_0
  ZZZZ_GD_LS_MFTRF_1
  ZZZZ_GD_LS_MFTRF_2
  ZZZZ_GD_LS_MFTRF_3
  ZZZZ_GD_LS_MFTRF_4
  ZZZZ_GD_LS_MFTRF_5
  ZZZZ_GD_LS_MFTRF_6
  ZZZZ_GD_LS_MFTRF_7
  ZZZZ_GD_LS_MFTRF_8
  ZZZZ_GD_LS_MFTRF_9
  zzzzzz_RemoteTech
Mods by directory (sub directories of GameData):
  000_ClickThroughBlocker
  000_FilterExtensions
  000_FilterExtensions_Configs
  000_TexturesUnlimited
  000_Toolbar
  000_USITools
  001_ToolbarControl
  002_CommunityPartsTitles
  AECS_Motion_Suppressor
  ATK_Propulsion_Pack
  AudioMufflerRedux
  B9_Aerospace_ProceduralWings
  B9_InfoDrive
  BahaConstellation
  BahaSP
  BasicProceduralTextures
  BLO
  Bluedog_DB_ullage_SRB
  CommNetAntennasExtension
  CommunityCategoryKit
  CommunityRealAgencyPack
  CommunityResourcePack
  CommunityTechTree
  ContractPacks
  CTTP
  CxAerospace
  DMagicOrbitalScience
  DMagicScienceAnimate
  DMagicUtilities
  EnvironmentalVisualEnhancements
  FASA
  FlareReplacer
  HeatControl
  InternalRCS
  JX2Antenna
  KerbalChangeLog1.1.4
  KerbalismConfig
  KerbalRenamer
  KerbNetController
  KerboKatz
  KermangeddonIndustries
  KspCraftOrganizer
  Lionhead_Aerospace_Inc
  ManeuverNodeEvolved
  MechJeb2Embedded
  MoarFilterExtensionConfigs
  NAU
  NavyFish
  NearFutureConstruction
  Nereid
  Pebkac
  ProceduralFairings-ForEverything
  ProceduralFairingsRetext
  RemoteTechRedev
  RN_Misc
  RN_Skylab
  RN_Solar_Panels
  RN_Soviet_Probes
  RN_US_Probes
  RocketSoundEnhancement
  RSS-Textures
  RSSDateTime
  RSSExpansionTextures
  SensiblePumpsCont
  SituationModuleInfo
  Squad
  SquidsoftCollective
  STMRibbons
  Taerobee
  TokamakIndustries
  TrackingStationEvolved
  TriggerTech
  UmbraSpaceIndustries
  VenStockRevamp
  WrobzTextures
  XyphosAerospace
  zFinal
Mods added by assemblies:
  ProfileRealismOverhaul (added by Kerbalism)
  FeatureDeploy (added by Kerbalism)
  FeatureSpaceWeather (added by Kerbalism)
  FeatureAutomation (added by Kerbalism)
  FeatureScience (added by Kerbalism)
  FeatureRadiation (added by Kerbalism)
  FeatureShielding (added by Kerbalism)
  FeatureLivingSpace (added by Kerbalism)
  FeatureComfort (added by Kerbalism)
  FeaturePoisoning (added by Kerbalism)
  FeaturePressure (added by Kerbalism)
  FeatureHumidity (added by Kerbalism)
  FeatureHabitat (added by Kerbalism)
  FeatureSupplies (added by Kerbalism)

 

^this includes HeapPadder - I did not use it with the MemoryAllocationFix together.

That was the last session, I have removed FAR just before that.
For being sure I can launch KSP with MemoryAllocationFix again ... (and without HeapPadder)

Edited by Gordon Dry
Link to comment
Share on other sites

@Gordon Dry I think the cause is what @Jognt suggested, your parameters, -force-glcore , I've had quite a few problems with it & so don't use it,it just crashes on me when i tried to test it so i can't exactly reproduce what you stated,so could you possibly try -force-opengl  instead, OR without any parameters if that too is problematic. Cheers

Edited by 0x00
Link to comment
Share on other sites

2 minutes ago, 0x00 said:

@Gordon Dry I think the cause is what @Jognt suggested, your parameters, -force-glcore , I've had quite a few problems with it & so don't use it,it just crashes on me when i tried to test it so i can't exactly reproduce what you stated,so could you possibly try -force-opengl  instead, OR without any parameters if that too is problematic. Cheers

Plenty of stuff (like the game itself) is rated to run on default settings. Anything custom means “you’re your own support”. 

If the problem occurs on a stock fresh install however, that’s a different story. 

Link to comment
Share on other sites

  • 3 weeks later...

Wew.. I went from hitting 15GB used RAM and having to restart KSP (and removing IVAs) to using less than 10GB with IVAs!

There's a slight stutter when it does its thing though, so I set the interval to 90 seconds to see what happens.

"The best thing since sliced bread!" - Jeb
"I can't believe it's not stock!" - Wernher

Edit: Observations: It does cause quite a bit of stutter when changing scenes after being elsewhere for a while. But by the time it gets really annoying it settles back down so that's ok. I haven't encountered any "ESC not working" problems on Dx11 so far, and the RAM savings are very nice!

One note though, after quitting KSP I noticed (once) I had firefox still running, and FF was slow as HECK. I'm guessing it got removed as well or something, because I had to restart it to get it to behave again.

Edited by Jognt
Link to comment
Share on other sites

On 8/11/2019 at 11:27 PM, Jognt said:

Edit: Observations: It does cause quite a bit of stutter when changing scenes after being elsewhere for a while. But by the time it gets really annoying it settles back down so that's ok. I haven't encountered any "ESC not working" problems on Dx11 so far, and the RAM savings are very nice!

One note though, after quitting KSP I noticed (once) I had firefox still running, and FF was slow as HECK. I'm guessing it got removed as well or something, because I had to restart it to get it to behave again.

I'll take a look into that,it might be because when the script starts or when ever you change interval it clears entire system memory pool, to increase overall available memory for KSP, I'll either find a better way to do that or may be just make it a hotkey for user to manually activate if/when a user chooses to do so.

I'm not home right now, but I'll post an update when i get back in a couple of days, thanks for the feedback.

Edited by 0x00
Link to comment
Share on other sites

5 minutes ago, 0x00 said:

I'll take a look into that,it might be because when the script starts or when ever you change interval it clears entire system memory pool, to increase overall available memory for KSP, I'll either find a better way to do that or may be just make it a hotkey for user to manually activate if/when a user chooses to do so.

I'm not home right now, but I'll post an update when i get back in a couple of days, thanks for the feedback.

You’re welcome. Thanks for the script and .exe!

Link to comment
Share on other sites

  • 1 month later...

Would there be a way to set it to trigger at a given RAM value? Or does that risk causing all kinds of loops from hell because of how allocation can vary between systems?

EDIT-

I just realized you could actually fetch the amount of allocated RAM the system allocated at the beginning and store it and then use a block of logic to trigger it once the allocated amount exceeded whatever i wanted to set. I just don't know how to do the first part.

Example

RAMfunction() // This is what i don't know how to do; especially in a OS agnostic way.

If (Initial_Allocation >=  Threshold + Inital_Allocation)

{

//run your tool

}

And would this even make sense from a performance standpoint?

Edited by Incarnation of Chaos
Forgot basic programming
Link to comment
Share on other sites

Updated OP, ksp working set is now cleared only when system memory usage exceeds a specified maxium in percent,by default when 70% system memory usage is exceeded ksp's working set will be continually cleared every N seconds as defined in config,default (30), until memory usage falls below set limits, this allows KSP to remain more responsive when under the limit, as opposed to before where it was indefinitely  cleared.

 

10 hours ago, Incarnation of Chaos said:

Would there be a way to set it to trigger at a given RAM value? Or does that risk causing all kinds of loops from hell because of how allocation can vary between systems?

I was actually just about to update it to function as you described,well sort of... I was testing i over the past couple of days,but you asked,so here you go...

Edited by 0x00
Link to comment
Share on other sites

7 hours ago, 0x00 said:

Updated OP, ksp working set is now cleared only when system memory usage exceeds a specified maxium in percent,by default when 70% system memory usage is exceeded ksp's working set will be continually cleared every N seconds as defined in config,default (30), until memory usage falls below set limits, this allows KSP to remain more responsive when under the limit, as opposed to before where it was indefinitely  cleared.

 

I was actually just about to update it to function as you described,well sort of... I was testing i over the past couple of days,but you asked,so here you go...

Oh the irony xD

Link to comment
Share on other sites

  • 1 month later...
On 10/20/2019 at 12:45 PM, blackk100 said:

Any idea how the engine update affects memory usage/leakage?

Performance is generally better, but memory usage isn't drastically reduced & despite updates to garbage collection, the longer you run it, memory usage still tends to inflate, as long as the game loads pretty much every texture in memory at start i don't see how performance, specifically memory usage could be reduced. For comparison my Arma 3 installation has about 200GB of mods ontop of the 68GB game(all dlc's included) on a dedicated 500GB SSD & it hardly ever uses more than 12GB of RAM(On ULTRA), so ksp's approach to loading textures is the core reason for this problem & i don't see how any update will resolve the matter so long as how it loads textures isn't revised, our best hope i think is with ksp2,but that too uses unity,so who knows, unity is notorious for bad memory management, city skylines is another brilliant example of that fact. Cheers.

Link to comment
Share on other sites

  • 1 month later...
  • 9 months later...
  • 3 months later...
  • 3 months later...
  • 1 month later...
  • 4 weeks later...

Not sure what voodoo this mod does but it works amazingly well. With a heavily modded install it keeps my CPU usage low and runs the game extremely smooth. My fan barely runs, compared to the times I forget to activate this mod and my fans are going constantly. Highly recommended.

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...