• 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

    • Thanks to Herr Musk being a total poison pill, they can't even give those pieces of scrap away.  They can't even ship them to the UK/EU because they're completely illegal over here.  
    • Intel vs AMD? Microsoft seemingly has a clear recommendation for Windows 11 Pro PC upgrade by Sayan Sen Microsoft and its partners are now quite actively and regularly promoting the upgrade to Windows 11. Asus, for example, recently published blog posts about the "mandatory Windows 11 upgrade" that is coming as the Windows 11 end of support date nears. Microsoft itself, from time to time, urges users to upgrade to its newest OS. Back in February 2024, Microsoft released an advert highlighting the best things about Windows 11 over Windows 10. Later, in June in the same year, the tech giant busted "myths and misconceptions" surrounding a Windows 11 upgrade. And towards the end of 2024, in December, Microsoft put up a blog post outlining the gaming features a user enjoys on 11 if they were to upgrade from Windows 10. While technically there is nothing wrong with a company promoting its own product, sometimes these campaigns make little sense and they fall flat. For example, in January earlier this year, Microsoft shared a blog post headlined "Free Upgrade to Windows 11 (For a Limited Time Only)" which did not make sense as it offered little information about it being a "free upgrade," and it was rightfully, later taken down. The company is back again with a new commercial about Windows 11. This time it is aimed mainly at IT professionals and enterprises as the advert talks about upgrading to Windows 11 Pro from Windows 10. This landed a few days after Microsoft released a new backup tool for organizations for such a purpose. What is interesting is that the company is promoting Intel's vPro processors and there is no mention of AMD's Ryzen PRO parts. The commercial is posted on the Windows official YouTube channel and has been titled "Right side of risk | Windows 11 Pro and Intel". The video description says, "Windows 10 support ends October 14. Stay on the right side of risk—upgrade now to the power of Windows 11 Pro PCs with Intel vPro®." AMD does have a support article about the subject headlined "Support Your Customers’ Move to Windows 11, With AMD Ryzen™ PRO Processors" and you can find it here. This is not the first time Microsoft has promoted Intel CPUs over AMD ones. Back in 2021, the company also put up a full page explaining how users should "look for the Intel EVO badge" on a new device before making a purchase decision because such PCs are "verified wonderful" which was a bit of an odd language. Like the limited upgrade time article, the page above was taken down after we reported on it (can be viewed via the archive) and replaced with something else. The new commercial was published about a couple of days ago, and it is possible that Microsoft may have a dedicated AMD advert too in the pipeline scheduled for a later release, and that would only be fair if both companies get a similar treatment.
    • Don’t blame web developers for the downfall of Firefox. 😂
    • Microsoft, Indian police bust AI-powered tech support scam ring targeting elderly in Japan by Paul Hill Pop-up scams pretending to be Microsoft Working with India’s Central Bureau of Investigation (CBI), Microsoft recently assisted in busting a scam network that was targeting the elderly in Japan. The CBI raided 19 locations on May 28, leading to the arrest of six key operatives and the taking down of two call centers. The scammers were impersonating Microsoft specifically and using tech support scams against Japanese seniors. The raid led to the seizure of both digital and physical infrastructure, including computers, storage devices, and phones. The scammers were targeting older adults, who are more vulnerable to fraud. To put this activity to an end, Microsoft’s Digital Crimes Unit (DCU), the Japan Cybercrime Control Center (JC3), Japan’s National Police Agency (NPA), and India’s CBI conducted significant cross-border collaboration to trace the criminals. Thanks to the internet, cross-border crimes like these have been around for a while and multinational tech firms like Microsoft are making significant efforts to help law enforcement agencies crack down on cybercrime. Artificial intelligence is also starting to be used to make more sophisticated scams. The evolving threat This case reveals an evolution in how Microsoft’s DCU addresses cybercrime involving tech support fraud. Thanks to AI, scammers have been able to scale their operations. In response, Microsoft has moved away from focusing on individual call centers to target the heads of criminal operations and disrupting their technical infrastructure. Notably, Microsoft’s collaboration with JC3 is the first time the DCU has partnered with a Japan-based organization to assist victims. Microsoft is continually getting tips from JC3 about malicious pop-ups urging recipients to call fake technical support lines that claim to be Microsoft. This data has allowed Microsoft to shut down 66,000 malicious domains and URLs globally since May 2024. Microsoft noted that artificial intelligence is now being used by criminals to scale their operations. Some ways in which these entities leverage AI are for victim identification, writing convincing scam emails and building fake web pages, as well as for convincing translations. Anyone can use AI for malicious purposes so it could increase the number of people or groups carrying out attacks. It also makes attacks much more sophisticated and harder to detect and necessitates better consumer protections and more sophisticated security tools such as passkeys to reduce hacks. Protecting vulnerable populations and what readers can do Tech support fraud attacks have been found by the FBI to disproportionately affect older people, resulting in $590 million in losses in 2023 for just older Americans alone. In this operation that targeted Japanese victims, around 90% of the 200 affected people were over 50. If you’ve ever received suspicious communications from a party claiming to be Microsoft, you should know that Microsoft never sends unsolicited emails or makes phone calls requesting personal or financial information, and it doesn’t offer unsolicited tech support. If you do get any suspicious communications, then you should report it to Microsoft so that it can take action.
  • Recent Achievements

    • Week One Done
      luxoxfurniture earned a badge
      Week One Done
    • First Post
      Uranus_enjoyer earned a badge
      First Post
    • Week One Done
      Uranus_enjoyer earned a badge
      Week One Done
    • Week One Done
      jfam earned a badge
      Week One Done
    • First Post
      survivor303 earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      434
    2. 2
      +FloatingFatMan
      239
    3. 3
      snowy owl
      214
    4. 4
      ATLien_0
      211
    5. 5
      Xenon
      157
  • Tell a friend

    Love Neowin? Tell a friend!