2

Is there any way to calculate memory consumption in C. I have checked other answers on Stackoverflow but they were not satisfactory.

Something similar to the one we have in Java:

// Get the Java runtime
Runtime runtime = Runtime.getRuntime();
// Run the garbage collector
runtime.gc();
// Calculate the used memory
long memory = runtime.totalMemory() - runtime.freeMemory();
System.out.println("Used memory is bytes: " + memory + "bytes");
System.out.println("Used memory is kilobytes: " + bytesTokilobytes(memory) +"kb");
robbmj
  • 16,085
  • 8
  • 38
  • 63
user3213918
  • 333
  • 1
  • 6
  • 11
  • 1
    What platform are you developing on? – mpontillo Jan 25 '14 at 02:39
  • 1
    Make sure to specify the target platform. There is no "standard" way to do this task in C - solutions will vary based on operating system and exactly *what* is measured. Also, consider profiling (e.g. using external development tools) unless the application really needs to do this for a real/internal purpose. – user2864740 Jan 25 '14 at 02:40
  • 4
    Have you looked at [this question](http://stackoverflow.com/questions/63166/)? I think it's a possible duplicate. Some of the answers are fairly exhaustive, and cover multiple platforms. If so, what wasn't satisfactory about it? – mpontillo Jan 25 '14 at 02:44
  • By Popular request as a comment. Memory is not consumed. It is borrowed/used. You can use a bit of computers memory for the task at hand and be able to give it back. It is just like using a road. You do not consume it. But memory is a funny thing – Ed Heal Jan 25 '14 at 03:19
  • @Mike - Thanks for the link you have provided. There is a lot of info on that link and I'll definitely go through it. I have developed a C program on Windows and one of the task is to print the memory used by this program. – user3213918 Jan 25 '14 at 03:23

2 Answers2

1

C-language itself does not provide any means.

Although every specific platform gives you some support.

For example on Windows you can look at the TaskManager, Details tab. Right click on the listview column headers to add/remove columns. Some of them give insight on how much memory the process consumes. There are a lot of other tools including commercial ones (use google), that give more detailed picture.

On Windows there is also special API that allows writing your own tool. A while ago I wrote one. I do not want this answer to be an ad.

Kirill Kobelev
  • 10,252
  • 6
  • 30
  • 51
0

The real question seems to be, can you get the C heap to report how much space it's currently holding. I don't know of a portable way to do this.

Or you could plug in a "debugging heap" implementation which tracks this number and provides an API to retrieve it; debugging heaps are available as second-source libraries, and one MAY come with your compiler. (Many years ago I implemented a debugging heap as a set of macros which intercepted heap calls and redirected them through wrapper routines to perform several kinds of analysis; I wasn't maintaining a usage counter but I could have done so.) ((CAUTION: Anything allocated from a debugging heap MUST be returned to that heap, not the normal heap, and vice versa, or things get very ugly very quickly.))

Or your compiler may have some other nonstandard way to retrieve this information. Check its documentation.

keshlam
  • 7,931
  • 2
  • 19
  • 33