• 0

Reduce .NET app RAM usage


Question

Recommended Posts

  • 0

You should leave the .NET runtime do the job. It will reduce the working sets automatically when memory runs out globally. I don't know why everyone is so obsessed with the memory column on taskman or perfmon with .NET apps. That counter is not representative for the .NET applications memory usage.

Other than that it will require you to import a function call from ntdll.dll or something. Winston and bangbang23 can tell you exactly how.

  • 0

Ahh gees let me reiterate myself for the 500000000 time, but it's alright, you see the .NET Framework, does the memory allocation, what the framework does is, when your system has alot of memory free, say for example you have 512 megs of RAM and use up only 100 then your app with just one form with nothing probably uses about 25 megs, this is primarily because the more RAM you have the more .NET apps reserves for itself, i duno the real reason why it does this it was so confusing listening to the architect guy talk in germanglish lol... but yeah, so say for example if your system had 128 megs of ram and you got 28 megs spare, then your .net app will only make out about 300k - 800k, this is the single form that has nothing on it.

Let's just say what you see in the task manager is probably deceiving, theoretically the application just runs at a very small amount of memory but what's shown for the memory is how much the application has allocated for itself, as soon as the system runs low on memory the .net app will decrease in memory usage, some computer illiterate people might look at the task manager and sh*t their pants seeing your app using so much, so below is the fix which will practically just refresh the task manager so that it displays the memory that's REALLY in use and remove the memory that's spare

so you can come to a conclusion to say what's shown in task manager is basically the representation of:

memory actually being used + additional memory reserved for the program = task manager display

applying of the following function:

? ?
Private Declare Function SetProcessWorkingSetSize Lib "kernel32.dll" (ByVal hProcess As IntPtr, ByVal dwMinimumWorkingSetSize As Int32, ByVal dwMaximumWorkingSetSize As Int32) As Int32
 ? ?'Call the Kernel API Set process memory size

Public Function SaveMemory() As Int32

 ? ? ? ?Return SetProcessWorkingSetSize(Diagnostics.Process.GetCurrentProcess.Handle, -1, -1)

 ? ?End Function

'Call the function in the appropriate events, i.e. form Activated, Form Deactivated, which a friend of mine recommeneded me doing

SaveMemory()[b]displays only memory actually consumed by app[/b]by app

I hope this has cleared it up a bit, and could some Mod just sticky this, it's been asked alot!

Cheers

  • 0

Note, however, that there are side effects to using SetProcessWorkingSetSize. If you call that API regularly and the Framework needs to reduce the working size of the app for some reason, the app will throw an OutOfMemoryException, whether the system is actually out of memory or not.

And, as Winston said, that API is just a convenient illusion for people with a gigabyte of RAM who still feel they need to guard every byte jealously. It doesn't actually accomplish anything.

--

Danny Smurf

  • 0

i've also found that, while most of my apps run at 20-25Mb while in use, if i let them sit there long enough and watch the task manager, they'll go down as low as about 500kb until i start using them again.

Really makes it look like some impressive memory management :)

  • 0
  nowimnothing said:
i've also found that, while most of my apps run at 20-25Mb while in use, if i let them sit there long enough and watch the task manager, they'll go down as low as about 500kb until i start using them again.

Really makes it look like some impressive memory management :)

trust me, release the app and you'll get alot of user complaining. It didn't bother me, I had my app at 15mb mem usage, but people kept complaining so I had to use the trick above.

  • 0
  session? said:
Private Declare Function SetProcessWorkingSetSize Lib "kernel32.dll" (ByVal hProcess As IntPtr, ByVal dwMinimumWorkingSetSize As Int32, ByVal dwMaximumWorkingSetSize As Int32) As Int32
  weenur said:
[DllImport("kernel32.dll")]

public extern static bool SetWorkingProcessSetSize( IntPtr hProcess, int min, int max );

Returns Int32, not bool...

  • 0
  bangbang023 said:
trust me, release the app and you'll get alot of user complaining. It didn't bother me, I had my app at 15mb mem usage, but people kept complaining so I had to use the trick above.

If I'm ever going to release one of my apps into public, I will make sure that it has a dialog that shows up on first run that explains them CLEARLY how .NETs memory management works, including checkbox to agree not to complain! Better way than ****ing up an applications performance and stability.

  • 0
  frogg said:
  weenur said:
