PDA

View Full Version : Amount of memory used



dco045
16-01-2018, 01:45
Hi,

Two questions :

First=
After writing test programs, tested them in thinair, then bundled them, I would like to know the memory impact
of the .exe generated both code size and data size.
I can get such info with tools like sysinternals process explorer.
But I also wish to use data size inside the program.

Second=
In a previous life, I used Unix computers (From Digital Equipments, Decstations running Ultrix32) and I was
relatively fluent with C coding and usig syscall from manpages chapter2 and fuctions from chapter 3.
Can I find in thinbasic help or forums or ...... a guide bringing a parallel between C-unix-like functions and ThinBasic functions

. . . . . . . . . . . . . . . . . . Unix C . . . . . . . . . . . . . . . . . . . . . . . Thin basic
exmpl : . . . . . . . . . . handle=fopen(string,"rw",.... -->> . . . . . . . . . . . . . . . ?
. . . . . . . . . . . . . . . strcpy(str1,str2) . . . . . . . . . . . . . . . -->> . . . . . . . . . . . . . . . this is a bad example str2=str1
But I sure you understand my wish :)


Thanks for help.

Dany

Petr Schreiber
16-01-2018, 09:08
Hi Dany,

as for files, have a look at the File_* functions in the help file. There is File_Open for your fopen like uses.

For strcpy alternative, there are many options.
I would suggest to look at Heap_* and Memory_* functions.

To copy string, you can actually assign it the way you did.
Have a look at varPtr and strPtr and what it does with the assignment.

Petr

dco045
16-01-2018, 12:10
Hi Petr,

Thanks for answering.

As I mentioned the copy string was a bad example, it's so easy in *basic*.

My question was more general, since I have a collection of programs/utilities I wrote during
my professional activity, Most writtent in K&R C, and some others in C-shell, Bourne-shell.....

And a parallel view between Unix / ThinBasic would greatly simplify my adaptations.
I apologize : I AM LAZY :)

For program / data size, I guess I have some long time to spend, looking for this info.

Regards,

Dany

Petr Schreiber
18-01-2018, 18:43
Hi Dany,

thanks for the explanation :)

If you want to make the best out of thinBASIC - please do not start with attempt to do 1:1 translation from C.

Did you know, you can load whole file with single line of code, for example?:


String content = Load_File("c:\myFile.txt")


ThinBASIC is a different kind of beast. You can dive very low level, if you need.

...but you can also enjoy high level goodies, which will make your code much more maintainable/readable than C.
For example thinBasic UDT (user defined types, like struct), which can have custom functions allowing neat object design.


Petr

dco045
19-01-2018, 01:26
Hi Dany,

thanks for the explanation :)

If you want to make the best out of thinBASIC - please do not start with attempt to do 1:1 translation from C.

Did you know, you can load whole file with single line of code, for example?:


String content = Load_File("c:\myFile.txt")


ThinBASIC is a different kind of beast. You can dive very low level, if you need.

...but you can also enjoy high level goodies, which will make your code much more maintainable/readable than C.
For example thinBasic UDT (user defined types, like struct), which can have custom functions allowing neat object design.


Petr

Hi Petr,

I agree, the goal is NOT to translate 1 for 1.

But I guess such a parallel would permit to emphasise how ThinBasic could simplify programs.
The list may state that a C function named 'any_work(3)' may be replaced by ThinBasic function name "another_way_to_get_the_same_result() "
As I say in previous post: "I am lazy" and if such a list exists, it would speed searches.

An example : the program I am working on, gathers data in files in a shared directory and reports results in a console.
But currently I'm looking how to print results in a way like old basics do with the semicolon terminator (no auto end of line).

for x= 1 to x

(gather data)
(format data in a word)
print x ; word$ ;
next x

The way could be : building a string word by word with "&" operator, and then prinltl(constructed_string).
BUT I need that each word appears when detected, not when 20 words are gathered.

Another, point :
In C, the function main() is called from shell with commend line arguments

main(argc,*argv)

When I was looking in directory SampleScripts I saw someting like, but now, I do not remember which sample,
and the directory contains 198 directories and 796 files. :shock28: :tears:


