how to find available memory in linux box

D

david wolf

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
 
V

Victor Bazarov

david said:
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
 
X

Xeranic

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 ?
 
D

david wolf

Thanks Xeranic.

But how to check /proc file system?
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 ?
 
N

Noah Roberts

david said:
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.
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.
 
T

TvN

Hi David,
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
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!
 
M

Marcus Kwok

david wolf said:
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.
 
X

Xeranic

Yes, Valgrind is best.

Thanks Xeranic.

But how to check /proc file system?
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 ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top