[DllImport("kernel32.dll")]

public extern static bool SetWorkingProcessSetSize( IntPtr hProcess, int min, int max );

Returns Int32, not bool...

the C declaration of SetProcessWorkingSetSize:

BOOL SetProcessWorkingSetSize( HANDLE hProcess, SIZE_T dwMinimumWorkingSetSize, SIZE_T dwMaximumWorkingSetSize)

You can use bool for BOOL in interop.

I inadvertently swapped Process with Working. That I will admit to being incorrect on. :D

  • 0
  bangbang023 said:
  nowimnothing said:
i've also found that, while most of my apps run at 20-25Mb while in use, if i let them sit there long enough and watch the task manager, they'll go down as low as about 500kb until i start using them again.

Really makes it look like some impressive memory management  :)

trust me, release the app and you'll get alot of user complaining. It didn't bother me, I had my app at 15mb mem usage, but people kept complaining so I had to use the trick above.

I have released the app (its not public stuff, its for internal stuff in my company) and noone cares - i don't think they'd ever notice.

If people are just going on whats showing up in task manager then, well, a) they don't know what they're talking about, and b) why are they staring at task manager anyway?

  • 0
  nowimnothing said:
  bangbang023 said:
  nowimnothing said:
i've also found that, while most of my apps run at 20-25Mb while in use, if i let them sit there long enough and watch the task manager, they'll go down as low as about 500kb until i start using them again.

Really makes it look like some impressive memory management  :)

trust me, release the app and you'll get alot of user complaining. It didn't bother me, I had my app at 15mb mem usage, but people kept complaining so I had to use the trick above.

I have released the app (its not public stuff, its for internal stuff in my company) and noone cares - i don't think they'd ever notice.

If people are just going on whats showing up in task manager then, well, a) they don't know what they're talking about, and b) why are they staring at task manager anyway?

I'm not saying those people are right, but I have had users complain because "it's a sticky note program and shouldn't be using 15mb of RAM." I basically had to do it or be faced with losing users. I don't make money off my app, but I still don't want to lose users.

Tom: First off, it's nice having a somewhat normal, non-fighting discussion lol. Second, I have not had any problems with stability or performance with my app.

  • 0
  bangbang023 said:
  nowimnothing said:
  bangbang023 said:
  nowimnothing said:
i've also found that, while most of my apps run at 20-25Mb while in use, if i let them sit there long enough and watch the task manager, they'll go down as low as about 500kb until i start using them again.

Really makes it look like some impressive memory management?:))

trust me, release the app and you'll get alot of user complaining. It didn't bother me, I had my app at 15mb mem usage, but people kept complaining so I had to use the trick above.

I have released the app (its not public stuff, its for internal stuff in my company) and noone cares - i don't think they'd ever notice.

If people are just going on whats showing up in task manager then, well, a) they don't know what they're talking about, and b) why are they staring at task manager anyway?

I'm not saying those people are right, but I have had users complain because "it's a sticky note program and shouldn't be using 15mb of RAM." I basically had to do it or be faced with losing users. I don't make money off my app, but I still don't want to lose users.

Tom: First off, it's nice having a somewhat normal, non-fighting discussion lol. Second, I have not had any problems with stability or performance with my app.

You'll understand that it may have adverse effects which is unpredictable at times, once again a friend of mine taught me programming, and he has brought me up the right way in thinking, and bangbang i hope you begin to take account for your applications performance, not criticising you much but like i always stressed to u on MSN you shouldnt care about if the thing can be implemented, it should be done in a way that it wont drastically effect performance on ur application, which i did back then wen i designed my app because i had a time constraint as it was a assignment and i needed to get it over and done with, but once i finish my exams im gonna learn .net from the ground up.

  • 0
  Winston said:
You'll understand that it may have adverse effects which is unpredictable at times, once again a friend of mine taught me programming, and he has brought me up the right way in thinking, and bangbang i hope you begin to take account for your applications performance, not criticising you much but like i always stressed to u on MSN you shouldnt care about if the thing can be implemented, it should be done in a way that it wont drastically effect performance on ur application, which i did back then wen i designed my app because i had a time constraint as it was a assignment and i needed to get it over and done with, but once i finish my exams im gonna learn .net from the ground up.

