Connecting Tech Pros Worldwide Help | Site Map

how to find available memory in linux box

 
LinkBack Thread Tools Search this Thread
  #1  
Old December 6th, 2006, 08:25 PM
david wolf
Guest
 
Posts: n/a
Default 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


  #2  
Old December 6th, 2006, 08:35 PM
Victor Bazarov
Guest
 
Posts: n/a
Default 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


  #3  
Old December 7th, 2006, 12:15 AM
Xeranic
Guest
 
Posts: n/a
Default 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 ?

  #4  
Old December 7th, 2006, 04:25 PM
david wolf
Guest
 
Posts: n/a
Default 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 ?
  #5  
Old December 7th, 2006, 05:15 PM
Noah Roberts
Guest
 
Posts: n/a
Default 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.

  #6  
Old December 7th, 2006, 06:35 PM
TvN
Guest
 
Posts: n/a
Default 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!

  #7  
Old December 7th, 2006, 07:55 PM
Marcus Kwok
Guest
 
Posts: n/a
Default 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
  #8  
Old December 14th, 2006, 09:27 PM
Xeranic
Guest
 
Posts: n/a
Default 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 ?
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

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 220,840 network members.