flat assembler
Message board for the users of flat assembler.

Index > Heap > How to check memory usage on Linux?

Author
Thread Post new topic Reply to topic
TmX



Joined: 02 Mar 2006
Posts: 822
Location: Jakarta, Indonesia
TmX
Let's say I made a simple code which calculates the factorial of given number, or ran commands like "ls" or "cat helloworld.txt".

How to measure the memory usages? I tried using valgrind, but couldn't figure out what the result meant.

For example, I ran "valgrind ls" in a directory, and the output was:
Code:

==11706== Memcheck, a memory error detector.
==11706== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
==11706== Using LibVEX rev 1854, a library for dynamic binary translation.
==11706== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
==11706== Using valgrind-3.3.1-Debian, a dynamic binary instrumentation framework.
==11706== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.
==11706== For more details, rerun with: -v
==11706==
faktorial  faktorial.c  helloworld.txt  massif.out.11671  svn-commit.tmp  test.txt
==11706==
==11706== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 24 from 1)
==11706== malloc/free: in use at exit: 12,678 bytes in 12 blocks.
==11706== malloc/free: 63 allocs, 51 frees, 20,676 bytes allocated.
==11706== For counts of detected errors, rerun with: -v
==11706== searching for pointers to 12 not-freed blocks.
==11706== checked 116,704 bytes.
==11706==
==11706== LEAK SUMMARY:
==11706==    definitely lost: 0 bytes in 0 blocks.
==11706==      possibly lost: 0 bytes in 0 blocks.
==11706==    still reachable: 12,678 bytes in 12 blocks.
==11706==         suppressed: 0 bytes in 0 blocks.
==11706== Rerun with --leak-check=full to see details of leaked memory.
    


Is there any easier way to calculate the memory usage? Confused
Post 05 Jul 2011, 18:25
View user's profile Send private message Reply with quote
Tyler



Joined: 19 Nov 2009
Posts: 1216
Location: NC, USA
Tyler
"malloc/free: in use at exit: 12,678 bytes in 12 blocks." This is how much was still allocated when the program exited.
Post 05 Jul 2011, 20:08
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
TmX: which memory? It's not that simple. Some parts of memory are shared between processes. Some are memory-mapped files. Some are reserved, but not commited. Some are commited, but not really mapped (overcommit). etc. etc.
Post 05 Jul 2011, 20:55
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
TmX



Joined: 02 Mar 2006
Posts: 822
Location: Jakarta, Indonesia
TmX
I mean actual amount of memory used by the app.
Post 06 Jul 2011, 02:05
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
I tried to explain there is nothing like that on a modern OS.
Post 06 Jul 2011, 08:47
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
r22



Joined: 27 Dec 2004
Posts: 805
r22
You check how much memory is FREE before you start the app and then compare it with after you start the app. Background processes and jobs will mess this estimate up a bit.

It must be difficult to check for memory leaks in these modern OSes. Unless software that runs on modern OSes no longer has memory leaks Very Happy
Post 06 Jul 2011, 18:20
View user's profile Send private message AIM Address Yahoo Messenger Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3170
Location: Denmark
f0dder
r22 wrote:
You check how much memory is FREE before you start the app and then compare it with after you start the app. Background processes and jobs will mess this estimate up a bit.
You'll have to find a way to define "free", then - and that's not easy either.

_________________
Image - carpe noctem
Post 06 Jul 2011, 19:04
View user's profile Send private message Visit poster's website Reply with quote
r22



Joined: 27 Dec 2004
Posts: 805
r22
f0dder wrote:
r22 wrote:
You check how much memory is FREE before you start the app and then compare it with after you start the app. Background processes and jobs will mess this estimate up a bit.
You'll have to find a way to define "free", then - and that's not easy either.

Finding how much FREE memory you have on a modern OS is a piece of cake. - Just start popping ram chips off your memory sticks until the OS and all your programs no longer load.
- Count the number of chips you've removed and your done.
Post 06 Jul 2011, 20:01
View user's profile Send private message AIM Address Yahoo Messenger Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
Quote:
Finding how much FREE memory you have on a modern OS is a piece of cake. - Just start popping ram chips off your memory sticks until the OS and all your programs no longer load.
- Count the number of chips you've removed and your done.

But the size of memory used then would be less than memory used by exactly same software when there is plenty of memory available. Remember swapping, and remember some components' allocation sizes can depend on amount of memory available.
Post 06 Jul 2011, 20:08
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
TmX



Joined: 02 Mar 2006
Posts: 822
Location: Jakarta, Indonesia
TmX
Actually I'm thinking of something like top
Image

See the "RES" column?

Of course I cannot use that if the process is already finished/closed.
Post 07 Jul 2011, 06:28
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17473
Location: In your JS exploiting you and your system
revolution
The only proper way to know how much memory is used by any app is for that app to track and report its own memory usage.

Things like top don't show you the memory actually used.

And besides, "memory used" is not a precise term. Do you mean peak amount of memory holding valid and useful data at some instant. Or perhaps do you mean the total of all reads, and/or writes, involving external RAM. Peak memory allocated, whether actually holding valid or not? Total number of pages touched and committed? Or maybe you mean something else?
Post 07 Jul 2011, 07:05
View user's profile Send private message Visit poster's website Reply with quote
TmX



Joined: 02 Mar 2006
Posts: 822
Location: Jakarta, Indonesia
TmX
>> Do you mean peak amount of memory holding valid and useful data at some instant.

I guess that's the closest one.
Post 07 Jul 2011, 08:43
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17473
Location: In your JS exploiting you and your system
revolution
TmX wrote:
>> Do you mean peak amount of memory holding valid and useful data at some instant.

I guess that's the closest one.
Then the answer is simple. You have no way of knowing without complete knowledge of the app and its data usage. The OS cannot know.
Post 07 Jul 2011, 09:13
View user's profile Send private message Visit poster's website Reply with quote
r22



Joined: 27 Dec 2004
Posts: 805
r22
vid wrote:
Quote:
Finding how much FREE memory you have on a modern OS is a piece of cake. - Just start popping ram chips off your memory sticks until the OS and all your programs no longer load.
- Count the number of chips you've removed and your done.

But the size of memory used then would be less than memory used by exactly same software when there is plenty of memory available. Remember swapping, and remember some components' allocation sizes can depend on amount of memory available.

DRAT!! my final and best suggestion, if you need to know the memory usage of your "modern OS" application you must first completely port it to Windows then use task manager. Problem solved forever... Done... :D
Post 07 Jul 2011, 12:58
View user's profile Send private message AIM Address Yahoo Messenger Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
Not really.
Post 07 Jul 2011, 13:54
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.