You say dive to low lever :p:p good new : I am diver CMAS:*** :cool::cool:

Regards


Dany

Petr Schreiber
19-01-2018, 09:57
Hi Dany,

these cases are simple ones :)

Print does print to console without line break, PrintL does the same + line break.

As for the command line parameters - please have a look at OS_Command* and OS_GetCommand* range of functions in the help file.


I hope it helps,
Petr

dco045
19-01-2018, 12:27
Thanks Petr,


Printl / print : It was so bright , my eyes were dazzled . :cool: :cool: :cool:

Os_commands : ok


Dany

ErosOlmi
20-01-2018, 13:16
Ciao Dany,

making a C to Basic (thinBasic) translation or parallel is quite a huge work if one want to do it professionally and honestly I do not have the time.
Instead ... maybe we can help more if you ask about the tasks you want to achieve so we can help/suggest something on how to achieve that tasks using thinBasic

Regarding Files, thinBasic has a specific module called "File" that you can load using:

Uses "File"
at the beginning of your code and you will have some file/directory specific functions in your script arsenal: http://www.thinbasic.com/public/products/thinBasic/help/html/fileequates.htm
Than check inside \thinBasic\SampleScripts\File\ directory and you should find some examples on how to start.

On memory, I come from old 640KB DOS era when looking at how much memory your program was consuming was a must do task.
In 32bit process (like thinBasic scripts) you have 2GB virtual memory for each process so you do not have to worry about memory unless you really need to load quite big piece of data to be handled at the same time.
In any case if you want to see how to get memory info of current process, you can use the following script. It uses 2 Windows API functions GetProcessMemoryInfo and GetCurrentProcess.

Ciao
Eros



Uses "Console"

'---References---------------------------------------
' https://msdn.microsoft.com/en-us/library/windows/desktop/ms683219(v=vs.85).aspx
' https://msdn.microsoft.com/en-us/library/windows/desktop/ms683179(v=vs.85).aspx


TYPE PROCESS_MEMORY_COUNTERS DWORD
cb AS DWORD
PageFaultCount AS DWORD
PeakWorkingSetSize AS DWORD
WorkingSetSize AS DWORD
QuotaPeakPagedPoolUsage AS DWORD
QuotaPagedPoolUsage AS DWORD
QuotaPeakNonPagedPoolUsage AS DWORD
QuotaNonPagedPoolUsage AS DWORD
PagefileUsage AS DWORD
PeakPagefileUsage AS DWORD
END TYPE


Declare Function GetProcessMemoryInfo Lib "PsApi.dll" Alias "GetProcessMemoryInfo" (BYVAL hProcess AS DWORD, ppsmemCounters AS PROCESS_MEMORY_COUNTERS, BYVAL cb AS DWORD) AS LONG
Declare Function GetCurrentProcess Lib "Kernel32.dll" Alias "GetCurrentProcess" () as Long
'-------------------------------------------------------

'---Get memory usage of current process
FUNCTION MemoryInfo(s) AS STRING
LOCAL sStat AS PROCESS_MEMORY_COUNTERS
local sBuffer as String


GetProcessMemoryInfo (GetCurrentProcess, sStat, SizeOf(sStat))


sBuffer = "Page fault count................." & $TAB & RSET$(FORMAT$(sStat.PageFaultCount, "0,"), 15) & $CRLF
sBuffer += "Peak working set size............" & $TAB & RSET$(FORMAT$(sStat.PeakWorkingSetSize, "0,"), 15) & $CRLF
sBuffer += "Current working set size........." & $TAB & RSET$(FORMAT$(sStat.WorkingSetSize, "0,"), 15) & $CRLF
sBuffer += "Quota peak paged pool Usage......" & $TAB & RSET$(FORMAT$(sStat.QuotaPeakPagedPoolUsage, "0,"), 15) & $CRLF
sBuffer += "Quota paged pool usage..........." & $TAB & RSET$(FORMAT$(sStat.QuotaPagedPoolUsage, "0,"), 15) & $CRLF
sBuffer += "Quota peak non paged pool usage.." & $TAB & RSET$(FORMAT$(sStat.QuotaPeakNonPagedPoolUsage, "0,"), 15) & $CRLF
sBuffer += "Quota non paged pool usage......." & $TAB & RSET$(FORMAT$(sStat.QuotaNonPagedPoolUsage, "0,"), 15) & $CRLF
sBuffer += "Peak page fileUsage.............." & $TAB & RSET$(FORMAT$(sStat.PeakPageFileUsage, "0,"), 15) & $CRLF
sBuffer += "Page file usage.................." & $TAB & RSET$(FORMAT$(sStat.PageFileUsage, "0,"), 15) & $CRLF

