Correct way to find memory used by process

Anirudh Sundar

New Member
Joined
Aug 14, 2018
Messages
2
Reaction score
1
Credits
0
Hi,

I am running a C++ application on Redhat linux, built using gcc. The program works in a step by step manner, and each steps uses new and delete to allocate and remove memory for huge data structures. I've made sure that we are freeing all the memory allocated, but when I check for the memory used by process, it just keeps increasing.

I read that linux tries to reuse memory given to a process and hence sometimes doesn't reclaim the memory freed by the process. Is this true, and if so, how do I make sure that exactly how much memory my process is using currently, excluding the memory freed but kept by linux.

I'm checking for memory used by process right now in 2 ways. One using top/htop command and other is fetching the data from /proc/*/status file.

Could you let me know how to find the exact amount of memory (only the the amount of memory it holds right now, not the freed memory).

Thanks.
 


wizardfromoz

Administrator
Staff member
Gold Supporter
Joined
Apr 30, 2017
Messages
7,502
Reaction score
6,350
Credits
26,511
Hello @Anirudh Sundar and welcome to linux.org :)

I am a Home User and not familiar with the rest of your work, but the following command should apply to Red Hat as well

Code:
free

Mine, on Linux Mint 'Sylvia' 18.3 with 16GB RAM looks like this

[email protected] ~ $ free
total used free shared buff/cache available
Mem: 16320176 1154536 13882776 138756 1282864 14699664
Swap: 0 0 0

Is that the sort of info you are after?

Cheers

Chris Turner
wizardfromoz

Edited added BTW

BTW - free -h will give the output in human readable form
 
OP
A

Anirudh Sundar

New Member
Joined
Aug 14, 2018
Messages
2
Reaction score
1
Credits
0
Hi @wizardfromoz, thanks for the reply. If I'm not mistaken, free -h command gives the total amount of free and used memory in the entire system right? I wanted to know how to check the memory used by a specific process, like maybe the current process.

My question is basically similar to this thread in ubuntuforums that I found. I have the same issue as given there, my program seems to be releasing the memory, but when I check using the top/htop commands or check the /proc/*/status file, it shows that the process still occupies that memory.

That thread has answers where people say that linux sometimes doesn't immediately reclaim the memory allocated to some process for efficiency, so I wanted to know if there was any way to check the exact amount of memory used by my process.
 

Rob

Administrator
Staff member
Joined
Oct 27, 2011
Messages
915
Reaction score
1,736
Credits
1,129
$100 Digital Ocean Credit
Get a free VM to test out Linux!

Linux.org Hosting Donations
Consider making a donation

Members online


Top