I've told you a million times before, I get the basic code layed out then I worry about performce. It can't run well if it don't run at all. My code is definitely not the best, but neither is yours or anyone else's here, cause otherwise we'd be very rich, but my code is descent (lacking comments but not a problem for me.) Anything that works on my program works well and does not slow down the program. I wouldn't include a "feature" if it slowed down the proggy.

Either way, this is off topic. You don't like my code? So be it. I honestly think you want it done your way or no way at all, but there are plenty ways to accomplish something.

I'll way for Tom to reply so we can continue the discussion we were having.

  • 0

Back to SetProcessWorkingSetSize...

As far as losing users, I can understand where you're coming from, bangbang. I went through that dilemma myself. In some cases, it's appropriate to use the API and shut them up, and in some cases it's not.

For a non-trivial app, it's definitely not worth using. If you're creating (for example), a productivity app, the memory footprint isn't so important. A word processor, email client, InfoPath-type app or whatever... people really aren't going to care if it takes 50MB. On the other hand, for a trivial app (a sticky note program, a registry startup watcher, that sort of power-tool type of stuff), people WILL care, because they've got the idea stuck in their head that minor apps should be minor RAM users. That's not the right attitude, of course, but when you're making these kinds of apps, your userbase is quite a bit different. You get every johnny-knowitall from all over the Internet critiquing your app.

If you're charging for your app (regardless of what type it is), it's definitely not a good idea to use that API. Side effects like those **** people off. And ****ing off paying customers is an entirely different thing from ****ing off freebie hunters.

--

Danny Smurf

  • 0
  bangbang023 said:
I've told you a million times before, I get the basic code layed out then I worry about performce. It can't run well if it don't run at all. My code is definitely not the best, but neither is yours or anyone else's here, cause otherwise we'd be very rich, but my code is descent (lacking comments but not a problem for me.) Anything that works on my program works well and does not slow down the program. I wouldn't include a "feature" if it slowed down the proggy.

I think you have a correct approach. Optimization is always something you do last, and judiciously. As Kestrel has correctly stated, as have many others, cpu cycles(and memory) are cheaper than man-hours.

@dannysmurf

I agree 100%. End users suck. ;)

  • 0
  dannysmurf said:
Back to SetProcessWorkingSetSize...

As far as losing users, I can understand where you're coming from, bangbang. I went through that dilemma myself. In some cases, it's appropriate to use the API and shut them up, and in some cases it's not.

For a non-trivial app, it's definitely not worth using. If you're creating (for example), a productivity app, the memory footprint isn't so important. A word processor, email client, InfoPath-type app or whatever... people really aren't going to care if it takes 50MB. On the other hand, for a trivial app (a sticky note program, a registry startup watcher, that sort of power-tool type of stuff), people WILL care, because they've got the idea stuck in their head that minor apps should be minor RAM users. That's not the right attitude, of course, but when you're making these kinds of apps, your userbase is quite a bit different. You get every johnny-knowitall from all over the Internet critiquing your app.

If you're charging for your app (regardless of what type it is), it's definitely not a good idea to use that API. Side effects like those **** people off. And ****ing off paying customers is an entirely different thing from ****ing off freebie hunters.

--

Danny Smurf

