Clear garbage memory of other software in c# [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
this is a strange question.
In my work I use a software (that NO one can modify or take vision of source code), this software, unfortunately, is very bad. It uses, when launched, about 1 gb RAM, but running and being used, it accumulates data in ram and after few hours it occupies over 4gb of ram, and can accumulates more and more if not stopped. Since not all pc of my company can handle this, they crash. Is there a way to clear data unused in ram from other software programming in c#?
I just need an hint, i program in c#.
I'm sorry for my English, and if this question is somehow wrong.
Thanks for time.
Edit: I will add some information about this software understand better what I can do, and what I cant't.
Since i would not gain anything hacking this software, but I would have a better life (and not only me but my co-workers and employees too), i don't see any issue in hacking this software. (well, my chief has no problems, and his chief too).
I will try the Arunasr hint, I program in c# and know this function. I will only have to decompile it.
I have to delete this question?
Thanks for all replies and all help you gave to me.

There is no such way, outside of killing and restarting that task. If it is a Windows Service, you can use Powershell, command line utility SC.exe, or this c# code:
public static void RestartService(string serviceName, int timeoutMilliseconds)
{
ServiceController service = new ServiceController(serviceName);
try
{
int millisec1 = Environment.TickCount;
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
// count the rest of the timeout
int millisec2 = Environment.TickCount;
timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds - (millisec2-millisec1));
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
}
catch
{
// ...
}
}
Reference: http://www.csharp-examples.net/restart-windows-service/

.NET runtime garbage collector tends to be lazy and only collects when free memory is about to run out. You can force garbage collection with GC.Collect(), but mind the performance: it is expensive if you do it frequently.
If the library really accumulates data (i.e. data remains references and GC cannot touch it), there is little you can do other than hack up the library, figure out what eats up the memory and release the unnecessary objects. It is not difficult to decompile the .NET bytecode. In the extreme, you can modify the assembly bytecode using reflection after it has been loaded. I would not recommend it unless you are desperate.
You can use GC to track real memory usage stats.

Related