function = sBuffer

END FUNCTION

FUNCTION TBMain () AS LONG
dim WasteMem as String


'---Loop while user press ESC key
Do
Cls
'---Allocate some random memory from 1M to 10M
WasteMem = string$(rnd(1000000, 10000000), $nul)


'---Call function that get memory usage and print data
printl MemoryInfo()


printl "Press any key to show again memory usare or ESC to exit" in %CCOLOR_FINTENSEWHITE


loop while WaitKey <> "[ESC]"


END FUNCTION

dco045
21-01-2018, 21:23
Ciao Dany,

making a C to Basic (thinBasic) translation or parallel is quite a huge work if one want to do it professionally and honestly I do not have the time.
Instead ... maybe we can help more if you ask about the tasks you want to achieve so we can help/suggest something on how to achieve that tasks using thinBasic


As I said to Petr in a previous reply, my goal is NOT to translate old C progs to ThinBasic. But after a 30 years use of Unix/C, I have some automatisms, which I should, of course, correct.
So when I have to read data from a file I think :
handle = fopen(filename;"R");
While ( count = fread(handle) etc etc etc.... :heat:

instead of FileLoad(...)


But now, I spend time with a window opened with ThinAir, and another with ThinBasic help, testing examples from templates.
And learning, learning, learning...... Physicians says that for the health of the brain, one have to make it works. Someones spend time with sudoku or crosswords,
I prefer building electronic circuits, old microprocessors programming. The current ThinBasic project I work on now, is a cross-assembler for RCA COSMAC processor designed in
the 'seventies (Eros was less than 10 years old. :D :D :D )

You say
we can help/suggest something on how to achieve that tasks using thinBasic

I'll do so... Perhaps you will feeel bored by my dumb questions sometimes. :confused: :confused:

Regards,


Dany

dco045
21-01-2018, 21:47
Ciao Dany,

On memory, I come from old 640KB DOS era when looking at how much memory your program was consuming was a must do task.
In 32bit process (like thinBasic scripts) you have 2GB virtual memory for each process so you do not have to worry about memory unless you really need to load quite big piece of data to be handled at the same time.
In any case if you want to see how to get memory info of current process, you can use the following script. It uses 2 Windows API functions GetProcessMemoryInfo and GetCurrentProcess.

Ciao
Eros


Thank for this program.

Why I posted this question ? .
While trying some TBasic programs my PC show me a popup saying Insufficient memory.
And the ThinBasic console did not open. So I guessed something wrong with my tests. Now I know that it was a video conversion program I started hours before
and iconized, going mad and forking sub-processes consuming more and more memory. I killed it, and things returned OK

As I said before, I am old..... My first job in IT departement, in 1985, was system manager of minicomputers VAX-750 from Digital Equipments.
With 4 (four) megabytes of ram, two disks of 200 (two hundred) megabytes, a 1600 bpi tape drive for backups.
They were running Ultrix32 OS, a unix derived from Berkeley 4.2. The loaded kernel was less than 1 megabyte
And they were more than 20 users connected simultaneously. So, the consumed memory was a permanent worry.
And now, I continue looking at these parameters.



Regards,


Dany

ErosOlmi
22-01-2018, 22:03
What a time, what a time.
Sometimes with friends we still try to remember the old times when all the job had to be done with such a limited hardware and power.

Anyway I will be very happy to help you in any doubt or problem or whatever you need using thinBasic for your project.
I'm so honored to know other programmers uses thinBasic for their needs.
And also I will be happy to develop something you need and you do not find in thinBasic Core or modules.