
December 6th, 2006, 09:25 PM
| | | how to find available memory in linux box
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 | 
December 6th, 2006, 09:35 PM
| | | 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 | 
December 7th, 2006, 01:15 AM
| | | 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 ? | 
December 7th, 2006, 05:25 PM
| | | 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 ?
| | 
December 7th, 2006, 06:15 PM
| | | 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. | 
December 7th, 2006, 07:35 PM
| | | 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! | 
December 7th, 2006, 08:55 PM
| | | 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 | 
December 14th, 2006, 10:27 PM
| | | 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 ?
| | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|