[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference 7.286::atarist

Title:Atari ST, TT, & Falcon
Notice:Please read note 1.0 and its replies before posting!
Moderator:FUNYET::ANDERSON
Created:Mon Apr 04 1988
Last Modified:Tue May 06 1997
Last Successful Update:Fri Jun 06 1997
Number of topics:1433
Total number of notes:10312

1264.0. "How do I obtain the free byte count?" by AIDEV::MISKINIS () Wed Mar 11 1992 01:02

Hi,

	Does anyone know how to obtain the amount of free memory
	on the system?

	(From within an application)

Thanks!

_John_
T.RTitleUserPersonal
Name
DateLines
1264.1To tell you or yuor application?TIMMII::RDAVIESAn expert AmateurWed Mar 11 1992 07:216
    I use the freeram accessory but that just displays on screen... But I
    guess you are trying to fing an internal call-back to provide this
    value to your application?. Or am I wrong?
    
    Richard
    
1264.2SMURF::COUTUHe who will not risk, cannot win.Wed Mar 11 1992 13:1718
    Two methods come to mind. Experiment for best fit. Both involve
    using the malloc() call.
    
    One: from your application call malloc(4194304) to attempt grabbing
    all 4 megabytes of possible memory. This is sure to fail but many
    C compilers will return the amount of memory actually available to
    you, which happens to be the number of free bytes left.
    
    If you're using Assembler (or a C compiler that supports it) then you
    can use the malloc gemdos call with a -1 value to get a report
    of free bytes. The sample assembler code from my ST Internals book is:
    
    move.l #-1,-(sp)
    move.w #$48,-(sp)
    trap #1
    addq.l #6,sp	Number of free bytes is in D0
    
    Dan
1264.3total or size of largest chunk?VFOVAX::PATTERSONThe world is flat, it's the universe thats roundWed Mar 11 1992 15:1313
>    One: from your application call malloc(4194304) to attempt grabbing
>    all 4 megabytes of possible memory. This is sure to fail but many
>    C compilers will return the amount of memory actually available to
>    you, which happens to be the number of free bytes left.
    
>    If you're using Assembler (or a C compiler that supports it) then you
>    can use the malloc gemdos call with a -1 value to get a report
>    of free bytes. The sample assembler code from my ST Internals book is:

    Will this give you the amount of free memory, or the size of the largest
    chunk of free memory?

    Jim Patterson
1264.4The official wayPRNSYS::LOMICKAJJeffrey A. LomickaWed Mar 11 1992 16:4921
The proper way to determine the free memory in the system is to call the
GEMDOS routine Malloc( -1).  Note that in Mark Williams C that "Malloc" is
very different from "malloc".  Again, Malloc( -1) might just be the size
of the largest free segment, I would have to double check the
documentation to be sure.

The problem with this is that if you are using Mark Williams malloc and
free routines you don't know how much memory the run-time library has
allocated from the operating system but not yet delivered to your
application.  I got around that by writing my own malloc() and free()
pakage which give me much more control over memory and allows me to
generate accurate statsitics.  If you look in the "memory usage" form of
the new Whack, you will see what I mean.  You get numbers for "free
memory" and "unallocated memory" as separate numbers.

BTW, using 4MB as the upper limit is not good enough.  It is common for
TT's to have 8MB, and using Dave Small's latest SST gadget, you can
turn a lowly Mega-2 into a *power user* Mega-12 (with a 40Mhz 68030 for
a CPU as well, and no wait states on the upper 8MB!  That computer must
FLY when running software that was meant for a lowly 8Mhz 512K 520ST.)

1264.5Some supporting code from Allan PrattYNGSTR::WALLACEWed Mar 11 1992 18:4081
Here's what Allan Pratt (of Atari) had to say about free memory when the
question came up on Usenet a few years ago, includes a example code.

	Ray

From: apratt@atari.UUCP (Allan Pratt)
Newsgroups: comp.sys.atari.st
Subject: Re: Malloc - what it really does (was: Re: SPUFILE 2.0, C questions)
Date: 23 Oct 89 19:29:10 GMT
Organization: Atari Corp., Sunnyvale CA
 
klute%trillian.irb@unido.uucp (Rainer Klute) writes:
>From what I have found out Malloc (-1L) returns the *sum* of the
>sizes of *all* free memory chunks.
 
This is emphatically not the case, and never has been.
 
The original answer was right: it returns the size of the LARGEST
SINGLE CHUNK of free memory.  A common way to find out how much memory
there is in the system is to call Malloc(-1L) many times, each time
allocating a chunk of the size it returns, until it returns zero.
Then you report the answer and Mfree() all the chunks you allocated.
 
Of course, Malloc(-1) is evil if you're in a multi-tasking environment,
because in the time between the Malloc(-1) and actually allocating the
block of the size it reported, you could be preempted and somebody else
might allocate that block away from you!  This is only one of a whole
class of problems which a multitasking TOS would have to address: the
single-thread, global, I-own-the-machine nature of TOS applications.
 
============================================
Opinions expressed above do not necessarily	-- Allan Pratt, Atari Corp.
reflect those of Atari Corp. or anyone else.	  ...ames!atari!apratt

From: apratt@atari.UUCP (Allan Pratt)
Newsgroups: comp.sys.atari.st
Subject: Re: Malloc(-1L), free memory...
Date: 30 Oct 89 20:37:35 GMT
Organization: Atari Corp., Sunnyvale CA
 
Well, in a large-memory machine, there can be a LOT of 4K blocks
around.  This makes for a lot of recursion.
 
Re-think this problem as a loop. Use the first long of each block as a
link to the previous block.  Keep Malloc'ing until you run out, then
free up the list.  Like this:
 
long freemem()
{
    long size, total;
    long *start;
    long *prev;

    prev = 0L;
    total = 0L;
 
    do {
	size = Malloc(-1L);
	if (size) {
	    total += size;
	    start = Malloc(size);
	    if (size < sizeof(char *)) {
		/* end of map: too small to keep track */
		Mfree(start);
		break;
	    }
	    *start = (long)prev;
	    prev = start;
	}
    } while (size);
 
    while (prev) {
	start = *prev;
	Mfree(prev);
	prev = start;
    }
}
 
============================================
Opinions expressed above do not necessarily	-- Allan Pratt, Atari Corp.
reflect those of Atari Corp. or anyone else.	  ...ames!atari!apratt