Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I determine how much memory my program is currently occupying

Related to my previous question:
Preventing Memory issues when handling large amounts of text

Is there a way to determine how much memory space my program is occupying? I end up processing a large amount of text file and usually store the processed objects in memory. There are times where there will be too much information, and I will run out of memory. I have a solution for avoiding the memory allocation problem, but I only want to use it when necessary, to avoid paging, which will ultimately decrease my performance when it's not necessary. Is there a way to figure out how much memory I am occupying, so that I can page my information only when necessary?

NOTE: I am looking for a solution that my program can utilize to begin paging when necessary.

like image 425
Dan McClain Avatar asked Sep 17 '09 19:09

Dan McClain


People also ask

How do I see how much memory a program is using?

Check Computer Memory Usage EasilyTo open up Resource Monitor, press Windows Key + R and type resmon into the search box. Resource Monitor will tell you exactly how much RAM is being used, what is using it, and allow you to sort the list of apps using it by several different categories.

How much memory does my program need?

4GB of RAM is the bare minimum memory needed to run a base computer model.

How do I check Python program memory usage?

You can use it by putting the @profile decorator around any function or method and running python -m memory_profiler myscript. You'll see line-by-line memory usage once your script exits.

How much memory is an app using?

Tap Developer options and then tap Memory. In the resulting screen (Figure B), you'll see a list of the average memory used by the device in the past three hours (you can adjust the time frame, by tapping the time drop-down at the top). The Memory usage window in Android 12.


2 Answers

long bytes = System.Diagnostics.Process.GetCurrentProcess().WorkingSet64;
like image 130
Matt Wrock Avatar answered Oct 13 '22 19:10

Matt Wrock


You can try GC.GetTotalMemory:

Retrieves the number of bytes currently thought to be allocated. A parameter indicates whether this method can wait a short interval before returning, to allow the system to collect garbage and finalize objects.

The important thing to note is this part: "Retrieves the number of bytes currently thought to be allocated". This means that this method may not be 100% accurate - as long as you know this going in, you should be able to get a rough idea of your virtual memory utilization at a given point in your application execution.

Edit: Let me now offer a different solution that will probably be more productive: use perfmon and the CLR performance counters.

like image 31
Andrew Hare Avatar answered Oct 13 '22 20:10

Andrew Hare