Determining free(available) memory in MV linux | Unix Linux Forums | Programming

  Go Back    


Programming Post questions about C, C++, Java, SQL, and other programming languages here.

Determining free(available) memory in MV linux

Programming


Tags
linux

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-07-2006
trancedeejay trancedeejay is offline
Registered User
 
Join Date: Feb 2006
Last Activity: 6 March 2006, 6:18 AM EST
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Determining free(available) memory in MV linux

HI
I'm a rookie in C programming and I'm working in Monta Vista Linux. I have to write a program that displays free memory. I have memtester(allready written by someone else) and now I have to type how much amount of memory tester will test and I want that memtester finds out himself how much of free memory is available in the system to test and tests it.

I tried by opening /proc/meminfo and locate the number of free memory, but when I would open a program to determine free memory free memory number would change probably and data wouldn't be real.
Please I need any suggestion of you experts how to determine amount of free memory that will be tested with program.
Have a nice Day

Matt
Sponsored Links
    #2  
Old 02-07-2006
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 30 October 2014, 11:30 PM EDT
Location: NM
Posts: 10,253
Thanks: 285
Thanked 803 Times in 747 Posts
My first suggestion is: get a copy of the source for the free utility - it is part of coreutils for Linux.

Second - this is probably a difficult exercise for a beginner. Consider using
a call to free -m inside a system() call in C.
Sponsored Links
    #3  
Old 02-09-2006
trancedeejay trancedeejay is offline
Registered User
 
Join Date: Feb 2006
Last Activity: 6 March 2006, 6:18 AM EST
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
HI
If I compare numbers on what free -m displays or what is the number on meminfo, they are different
free -m
total used free shared buffers cached
Mem: 492 23 469 0 1 14
-/+ buffers/cache: 7 485
Swap: 0 0 0



meminfo:
total: used: free: shared: buffers: cached:
Mem: 516644864 25026560 491618304 0 1560576 15425536
Swap: 0 0 0
MemTotal: 504536 kB
MemFree: 480096 kB
MemShared: 0 kB
Buffers: 1524 kB
Cached: 15064 kB
SwapCached: 0 kB
Active: 5556 kB
Inactive: 12048 kB
HighTotal: 0 kB
HighFree: 0 kB
LowTotal: 504536 kB
LowFree: 480096 kB
SwapTotal: 0 kB
SwapFree: 0 kB
Committed_AS: 1156 kB
VmallocTotal: 491512 kB
VmallocUsed: 596 kB
VmallocChunk: 490916 kB

What if I would somehow get the number of free memory and store it somewhere. Then memtester reads the number from stored place and checks that much amount of memory that number(should be in MB) is writen in stored place. Please help
Have a nice Day

Matt
    #4  
Old 02-09-2006
trancedeejay trancedeejay is offline
Registered User
 
Join Date: Feb 2006
Last Activity: 6 March 2006, 6:18 AM EST
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
HI
Is maybe some function available something like getfreemem() or something that would display or..........
I don't know - I just have to have number of MB that is available on system and that is reachable from other program like a parameter to the second program that orders to memtester to check exactly that amount of memory.
Have a nice Day

Matt
Sponsored Links
    #5  
Old 02-09-2006
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 30 October 2014, 11:30 PM EDT
Location: NM
Posts: 10,253
Thanks: 285
Thanked 803 Times in 747 Posts
example:

Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

double get_meminfo(void)
{
	double retval=0;
	char tmp[256]={0x0};
	/* note= add a path to meminfo like /usr/bin/meminfo 
	   to match where meminfo lives on your system */
	FILE *shellcommand=popen("meminfo","r");
	while(fgets(tmp,sizeof(tmp),shellcommand)!=NULL)
	{
		if(memcmp(tmp,"Mem:",4)==0)
		{
			int	wordcount=0;
			char *delimiter=" ";
			char *p=strtok(tmp,delimiter);
			while(*p)
			{
				wordcount++;
				if(wordcount==3) retval=atof(p);
			}
		}
	}
	pclose(shellcommand);
	return retval;
}

int	main(int argc, char	*argv[])
{
	double freemem=get_meminfo();
	
	printf("free memory=%f\n", freemem);
	return 0;
}

Sponsored Links
    #6  
Old 02-13-2006
trancedeejay trancedeejay is offline
Registered User
 
Join Date: Feb 2006
Last Activity: 6 March 2006, 6:18 AM EST
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
I've noticed that meminfo does not display real values - function free -m does. If I insert to memtester the same value that I got from free -m memtester works normally, but when I put only 1MB higher number memtester terminates - not enough memory(so that means the function in shell free -m displays real free memory data). Question now is how to enter the shell function from C to save and display data from free -m?
Have a nice Day

Matt
Sponsored Links
    #7  
Old 02-13-2006
trancedeejay trancedeejay is offline
Registered User
 
Join Date: Feb 2006
Last Activity: 6 March 2006, 6:18 AM EST
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
HI
I've tried your program but I have got premission denied:

sh: /proc/meminfo: Permission denied
free memory=0.000000

When you wrote the path "meminfo", I entered whole path "/proc/meminfo"
But that is not the question now as meminfo does not display real data.

Have a nice Day

Matt
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
free memory in debian linux lhareigh890 UNIX for Advanced & Expert Users 1 07-01-2011 01:08 PM
how to get the more memory free space (see memory free column) murthy76 Solaris 3 04-21-2009 09:34 AM
determining vxdg free space itik Linux 0 12-23-2008 06:19 PM
Linux Server free memory decreases Vaibhav Agarwal Linux 13 12-18-2008 09:15 AM
Free Linux Memory by Dropping Caches Neo Linux 0 11-29-2008 10:29 AM



All times are GMT -4. The time now is 08:08 AM.