News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

BSS Segment or Allocate?

Started by theunknownguy, March 16, 2010, 07:05:46 AM

Previous topic - Next topic

theunknownguy

I have a problem my friends, i am in a doubt by trying to allocate space. The problem (is not a real problem more like a question) if what is better?.

Allocate memory or implicit declare the space by BSS segment.

Ill see some benefits on declaring in BSS segment in the way i dont have to call the initialiser procedures for each one of the unitialised structures.
I use alot of structures that use pretty much alot of size like 26400000 (and initialise it by a loop), so when it comes to do by BSS i just save a few clocks by avoiding use HeapAlloc or VirtualMemory.

But anybody know if its better to use any kind of allocation method instead of just declarin?
Also this affect in the performance of the code?

Thanks !

dedndave

i guess it depends on whether or not you intend to keep those structures throughout the remainder of the program
keep in mind that if you reduce the memory used, other programs may run faster, allowing yours to also
it probably assembles faster to use dynamic allocation, but that shouldn't really be a primary concern

redskull

Depending on the program, DATA segment declarations can often impose hardcoded limits which don't scale correctly and waste space; if you allocate room for 10 'widgets', then your program can only operate on 10 widgets at a time and wastes the space if the user only wants 1.  By using HeapAlloc (in some sort of loop), your program is limited only by system memory.  Obviously, a lot depends on the function of the program.

-r
Strange women, lying in ponds, distributing swords, is no basis for a system of government

theunknownguy

Thanks for the answer, i lack to say its for a game, and all the structs (all the variables) are initialised and used for a very long time, even days, they can only be destruct when game close...
So there isnt like user want to use 1 or 2, they have to use them all...

Saying this, still better allocate or BSS?

jj2007

> size like 26400000

You can use the .data? segment, but a db 26400000 dup(?) will freeze Masm eternallly. Either you use the method below, or, much simpler
mov MyBuf, alloc(26400000)
....
free MyBuf


This should work, too.
.data?
align 4
  lbl LABEL byte
  ORG $+BufLen-1
  db ?
.code

theunknownguy

Using JWASM doenst freeze... i dont like ML compiler for this and many other things.

And the loop initialisers are from example 600 times allocing 6000 of space... so by doing BSS i save some clocks and leave the code alot more clean...

jj2007

Quote from: theunknownguy on March 16, 2010, 05:22:02 PM
Using JWASM doenst freeze... i dont like MASM32 for this and many other things.
Just for the sake of clarity: Masm32 is a private almost bug-free library maintained by Hutch with some support from forum members; Masm is the buggy freezing program originating from a major software company ;-)

theunknownguy

Sorry, my mistake the ml compiler is the bugged ^^ (lack of expression)

Still cant find a real reason to switch to memory allocation... i guess for this project BSS segment its just fantastic.


PS: Also i cant find much info about what happen to the BSS segment once i close the program, i assume that OS will find it wasy to clean, but there isnt a "way" to make things easier for the OS, example initialise to 0 all the BSS segment once i want to end the program? meaby i am wrong... just an idea...

dedndave

the OS tends to clear out the area before handing it to another program
unless you have some kind of secure info in there, you can ignore what happens to it
of course, you can always clear it yourself

hutch--

theunknownguy,

If you need to do anything serious where you need memory, allocate it, the .DATA? section is not really well suited for large amounts of memory as it remains allocated by the OS until the program closes. Learn to use the dynamic memory allocation strategies in Windows and you can use up to the amount of available memory in the machine.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Astro

I've noticed an interesting behavior in that even if you forcibly terminate an app that used dynamic allocation, Windows frees the memory anyway. I've noticed this behavior with the Heap* functions in the current process heap. I haven't tried creating a seperate heap and using that, but I've gone crazy allocating 2 Gb of RAM at a time, and it doesn't leak even if I try. This does not absolve you of correctly freeing memory after use, but intriguing nonetheless. I haven't looked deeply into why it does this yet (but it appears Windows is tracking my memory allocations and freeing it if it sees my app terminate without freeing them first).

Note that you can still leak memory by allocating but not freeing memory during the lifetime of the app, so if your app runs for weeks it can still kill the machine!

Best regards,
Robin.

BlackVortex

Is that strange ? Of course Windows cleans up after terminated processes as much as possible.  :bg

hutch--

Robin,

You missed the joys of writing Win3.0 series softare, it was a software multitasking system that was effectively one big running program and if you did not accurately de-allocate memory, eventually you brought down the whole system. With modern pre-emptive multitasking the OS tracks threads and if they are closed or crash  the OS cleans up any mess. A single 32 bit Windows thread does what all of win3.0 did without proper threads and where the OS acted like one app that task switched in software to run more than one app at a time.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

theunknownguy

Quote from: hutch-- on March 17, 2010, 12:12:41 AM
theunknownguy,

If you need to do anything serious where you need memory, allocate it, the .DATA? section is not really well suited for large amounts of memory as it remains allocated by the OS until the program closes. Learn to use the dynamic memory allocation strategies in Windows and you can use up to the amount of available memory in the machine.

Thanks hutch, but if the data i need its used till the end of execution?

Meaning that, i ofcourse use memory allocation when i need. Using it for a while like reading a file or any other thing, but on a struct that is initialised in the begin of the program and runs till the end of it?...

I mostly dont care if its BSS or allocated, but i care about the process of initialised them, i try to optimise the most i can and there is a big difference between calling 2000 times heapalloc and dont call it...

PS: Thanks for the answers of everyone i love this community (always with so much knowledge)

redskull

Quote from: theunknownguy on March 17, 2010, 05:07:00 PM
...and there is a big difference between calling 2000 times heapalloc and dont call it...

Actually, less than you'd think.  Without getting into the details, a DATA? section essentially just instructs the loader to allocate the memory for you during loading; so yes, your program may run faster, but it will take longer to load.  Similarly, no memory is actually "created" until you access it for the first time.  So whether it gets allocated by you using HeapAlloc or by the system in a DATA? section, most of the slowdown occurs the first time you write to it.  The only real optimization you get are the negligable few bytes in the EXE it takes to call the HeapAlloc function.

-r
Strange women, lying in ponds, distributing swords, is no basis for a system of government