Restart Application If using too much RAM [duplicate] - c#

This question already has answers here:
How to get memory available or used in C#
(6 answers)
Closed 9 years ago.
I'm creating a server (Console App), but after doing some long-term testing I found out it grows eating RAM. For the local test suite, I am not working with much RAM
(8GB-DDR3 #2400MHz)Is there a way (In Program.cs, I assume) to restart the program if it is using over 'x' amount of RAM? Also, one way could be a timed loop/checkup?

You can use GC.GetTotalMemory. It returns an approximate value (long) of how much memory your program has allocated.
You can create a Timer object and make this comparison under the Tick event handler.
For more information, you can look here: http://msdn.microsoft.com/en-us/library/system.gc.gettotalmemory.aspx

I agree with what others have said about fixing your memory leak.
If you want to restart your program, create a second application that monitors the first process. Then, when memory gets too high in your original app, safely shut it down and allow the second application to launch it again.

Related

How to know when gc working [duplicate]

This question already has an answer here:
Monitoring Garbage Collector in C#
(1 answer)
Closed 5 years ago.
After I handle all memory leaks in my application I experience a time to time freeze , most of the freeze happens on weak PC's
I thinking it's exactly the time when GC starts to work.
can I monitor GC invoke time to be sure that that's the problem
Thanks.
There are a number of performance counter related to GC: https://msdn.microsoft.com/en-us/library/x2tyfybc(v=vs.71).aspx
In your case, you should watch the value of # Gen 2 Collections and see if it increases when you experience the freeze. Note that it could also be gen 0 or 1 collections, but they're much less likely to cause a noticeable freeze.

Do something at a given (odd) BPM [duplicate]

This question already has answers here:
High resolution timer in C#
(5 answers)
Closed 5 years ago.
No matter where I look, I can't find a good answer to this question. I'd like to have something happen at a given BPM (in my example, I'm using BPM), but the basic C# Timer class isn't working for me. Since it only measures in milliseconds, any actions performed within the timer get noticeably unsynced from the music. I've attempted to use this MicroTimer Library but with no luck! Though it can be quite fine grained, it's resource heavy and it doesn't have the resolution necessary. I understand I can have a function with a counter, but is there a good way to do this with Visual Studio's libraries (like the basic timer)? I hear those aren't as processor hungry.
I doubt you'll get the kind of time resolution you're looking for in a managed language like C#.
Hell, even if you were writing in C the OS could decide another process is more important and just like that you're out of sync.
Maybe consider using the timer, but resyncing every second or half second? I'd default to another user if they have experience in this area, but I'd at least give that a shot. Or go by the system clock ticks?

C# lock vs Java syncronized - Is there any difference in runtime? [duplicate]

This question already has answers here:
Are there any differences between Java's "synchronized" and C#'s "lock"?
(3 answers)
Closed 9 years ago.
I'm wondering if there is any difference in runtime at lock vs syncronized.
I have learnd that syncronized is a slow operation and outdated at Java.
Today I saw the lock at C# and I'm wondering if they are the same and lock is something I "want" to avoid same as in Java or maybe he is much faster and I want to use it...
Thanks!
1 synchronized is not outdated, java.util.concurrent.locks package simply provides extended functions which are not always needed.
2 Locking is done at CPU level and there is no difference between Java and C# in this regard
see http://www.cs.umd.edu/~pugh/java/memoryModel/jsr-133-faq.html
... special instructions, called memory barriers, are required to flush or invalidate the local processor cache in order to see writes made by other processors or make writes by this processor visible to others. These memory barriers are usually performed when lock and unlock actions are taken; they are invisible to programmers in a high level language.

Run System.Timers.Timer event on specific day and time each week c# [duplicate]

This question already has answers here:
How to call a method daily, at specific time, in C#?
(19 answers)
Closed 9 years ago.
I'm in the process of writing a windows service using System.Timers.Timer to keep track of my interval. What I'd like to do is make it so my service will launch on a specific day and time based on variables in my app.config file.
I don't think you're really writing a Windows service, I think you're writing a job. A cleaner, and easier approach, to your problem would be to write a Console application and then setup a Windows Scheduled Task to run that Console application at the intervals you want.
Never mind the fact that you'll have to set the time in milliseconds with the Timer approach, the Windows service would not be capable of handling Daylight Savings Time and more.
Although I think your best bet is to make this a scheduled task, you can easily create a waitable timer that will signal at the same time every day. Windows has a Waitable Timer object, but there's no .NET support for it.
I published an article a while back called Waitable Timers in C#, in which I showed how to use this object from a C# program. Unfortunately, the site that published the article is no longer. However, you can download the code examples for the article from my site at http://www.mischel.com/pubs/waitabletimer.zip. You're free to use the code in any way you see fit.

.NET framework issue? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
C# memory leak?
Sorry if this is not the right place to post this, seeing as it's not strictly code related.
Basically, since the beginning of this work, a program which has worked flawlessly before has suddenly started throwing Out of Memory exceptions. After checking out the usage, I discovered the memory usage increases by a ridiculous 1mb per second! This happens on a part of the program that iterates over a collection, and calls many different methods - so it seems like these objects are staying in memory even after use.
What does this issue sound like to everyone else? I've been debugging the code, ensuring nothing's left open and all is disposed, but to no avail. I'm wondering if perhaps the .NET framework on the computer it runs from has become corrupt, and the garbage collection isn't running.
I plan to test on another computer later to hopefully prove this theory
Garbage collection will only clear up objects in the heap with no references left to them. Although you've ensured you're disposing everything it'll also be an idea to specifically null any of these references to objects after use.
Perhaps you can post the troublesome code?
Have a look at dotTrace tool. 10 days trial version is available.
Using it you can track what keeps the object reference to objects that should be removed from memory.

Categories