Memory access ? (Solved on 64 bit distro)

General FreeBASIC programming questions.
ppf
Posts: 45
Joined: Oct 10, 2017 6:41

Memory access ? (Solved on 64 bit distro)

Postby ppf » May 23, 2019 20:16

Hi,
just more work with RAM + bigger arrays, unclear thing founded.
Which kind of computer memory is Freebasic able to use ?
I see only "mem" and "xyAllocate" keywords and disproportion between memory stats.
Htop says :
Total 2.2 Giga
Used 680 Mega
Buffers 36 Mega
Cached 1.5 Giga

FB mem after program start says:
170 Mega free

When building big array (cca 600 Mega planned size), program freezes after touching 900 Mega Used memory, by Htop.
It looks like Cached part of memory, 1,5 Giga , is hidden/unaccessible for FB .
Can I access it in some manner ?
[
Last edited by ppf on May 29, 2019 0:09, edited 1 time in total.
srvaldez
Posts: 2105
Joined: Sep 25, 2005 21:54

Re: Memory access ?

Postby srvaldez » May 23, 2019 20:57

try using redim instead of dim
badidea
Posts: 1542
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Memory access ?

Postby badidea » May 23, 2019 21:04

On my (linux) laptop with 8 GB memory, this code runs fine:

Code: Select all

const as longint KB = 1024
const as longint MB = KB * 1024
const as longint GB = MB * 1024

const as longint BLOCK_SIZE = 4 * GB

Print "Free memory: "; fre()  \ MB; " megabytes"
print

print "allocating bytes"
dim as ubyte ptr pBlock = allocate(BLOCK_SIZE)
if pBlock = 0 then print "Error allocate" : end -1
print

Print "Free memory: "; fre()  \ MB; " megabytes"
print

print "filling bytes ..."
for i as longint = 0 to BLOCK_SIZE-1
   pBlock[i] = (i and 255)
next
print

Print "Free memory: "; fre()  \ MB; " megabytes"
print

print "checking bytes ..."
for i as longint = 0 to BLOCK_SIZE-1
   if pBlock[i] <> (i and 255) then print i; "Byte error": exit for
next
print

Print "Free memory: "; fre()  \ MB; " megabytes"
print

print "freeing bytes"
deallocate pBlock
print

Print "Free memory: "; fre()  \ MB; " megabytes"
print

Output:

Code: Select all

Free memory: 3732 megabytes

allocating bytes

Free memory: 3732 megabytes

filling bytes ...

Free memory: 141 megabytes

checking bytes ...

Free memory: 142 megabytes

freeing bytes

Free memory: 4245 megabytes

The only odd thing is that fre() still returns the same value after memory allocation.
fxm
Posts: 9177
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Memory access ?

Postby fxm » May 23, 2019 21:30

'Fre' returns the memory size allocated by the OS at the call time. This size is dynamically readjusted by the OS during program execution.
For fbc 32-bit, maximum allocated memory size < 2 GB.
For fbc 64-bit, maximum allocated memory size < virtual memory.
ppf
Posts: 45
Joined: Oct 10, 2017 6:41

Re: Memory access ?

Postby ppf » May 25, 2019 13:05

Thanks for your replies and code, very helpful!
But my distro seems preconfigured strangely.
I was able to allocate memory for array in amount of 256 Mega only; after additional web search
founded two commands
"sysctl -w vm.drop_caches=3"
"sysctl vm.swappiness=2"
Also added ramdisk of 512 Mega.
After that examinating I finally got to allocate of 512 Mega, but allocate test prog is on edge of hanging.
'Htop' shows both CPU cores on 99%.
'Mem' shows Cached memory downed to 1 Giga only.
This is useless for my real prog.
Switching to 64 bit distro.I want to see my 8Giga RAM completely accessible, in work.
ppf
Posts: 45
Joined: Oct 10, 2017 6:41

Re: Memory access ?

Postby ppf » May 27, 2019 1:56

Well, currently on 64 bit distro.
'free' says 5.7 Giga RAM available. OK.

But compiled prog doesn't work.
Where are FAQ , hints, other dcs etc related to 64 bit Freebasic compiling ?
Ldd says missing libraries, but all is installed in 64 bit version.
Seems like 64 bit fbc tries to compile source as 32 bit default target, I don't understand, what is wrong.
Could somebody navigate me to the light in the darkness ?
MrSwiss
Posts: 3257
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Memory access ?

Postby MrSwiss » May 27, 2019 12:11

ppf wrote:Ldd says missing libraries, but all is installed in 64 bit version.

You probably meed to download the 64 bit libraries (NOT included, just the .bi files).
See: External Libraries Index

ppf wrote:Seems like 64 bit fbc tries to compile source as 32 bit default target, I don't understand, what is wrong.

No, its only "running" on 64 bit HW/OS and "producing" 64 bit binaries.
ppf
Posts: 45
Joined: Oct 10, 2017 6:41

Re: Memory access ?

Postby ppf » May 27, 2019 16:58

Ahh, you are right ! Completely forgot on additional libs of 3rd party.
Used 'libpng' for beauty intro image, sGui for Messagebox and C function 'fflush'.
Firstly it 'll be commented out, to see what happened next.
Much appreciated advices, thank you again.
badidea
Posts: 1542
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Memory access ?

Postby badidea » May 27, 2019 17:09

For the 'default' stuff that FBC needs, see: https://www.freebasic.net/wiki/wikka.ph ... Installing
ppf
Posts: 45
Joined: Oct 10, 2017 6:41

Re: Memory access ?

Postby ppf » May 27, 2019 21:57

Yes, founded&compared with yum.Thank you.

Now, uff, it works.Seeing on ldd output, it searches libs in /lib/ folder.
So added 32 bit version libX randr render pm.., compiled and works in quick test.
Sorts seems a bit slower, then on old 32 bit distro.No changes in sources.

IMO I have 32 bit app compiled on 64 bit distro, by default.
But why ?
Now is time to check accessible memory, big arrays&files read/write and speed.
Forwarding ;
badidea
Posts: 1542
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Memory access ?

Postby badidea » May 27, 2019 22:13

ppf wrote:IMO I have 32 bit app compiled on 64 bit distro, by default. But why ?

Are you sure you installed 64-bit FBC?
I have both installed (for testing). Renamed them after installing to fbc32 & fbc64:

badidea@laptop:~$ fbc32 --version
FreeBASIC Compiler - Version 1.05.0 (01-31-2016), built for linux-x86 (32bit)
Copyright (C) 2004-2016 The FreeBASIC development team.

badidea@laptop:~$ fbc64 --version
FreeBASIC Compiler - Version 1.05.0 (01-31-2016), built for linux-x86_64 (64bit)
Copyright (C) 2004-2016 The FreeBASIC development team.


I see have to update to the latest version I see.
ppf
Posts: 45
Joined: Oct 10, 2017 6:41

Re: Memory access ?

Postby ppf » May 29, 2019 0:08

Yes, I have fbc 64 bit installed.
After searchin, founded this compiler parameter - 'fbc -arch 64...'.
Then correctly identified bad 32 bit libs and code in compiled sources.
After correction, compiled Ok.Now Ldd shows searchin in /lib64/ as awaited.
Memory is Ok too.
Looks good !

Return to “General”

Who is online

Users browsing this forum: No registered users and 24 guests