Connecting Tech Pros Worldwide Forums | Help | Site Map

how to find available memory in linux box

david wolf
Guest
 
Posts: n/a
#1: Dec 6 '06
I have a problem that is used to test memory leak as follows, however,
after each run of the program, if I am using the command "free" on the
linux box, I did not see any difference about the usable memory. My
questions are:
1) How to find out exact free memory that my program can use?
2) Use which command to do this (top, or free)
3) why it does not show memory leak in my case (I am using the linux
command free)

----------------------------------------------------------------------------------------------------------------
#include <iostream>
#include <new>
#include <stdlib.h>
using namespace std;


int main(){
int *p= new int[100000000];
if ( p == NULL ) cout << "Failed to allocate memory" << endl;
else {
p[0] = 1;
p[1] = 2;
p[2] = 3;
cout << "p[0] = " << p[0] << ", p[1] = " << p[1] << ", p[2]
= " << p[2] << endl;
}
}

--------------------------------------------------------------------------------------------------------------------------------------------

$ a.out
p[0] = 1, p[1] = 2, p[2] = 3
$ free
total used free shared buffers
cached
Mem: 3907304 3884812 22492 0 281760
3123052
-/+ buffers/cache: 480000 3427304
Swap: 4145288 16084 4129204
$ a.out
p[0] = 1, p[1] = 2, p[2] = 3
$ free
total used free shared buffers
cached
Mem: 3907304 3884812 22492 0 281760
3123052
-/+ buffers/cache: 480000 3427304
Swap: 4145288 16084 4129204


Victor Bazarov
Guest
 
Posts: n/a
#2: Dec 6 '06

re: how to find available memory in linux box


david wolf wrote:
Quote:
I have a problem that is used to test memory leak as follows, however,
after each run of the program, if I am using the command "free" on the
linux box, I did not see any difference about the usable memory. My
questions are:
1) How to find out exact free memory that my program can use?
2) Use which command to do this (top, or free)
3) why it does not show memory leak in my case (I am using the linux
command free)
All good questions. Wrong newsgroup, though. Try 'comp.os.linux.*'
hierarchy. Good luck!

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Xeranic
Guest
 
Posts: n/a
#3: Dec 7 '06

re: how to find available memory in linux box




On Dec 7, 5:46 am, "david wolf" <yih...@gmail.comwrote:
Quote:
I have a problem that is used to test memory leak as follows, however,
after each run of the program, if I am using the command "free" on the
linux box, I did not see any difference about the usable memory. My
questions are:
1) How to find out exact free memory that my program can use?
2) Use which command to do this (top, or free)
3) why it does not show memory leak in my case (I am using the linux
command free)
>
---------------------------------------------------------------------------*-------------------------------------
#include <iostream>
#include <new>
#include <stdlib.h>
using namespace std;
>
int main(){
int *p= new int[100000000];
if ( p == NULL ) cout << "Failed to allocate memory" << endl;
else {
p[0] = 1;
p[1] = 2;
p[2] = 3;
cout << "p[0] = " << p[0] << ", p[1] = " << p[1] << ", p[2]
= " << p[2] << endl;
}
>
}---------------------------------------------------------------------------*-----------------------------------------------------------------
>
$ a.out
p[0] = 1, p[1] = 2, p[2] = 3
$ free
total used free shared buffers
cached
Mem: 3907304 3884812 22492 0 281760
3123052
-/+ buffers/cache: 480000 3427304
Swap: 4145288 16084 4129204
$ a.out
p[0] = 1, p[1] = 2, p[2] = 3
$ free
total used free shared buffers
cached
Mem: 3907304 3884812 22492 0 281760
3123052
-/+ buffers/cache: 480000 3427304
Swap: 4145288 16084 4129204
As your program 'a.out' finished the memory have been 'free', the
memory record is same. Some os implements copy-on-wirte tech, so the
virtual address (not memory) you alloc may not exist in physical
memory, until you write something to it. Try check /proc file system
instead of 'free' command. At last, It's not a good idear to check
memory leak like this, which program's (process's) memory leak you want
see ?

david wolf
Guest
 
Posts: n/a
#4: Dec 7 '06

re: how to find available memory in linux box


Thanks Xeranic.

But how to check /proc file system?

Xeranic wrote:
Quote:
On Dec 7, 5:46 am, "david wolf" <yih...@gmail.comwrote:
Quote:
I have a problem that is used to test memory leak as follows, however,
after each run of the program, if I am using the command "free" on the
linux box, I did not see any difference about the usable memory. My
questions are:
1) How to find out exact free memory that my program can use?
2) Use which command to do this (top, or free)
3) why it does not show memory leak in my case (I am using the linux
command free)

---------------------------------------------------------------------------*-------------------------------------
#include <iostream>
#include <new>
#include <stdlib.h>
using namespace std;

int main(){
int *p= new int[100000000];
if ( p == NULL ) cout << "Failed to allocate memory" << endl;
else {
p[0] = 1;
p[1] = 2;
p[2] = 3;
cout << "p[0] = " << p[0] << ", p[1] = " << p[1] << ", p[2]
= " << p[2] << endl;
}

}---------------------------------------------------------------------------*-----------------------------------------------------------------