I agree with you fully. My app has, in my mind, the nicest set of features for a note program (maybe lacking a few areas but it's only on 0.52). Either way, if people were going to pay for this and I knew they wouldn't nitch and moan, I would remove the call to that API in a flash, but, being a free program, I had nothing else to offer except the low ram usage selling point. I can't afford benefits or anythign like full 24/7 tech support lol.

  • 0

Well, what about an option based on my dialog proposal?

Make a dialog that shows up on first run that explains the memory management **** in easy words and then two radio buttons that allow you to choose to use the "memory preserving" option (SetProcessWorkingSetSize) or not.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • How can all of these Neowin deals be "an all time low price". I find that pretty hard to believe.
    • Oh wow, a patch for an Unreal game to improve performance? We'll see...
    • Get this massive 4TB WD_BLACK SN7100 SSD at a new all-time low price by Taras Buria Xbox owners recently received a new storage upgrade option, which allows them to equip the Xbox Series X|S with 4TB of extra space. That card, however, has an eye-watering price tag—nearly as much as the 1TB Xbox Series S. On the PC side, though, things are much better. Right now, if you need a 4TB SSD without selling a kidney, you can get the WD_BLACK SN7100 PCIe Gen4 SSD. It is now available on Amazon at a new all-time low price after a 16% discount (nearly half the cost of the new 4TB Xbox Storage Expansion Card). The SN7100 is a fast, reliable, and, more importantly, affordable Gen4 solid-state drive. Its speeds are rated up to 7,000MB/s sequential read and 6,700MB/s sequential write, while random input-output speeds are rated at 900K IOPS read and 1,350K IOPS write. These specs are not record-breaking, but they are fast enough for modern gaming (DirectStorage is supported), fast loading times in games and apps, and quick file transfer. The WD_BLACK SN7100 has a limited five-year warranty and endurance rating up to 2,400 TBW. You can monitor the health of your drive in the WD_Black Dashboard app on Windows. As for compatibility, you can use the SN7100 in any PC that accommodates M.2 2280 PCIe Gen4 drives, including PlayStation 5. It is compatible with PCIe Gen3 systems, but the maximum speeds will be limited. 4TB WD_BLACK SN7100 PCIe Gen4 Solid-State Drive - $249.99 | 16% off on Amazon US This Amazon deal is US-specific and not available in other regions unless specified. If you don't like it or want to look at more options, check out the Amazon US deals page here. Get Prime (SNAP), Prime Video, Audible Plus or Kindle / Music Unlimited. Free for 30 days. As an Amazon Associate, we earn from qualifying purchases.
    • Snagit 2025.2.0 by Razvan Serea Snagit is the most complete screen capture utility available. Showing someone exactly what you see on your screen is sometimes the quickest and clearest way to communicate. With Snagit, you can select anything on your screen – an area, image, article, Web page, or error message – and capture it. Then, save the screen capture to a file, send it to Snagit​'s editor to add professional effects, share it by e-mail, or drop it into PowerPoint®, Word®, or another favorite application. Capture and share images, text or video from your PC. Create beautiful presentations, flawless documentation and quickly save online content. The latest version of Snagit offers a totally new interface and workflow - making SnagIt easier for beginners to use, while still providing maximum convenience and flexibility for the screen capture experts. Snagit 2025.2.0 changelog: Edit Images from Camtasia Snagit can now be used to seamlessly edit images from Camtasia. Requires Camtasia version 2025.2.0 or later. In the Camtasia Media Bin, right-click an image and select the Edit in Snagit option. In Snagit, make your edits. When finished, click Send in the "Send changes to Camtasia" notification to replace the image on your Camtasia timeline. Step Capture Improved the Step Capture template to accommodate longer auto-populated step text. Annotations such as Callout or Arrow tool objects now anchor to and move with sections as sections are added to, reordered, or deleted from templates. Subscription Software Updated the Account dropdown to open automatically when new subscription related messages are present. Performance Improvements Improved the startup time for Snagit Capture application. Updates for IT Administrators Updated BouncyCastle.Cryptography package to address CVE-2024-29857 and CVE-2024-30172. Removed dependency on Xceed Zip. Updated LeadTools DLLs. Fixed an issue where Snagit might not recognize offline subscription activation. Fixed an issue where the video recording toolbar could be hidden from users when using Snagit in virtual environments or with remote desktop applications. Bug Fixes Fixed an issue where using Step Capture with the Share destination set to File in the Capture Window could result in data loss. Fixed a crash that could occur when capturing on some HDR monitors. Fixed an issue where the cursor object in a capture might include some of the background image in certain situations. Fixed an issue with the Box share destination to use the default browser for authentication. Fixed an issue where the Blur tool Color property could show when the blur or pixelate Type was selected. Fixed an issue where the privacy policy link in Snagit's installer might not open in the expected language. (PONRPD) Download: Snagit 64-bit | 419.0 MB (Shareware) Links: SnagIt Home Page | Release Notes Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Recent Achievements

    • Week One Done
      jfam earned a badge
      Week One Done
    • First Post
      survivor303 earned a badge
      First Post
    • Week One Done
      CHUNWEI earned a badge
      Week One Done
    • One Year In
      survivor303 earned a badge
      One Year In
    • Week One Done
      jbatch earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      420
    2. 2
      +FloatingFatMan
      185
    3. 3
      snowy owl
      183
    4. 4
      ATLien_0
      179
    5. 5
      Xenon
      140
  • Tell a friend

    Love Neowin? Tell a friend!