Any new method on checking internet connectivity using .NET? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I already read some of the questions posted here and I found this post to be the most reliable regarding this matter: What is the best way to check for Internet connectivity using .NET?
But what if the network where the interface is connected have google blocked? (e.g. Internet in China)
If you're checking for internet connectivity, you probably have a reason... meaning you're looking to use some specific web resource. So check against that resource.
Even better, don't check at all. Internet services can go up or down at any moment... including the moment in between when you run your check and when you try to use the service. That means anything you do has to be able to handle failure anyway. So just don't run the check; put the work into your exception handler instead.
I know that may sound slow, or strange to use exception handling for flow control. But the main reason not to use exceptions for flow control is it's nearly the slowest thing you can do in all of computer science. You know what's even worse? Waiting for network packets to travel half way around the world or timeout, that's what.
In the rare case when you just want to show general internet status to the user, you can do it the same way Microsoft does, and use www.msftncsi.com.
If what you want is to check the state of the internet in .Net without depending on WebClient class or Google, this is the best way
First, import the DLL wininet
[System.Runtime.InteropServices.DllImport("wininet.dll")]
Then, call the static extern bool InternetGetConnectedState(...)
This returns true if the internet connects and false if it can not connect, regardless of Google:
[System.Runtime.InteropServices.DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState(out int Description, int ReservedValue);
public static bool IsConnected()
{
return InternetGetConnectedState(out int description, 0);
}
Where:
if(IsConnected())
{
//Internet is connected
}
else
{
//Internet is not connected
}

c# memory allocation in Stack/Heap occurs [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
In c# When does the memory allocation in Stack/Heap occurs. Is it during the runtime or compile time. From my study I understand that all memory allocations happens during run time for both value type and reference type. Is this correct?
How would it happen during compile-time ? The program is not yet running and there is no need to allocate memory before the program run. It is common sense that this should happen at run-time (When actually executing the generated IL).
Memory management it also optimized that it may not happen when you just create the variable, but when you first use it.
I think you might be confusing the actual allocation, which can only happen at runtime when the program is actually running, with the allocation calculation/determination. For example, consider the following method:
void Foo()
{
int i = 42;
Console.WriteLine(i);
}
The compiler will now statically (compile-time) that i will require 4 bytes of space on the stack. However, it will not be until the program actually runs that the actual allocation takes place.
Moreover, the above method won't even be compiled into machine code (a prerequisite for any operation such as allocation) until the CLR loads the code and passes it to the JIT (Just in Time compiler). Of course, even if it did, it's not until the actual process is created that the OS even allocates a memory address space for it to use...

All System.Threading.Timer instances in the process (including new ones) stops working after some while [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am encountering following issue in my .Net application:
It is a game server so there are around 150 threads to process all time-related logic, and some of them are Threading.Timer instances. The server was running fine at the beginning and to some point all timers stops working. I tried to start new timers via script executor. The new timers could be started but they are not firing events neither.
Anyone has idea about what could cause all the timers stop working in the process? I am not aware how could this happen theoretically.
System.Threading.Timer "executes a single callback method on a thread pool thread at regular intervals". Considering you are using 150 threads, its possible you are exhausing the thread pool.
Additionally, if you handler takes too long to process an event, Timer might fall behind and queue updates.
Re-think your design
Unless its a major component like scene loading; or audio playback; game makers generally avoid threading in games for reasons of reliability and simplicity.
Instead you may want to consider using:
deferred processing - delay processing until some time later
batch processing - rather than processing 10,000 items, process a batch of 100 items every 10ms
Gregory, J, "Game Engine Architecture"

Execution time testing for c# method [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a cs page with a few methods. When I try to load that page it takes a long time. Is there any way to identify how long each method is taking to fetch data? Instead of debugging is there any way of capturing the time spans?
Any tool or any other suggestions?
Yes you can enable Tracing to check which event takes how much time.
set Trace = true in the Page directive.
Also you can write custom trace line
Trace.Write("Method 1 begin");
//Call you method thats taking time.
CalculateSomething();
Trace.Write("Method 1 begin");
And you can use stopwatch to check the exact time.
using System.Diagnostics;
Stopwatch St = new Stopwatch();
St.Start();
//Call your method
Trace.Write("Stopwatch " + St.ElapsedTime.toString());
St.Stop();
The Visual Studio Profiling Tools let developers measure, evaluate, and target performance-related issues in their code. These tools are fully integrated into the IDE to provide a seamless and approachable user experience.
Profiling an application is straightforward. You begin by creating a new performance session. In Visual Studio Team System Development Edition, you can use the Performance Session Wizard to create a new performance session. After a performance session ends, data gathered during profiling is saved in a .vsp file. You can view the .vsp file inside the IDE. There are several report views available to help visualize and detect performance issues from the data gathered.
MSDN REFERENCE

Memory issue or not a best practice [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Please let me know if the following design is bad for managing heap /memory and from a design pattern point of view also.
Here I am using a C# Timers.Timer on a windows service where the time tick event fires on multiple threads.
Here my main point is about heap memory consumption , since lots of objects being created in different threads in micro sec and also we cannot guarantee when GC will collect them. so this can be harmful to heap and performance issue later to the application.Am i correct.
timer_tick()
{
Test objTst=null;
try
{
objTst=new Test();
objTst.Process();
}
catch(execption e){}
finally
{
objTst =null;
}
}
Please let me know following design is bad for manage heap /memory wise and design pattern wise also.
This "pattern" serves no purpose. Setting the variable to null is not required. As soon as the method completes, and objTst goes out of scope, it will be eligible for garbage collection, even if you don't set it to null.
I would also recommend not having an empty exception handler that just swallows and ignores exceptions entirely. If nothing else, you should at least log the exception you receive.
In general, I would write this as:
private void timer_Tick(object sender, EventArgs e)
{
var tester = new Test();
tester.Process();
}
If you have an exception logging mechanism, you could wrap it in a try/catch to handle or log the exceptions, but don't just swallow them completely.

Categories