$ a.out
p[0] = 1, p[1] = 2, p[2] = 3
$ free
total used free shared buffers
cached
Mem: 3907304 3884812 22492 0 281760
3123052
-/+ buffers/cache: 480000 3427304
Swap: 4145288 16084 4129204
$ a.out
p[0] = 1, p[1] = 2, p[2] = 3
$ free
total used free shared buffers
cached
Mem: 3907304 3884812 22492 0 281760
3123052
-/+ buffers/cache: 480000 3427304
Swap: 4145288 16084 4129204
>
As your program 'a.out' finished the memory have been 'free', the
memory record is same. Some os implements copy-on-wirte tech, so the
virtual address (not memory) you alloc may not exist in physical
memory, until you write something to it. Try check /proc file system
instead of 'free' command. At last, It's not a good idear to check
memory leak like this, which program's (process's) memory leak you want
see ?
Noah Roberts
Guest
 
Posts: n/a
#5: Dec 7 '06

re: how to find available memory in linux box



david wolf wrote:
Quote:
I have a problem that is used to test memory leak as follows, however,
after each run of the program, if I am using the command "free" on the
linux box, I did not see any difference about the usable memory.
Quote:
3) why it does not show memory leak in my case (I am using the linux
command free)
Because when your program terminates the OS reclaims the resources it
reserved.

TvN
Guest
 
Posts: n/a
#6: Dec 7 '06

re: how to find available memory in linux box


Hi David,
Quote:
I have a problem that is used to test memory leak as follows, however,
after each run of the program, if I am using the command "free" on the
linux box, I did not see any difference about the usable memory. My
questions are:
1) How to find out exact free memory that my program can use?
It depends on: you limits (ulimit -a) , available memory, etc
Quote:
3) why it does not show memory leak in my case (I am using the linux
command free)
Use valgrind for this.

Best regards,
Volodymyr!

Marcus Kwok
Guest
 
Posts: n/a
#7: Dec 7 '06

re: how to find available memory in linux box


david wolf <yihucd@gmail.comwrote:
Quote:
I have a problem that is used to test memory leak as follows, however,
after each run of the program, if I am using the command "free" on the
linux box, I did not see any difference about the usable memory. My
questions are:
1) How to find out exact free memory that my program can use?
2) Use which command to do this (top, or free)
3) why it does not show memory leak in my case (I am using the linux
command free)
>
----------------------------------------------------------------------------------------------------------------
#include <iostream>
#include <new>
#include <stdlib.h>
using namespace std;
>
>
int main(){
int *p= new int[100000000];
if ( p == NULL ) cout << "Failed to allocate memory" << endl;
This doesn't answer your question, but in case allocation fails, new
will throw an exception. If you want it to return NULL, you have to use
new(std::nothrow) or something like that.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Xeranic
Guest
 
Posts: n/a
#8: Dec 14 '06

re: how to find available memory in linux box


Yes, Valgrind is best.

On Dec 8, 1:43 am, "david wolf" <yih...@gmail.comwrote:
Quote:
Thanks Xeranic.
>
But how to check /proc file system?
>
Xeranic wrote:
Quote:
On Dec 7, 5:46 am, "david wolf" <yih...@gmail.comwrote:
Quote:
I have a problem that is used to test memory leak as follows, however,
after each run of the program, if I am using the command "free" on the
linux box, I did not see any difference about the usable memory. My
questions are:
1) How to find out exact free memory that my program can use?
2) Use which command to do this (top, or free)
3) why it does not show memory leak in my case (I am using the linux
command free)
>
Quote:
Quote:
---------------------------------------------------------------------------*-------------------------------------
#include <iostream>
#include <new>
#include <stdlib.h>
using namespace std;
>
Quote:
Quote:
int main(){
int *p= new int[100000000];
if ( p == NULL ) cout << "Failed to allocate memory" << endl;
else {
p[0] = 1;
p[1] = 2;
p[2] = 3;
cout << "p[0] = " << p[0] << ", p[1] = " << p[1] << ", p[2]
= " << p[2] << endl;
}
>
Quote:
Quote:
}---------------------------------------------------------------------------*-----------------------------------------------------------------
>
Quote:
Quote:
$ a.out
p[0] = 1, p[1] = 2, p[2] = 3
$ free
total used free shared buffers
cached
Mem: 3907304 3884812 22492 0 281760
3123052
-/+ buffers/cache: 480000 3427304
Swap: 4145288 16084 4129204
$ a.out
p[0] = 1, p[1] = 2, p[2] = 3
$ free
total used free shared buffers
cached
Mem: 3907304 3884812 22492 0 281760
3123052
-/+ buffers/cache: 480000 3427304
Swap: 4145288 16084 4129204
>
Quote:
As your program 'a.out' finished the memory have been 'free', the
memory record is same. Some os implements copy-on-wirte tech, so the
virtual address (not memory) you alloc may not exist in physical
memory, until you write something to it. Try check /proc file system
instead of 'free' command. At last, It's not a good idear to check
memory leak like this, which program's (process's) memory leak you want
see ?
Closed Thread