Refesh free memory problem

DOS specific questions.
Post Reply
Cpcdos
Posts: 207
Joined: Mar 06, 2013 13:52
Location: France - LYON 69003
Contact:

Refesh free memory problem

Post by Cpcdos »

Hi,

I use this code :

Code: Select all

print "Bebore:" & fre(0)
Results = MyInstance.MyCfunction(blabla)
print "After:" & fre(0)
MyCfunction:

Code: Select all

void MyCfunction(blabla){
    int * value;
    value = malloc(123); // "123" Is an example
    free(value);
    printf("Finish!");
}
Results :
Bebore:1118208
Finish!
After:1380352
(The memory number is not exact, it's an example)

Why fre(0) do not refresh memory correctly?
You have another method?

Regards
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Refesh free memory problem

Post by MrSwiss »

Cpcdos wrote:

Code: Select all

print "Bebore:" & fre(0)
Results = MyInstance.MyCfunction(blabla)
print "After:" & fre(0)
You don't assign anything NEW to fre(0), e.g.:

Code: Select all

fre(0) = MyInstance.MyCfunction(blabla)
or

Code: Select all

print "After:" & Results
marcov
Posts: 3462
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: Refesh free memory problem

Post by marcov »

fre(0) seems to defined against allocate/deallocate, not C functions.

In the case that FB's memory manager is not 1:1 the C one, this will thus fail.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Refesh free memory problem

Post by fxm »

The 'Fre()' FB keyword does not refresh the memory.
The 'Fre()' FB keyword returns the amount (in bytes) of available or unused dynamic memory which is allocated at this time by the OS.

I think that the quantity of dynamic memory allocated by the OS is not constant during the program execution. The OS try to regulate the available dynamic memory quantity for the program:
- When the program uses some quantity of allocated dynamic memory, the OS may allocate new memory quantity for the program, in asynchronous way.
- Inversely when the program frees some quantity of dynamic memory, the OS may reclaim some memory, always in asynchronous way.

This is why the variation of the value returned by the 'Fre()' FB keyword does not necessarily follow the variation of the dynamic memory quantity used by the program.
dkl
Site Admin
Posts: 3235
Joined: Jul 28, 2005 14:45
Location: Germany

Re: Refesh free memory problem

Post by dkl »

Maybe the CRT (malloc/free) keeps some pages alive in memory, as a pool for later malloc calls?

Or maybe, it could be FB rtlib internal memory allocations for temporary strings (such as those created by the & operator).
Cpcdos
Posts: 207
Joined: Mar 06, 2013 13:52
Location: France - LYON 69003
Contact:

Re: Refesh free memory problem

Post by Cpcdos »

Hi,
Thank you for your replies, I don't know how fre() work, if I execute this code on a loop, the memory number indicated by fre() increases more in more

@MrSwiss Sorry, please do not take account "Results = "

I think that the best solution for calculate free ram space without use "the operating system", is to use malloc (with big allocations) on a loop, and freeing the memory (getting the maximum byte number) when malloc return an exception (memory full) This take more cpu time?

Or create my own memory manager with my own "malloc/free" for freebasic and own "malloc/free" for C code, and increase / decrease the memory number when this functions was used

A good alternative to fre() ? What is the best ?

Regards
Post Reply