|
First, are you looking in Task Manager to determine this?
It's lying to you. It's not showing you how much memory your app is taking. It's showing you how much is RESERVED for your app.
You have to understand how memory allocations work in the .NET CLR and how it relates to Windows memory management. The .NET CLR manages the "Managed Heap", which is what your app sees (and Task Manager does NOT). When your app allocates an object, it's allocated on the Managed Heap. When an object is freed, that memory goes back into the Managed Heap, NOT BACK TO WINDOWS!
Just because your code is not using an object anymore, that does NOT mean the memory it occupies is immediately returned to the Managed Heap!
When more objects need to be allocated, it's faster for the CLR to allocate from the Managed Heap than it is for it to find out there's not enough enough heap left, go to Windows to get another block of memory, add it to the heap, then allocate your object.
This is why you're seeing what's RESERVED for your app.
When the CLR feels the conditions are right, it cleans up the managed heap, moving objects around and freeing up objects that don't have a reference being held anymore, returning that freed memory back to the Managed Heap. This is called Garbage Collection.
When Windows needs more memory, it asks the .NET CLR for whatever it can free up. If there's sufficient space on the Managed Heap, it'll shrink the size of the heap and return that freed memory back to Windows. If your code quickly allocats lots of objects and frees them, this can give the illusion of a memory leak.
|
|
|
|
|
Thanks for the explanation. Another question: Can giving a null value to a filled property release the memory it takes?
|
|
|
|
|
If you're thinking about the memory the property itself takes, no.
If you're thinking of the object the property is holding onto, it just reduces the reference count on the object. Reducing that count to 0 (nothing holds a reference to the object anymore) only tells the GC that the object CAN be collected and the memory returned to the managed heap.
In no case does releasing a managed object in .NET return memory back to Windows.
|
|
|
|
|
(both applications are written by us, application A can be a C# application or a C++ application )
I have an application A that starts application B.
I would need to know when application B is ready to process some rpc messages from application A.
I was thinking of sending a windows event from application B and wait for it in application A (application A can wait a little bit, no need to be extra fancy)
Or maybe start sending rpc (dummy ping) message in repetition from application A until application B can answer them properly ?
Any other better way to do this ?
Thanks.
CI/CD = Continuous Impediment/Continuous Despair
|
|
|
|
|
Why not get application B to send the first message? That way application A knows that B is ready.
|
|
|
|
|
If A has a FileSystemWatcher, and B writes a file when it starts, A gets a wake-up call via the FileSystemWatcher's event handler.
FileSystemWatcher Class (System.IO) | Microsoft Docs
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
I have this simple async web request code:
class AsyncResponse
{
public AsyncResponse()
{
test2();
Console.ReadKey();
}
public void test2()
{
for (int i = 0; i < 100; i++)
{
string url = "https//www.google.com";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.BeginGetResponse(new AsyncCallback(FinishWebRequest), request);
}
}
private void FinishWebRequest(IAsyncResult result)
{
HttpWebResponse response = (result.AsyncState as HttpWebRequest).EndGetResponse(result) as HttpWebResponse;
Console.WriteLine("Finished");
}
}
I only got on console two "Finished" lines, meaning FinishWebRequest didn't get finished more then 2 times, but I have 100 requests.
What is the issue?
|
|
|
|
|
You don't bother checking or timing any of the responses; how do you know what's going on? All this while sitting in your constructor.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
I want to learn about the Messenger feature of Microsoft.ToolKit.MVVM. In the documentation, I found the following code:
public class LoggedInUserRequestMessage : RequestMessage<User>
{
}
WeakReferenceMessenger.Default.Register<MyViewModel, LoggedInUserRequestMessage>(this, (r, m) =>
{
m.Reply(r.CurrentUser);
});
User user = WeakReferenceMessenger.Default.Send<LoggedInUserRequestMessage>();
I don't know what this, r, and m are. Do we need to always inherit from RequestMessage<user>? I need a simple example of how to use the Messenger feature.
Please guide me.
|
|
|
|
|
this is a keyword in C# which always represents the current instance of the current class - as such it s only ever available in non-static methods, because static code does not and cannot refer to an instance at all.
It's like a car: "my car" is an instance, "your car" is a different instance. But "this car" could refer to either, and the question "what colour is this car?" will return a different value depending on which instance we are talking about. If we are driving in my car, then the answer would be "black" because "my car" is a black Mercedes. If your BMW is being driven then the answer might be "green".r
r and m are parameters which come from the invocation of the lambda: MVVM - The MVVM Light Messenger In-Depth | Microsoft Docs[^] is a good place to start.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hi
I want to apply Client side and Server side validations for Calendar control (for cybersecurity). Can I apply ajax and if so how can I apply both Client side and Server side validations for the control.
|
|
|
|
|
You verify the transaction, not (just) some "control" you think may be the client. By doing too much on the front end (in terms of restricting), you start revealing what will work.
Note how they always say "Your PW or ID is wrong" (Or, guess which).
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
It seems other users have same problem than me, I can try to re-forumlate my question with other words hoping someone can help me:
I have been Googling and Binging all morning trying to find this answer.
I have a DataGridView that is bound to my SQL database using string builders. I have an update button on my form a user clicks to commit their new changes to the database. However, sometimes, I forget to press "Enter" to activate the event that tells the datagridview there are new changes on the data table. Is there a way to not have to press enter to identify changes?
|
|
|
|
|
|
That doesent works...
following my "savebutton" what I shoud add to this code in order to aplly changes on DataGridView despite enter is not clicked?
private void salvaClientiBtn_Click(object sender, EventArgs e)
{
tabellaClientiTableAdapter.Update(dataSet.TabellaClienti);
}
|
|
|
|
|
|
I have the following code for getting data and paginating them inside the DataGrid:
private void dataPager_OnDemandLoading(object sender, Syncfusion.UI.Xaml.Controls.DataPager.OnDemandLoadingEventArgs args)
{
List<Equipment> data;
using (var _sqliteContext = new SQLiteContext())
{
data = _sqliteContext.Equipments.Include(x => x.CostCenter).Skip(args.StartIndex).Take(args.PageSize).ToList();
}
dataPager.LoadDynamicItems(args.StartIndex, data);
(dataPager.PagedSource as PagedCollectionView).ResetCache();
}
Now, I want to use background worker to do calculation in an another thread. I make the following changes:
private void dataPager_OnDemandLoading(object sender, Syncfusion.UI.Xaml.Controls.DataPager.OnDemandLoadingEventArgs args)
{
BackgroundWorker worker = new BackgroundWorker();
worker.WorkerReportsProgress = false;
worker.DoWork += worker_DoWork;
worker.RunWorkerAsync();
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
List<Equipment> data;
using (var _sqliteContext = new SQLiteContext())
{
data = _sqliteContext.Equipments.Include(x => x.CostCenter).Skip(args.StartIndex).Take(args.PageSize).ToList();
}
dataPager.LoadDynamicItems(args.StartIndex, data);
(dataPager.PagedSource as PagedCollectionView).ResetCache();
}
But, worker_DoWork does not have any overload. I cannot pass args to worker_DoWork
How can I solve it?
|
|
|
|
|
The DoWorkEventArgs class includes an Argument Property[^]
You can set that to an instance of any class (or struct) to pass whatever you need in.
It also contains a Result[^] property for passing completed data back to the caller via the RunWorkerCompleted Event[^]
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Is it possible to pass more than one instance of any class? Do I need to use List<t> in this case?
|
|
|
|
|
You can use any object - so an int , a MyClass , a collection of IEnumerable objects, an array ... literally anything you need to.
Just be aware that you are multithreaded so you need to ensure thread safety if the argument data is going to be even looked at by two different threads, and that Control derived classes can only be accessed from the thread that created them (the UI thread).
But you almost certainly knew that ... I added it for those other readers who are just getting started with threading.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I used the following code for getting data from SQLite:
private void dataPager_OnDemandLoading(object sender, Syncfusion.UI.Xaml.Controls.DataPager.OnDemandLoadingEventArgs args)
{
List<int> list = new List<int>()
{
int.Parse(args.StartIndex.ToString()),
int.Parse(args.PageSize.ToString())
};
BackgroundWorker worker = new BackgroundWorker();
worker.WorkerReportsProgress = false;
worker.DoWork += worker_DoWork;
worker.RunWorkerAsync(list);
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
List<int> value = (List<int>)e.Argument;
List<Equipment> data;
using (var _sqliteContext = new SQLiteContext())
{
data = _sqliteContext.Equipments.Include(x => x.CostCenter).Skip(value[0]).Take(value[1]).ToList();
}
this.Dispatcher.Invoke(() =>
{
dataPager.LoadDynamicItems(value[0], data);
(dataPager.PagedSource as PagedCollectionView).ResetCache();
});
}
There is no runtime error but the table in the UI is empty.
|
|
|
|
|
Start with the debugger and see what data contains before you start the Invoke.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I debugged. Before Dispatcher.Invoke, the data is full of 50 records. But something happens in Dispatcher.Invoke and prevent data to be shown in the UI.
modified 16-Jul-22 2:16am.
|
|
|
|
|
Could it be that in worker_DoWork your variable data is going out of scope when the function ends?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Your BackgroundWorker is going to go out of scope; I haven't used that pattern so if it gets "busy", it will be hard to tell, if at all.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|