News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Garbage collection in Assembly

Started by jj2007, February 21, 2023, 01:11:12 AM

Previous topic - Next topic

jj2007

Quora:
QuoteA garbage collector works by scanning the memory address space and identifying memory addresses that were previously allocated but are no longer "reachable", meaning no other memory addresses contain a reference to the unreachable memory.

Wiki:
QuoteThe garbage collector attempts to reclaim memory which was allocated by the program, but is no longer referenced
...
Tracing garbage collection is the most common type of garbage collection, so much so that "garbage collection" often refers to tracing garbage collection, rather than other methods such as reference counting. The overall strategy consists of determining which objects should be garbage collected by tracing which objects are reachable by a chain of references from certain root objects, and considering the rest as garbage and collecting them.

So, how relevant is all this for us?

I've never used mulltiple references to a single memory area. What would be the advantage?

In MasmBasic...
Let my$="Hello"
Let my$=my$+" World"
... is a perfectly valid sequence, which includes de- and reallocation of my$. A simple Clr$ my$ would make my$ disappear from the process space. Is that "garbage collection"? I guess it's not, but I am not sure.

How could garbage possibly appear in Assembly?

Forum search does not find any significant discussion of GC, that's why I am asking your opinions.

Siekmanski

Not sure but, sounds to me as finding memory leaks.

> I've never used multiple references to a single memory area. What would be the advantage?

Multi-threading using the same memory area?
Creative coders use backward thinking techniques as a strategy.

jj2007

Quote from: Siekmanski on February 21, 2023, 01:41:13 AM
Not sure but, sounds to me as finding memory leaks.

> I've never used multiple references to a single memory area. What would be the advantage?

Multi-threading using the same memory area?

Typically, I would use a global variable. If one of the threads says "don't need the area any more: Clr$ my$", then my$ will be set to zero, so that other threads know it's no longer valid. How would you do that?

FORTRANS

Hi.

Quote from: jj2007 on February 21, 2023, 01:11:12 AM
I've never used mulltiple references to a single memory area. What would be the advantage?

   Would multiple calls to a function or subroutine be an example?
A call references the memory it is "jumping" to.  Loading a DLL
might be another example if used by more than one program.
When all programs using the DLL have exited, then it can be
unloaded by a garbage collector.

Regards,

Steve N.

jj2007

Quote from: FORTRANS on February 21, 2023, 02:27:47 AM
Quote from: jj2007 on February 21, 2023, 01:11:12 AM
I've never used multiple references to a single memory area. What would be the advantage?

   Would multiple calls to a function or subroutine be an example?
A call references the memory it is "jumping" to.

What I've understood is that they are talking about freeing heapalloc'ed memory. That's a different kind of reference.

QuoteLoading a DLL might be another example if used by more than one program.
When all programs using the DLL have exited, then it can be unloaded by a garbage collector.

Valid example. One might use a reference count. However, if I really saw a need to unload a DLL, I would know when and where to do it, manually with FreeLibrary.

hutch--

Its something I have usually seen with dialects of basic and if you have a start and end as in a normal procedure, you have an array of pointers and for each array member, you have dynamic allocation of whatever memory amount is required.

On the up side, the assembler/compiler counts the number of strings, allocates an array of that count then as required, each array member is a handle to any allocation required.

At the exit end, the array is scanned and for each member, it deallocates that memory and when that is completed, the pointer array is deallocated as well. The exit end is the garbage collection.

daydreamer

Isn't gc good for java programmer to migrate to non- gc languages and newbies before learn tidy up with delete objects,free memory allocation?
my none asm creations
https://masm32.com/board/index.php?topic=6937.msg74303#msg74303
I am an Invoker
"An Invoker is a mage who specializes in the manipulation of raw and elemental energies."
Like SIMD coding

LiaoMi

Hi jj2007,

Simple, zero-dependency garbage collection for C - https://github.com/mkirchner/gc
A basic garbage collection application written in C to manage memory - https://github.com/briancain/garbage-collector

An implementation of Cheney style garbage collection for cons cells and atomic data. Heap resizing is also permitted either explicitly and/or automatically - https://github.com/rrozansk/Cheney-GC
https://en.wikipedia.org/wiki/Tagged_pointer
https://en.wikipedia.org/wiki/Cons
https://en.wikipedia.org/wiki/Cheney%27s_algorithm

A Collection library for C with garbage collection - https://github.com/ksmandersen/CCollections
CCollections is a general purpose collection library for the ANSI C programming language. It features a variety of commonly used data structures like Lists, Sets, Dictionaries and Trees. Data structures are garbage collected and has features like sorting, enumeration, filtering and mapping.

cell allocator with hard-realtime garbage collection AND DEDUPLICATION - https://github.com/nmushegian/celloc
Garbage collection for C - https://github.com/unveres/ezgc

Richard Jones, Rafael D Lins - Garbage Collection Algorithms for Automatic Dynamic Memory Management (1996, Wiley)
(Linkoping Studies in Science and Technology) Tobias Ritzau - [Dissertation] Memory Efficient Hard Real-Time Garbage Collection
(Chapman & Hall_CRC Applied Algorithms and Data Structures series) Richard Jones Antony Hosking Eliot Moss - The Garbage Collection Handbook The Art of Automatic Memory Management

jj2007

Hi LiaoMi,

That's an impressive list, but can you explain in a few words what they actually do? In Assembly speak?

NoCforMe

Yes, and another question: what do you need to do in order to have your data garbage-collected with these schemes? Use malloc()? HeapAlloc()? some other magic incantations?
Assembly language programming should be fun. That's why I do it.

daydreamer

but if you use JAVA alot you are spoiled by gc and nullpointerexception and go from JAVA to c/asm lowlevel without gc,you can end up like me cpp which works for a short while,but WM_PAINT stops working properly because I constantly created GDI resources without deleteobject:(

more useful and important for asm newbies would be create a nullpointer exception handler to make the exception handler popup tell you have null pointer exception, instead of complete windows crash
my none asm creations
https://masm32.com/board/index.php?topic=6937.msg74303#msg74303
I am an Invoker
"An Invoker is a mage who specializes in the manipulation of raw and elemental energies."
Like SIMD coding

jj2007

I found a nice description here - and it looks like garbage collection is garbage that should be collected and disposed of :cool:

Quote[Without garbage collection] you might say:

int *x = malloc ( 100 ) ;  // Give me 100 bytes of memory
...
free ( x ) ;   // Give it back.
There are MANY ways in which programmers can make mistakes with this simplistic approach.

One is to forget that 'x' has been free'd up and continuing to use it. You can kinda protect yourself a bit by setting x = NULL ; right after you free it.

Another is to forget to free(x) - and to just set it to NULL instead. Then the memory block is lost forever because it's still allocated to you - but you've forgotten where it is because you overrode 'x'.

I doubt that any serious assembly programmer forgets that 'x' has been free'd up and continues to use it...

However, the fact that GC exists supports my view that being a hobby programmer is a privilege. Those who code for money must experience extreme pressure if they commit such blunt errors. Plus, it explains why there are so many awful bugs in professional software.

mineiro

hello sir jj2007;
Translate link bellow to your language. GC starts at paragraph 6 to end of text.
The guy that wrote that have a lot of skills, he is an assembly programmer, book writer, ... .
Basically he talks about 2 GC methods (reference counter and eden/elder(new/old) in an easy way to understand.
https://bitismyth.wordpress.com/2015/05/30/sera-que-minha-birra-com-java-e-justificada/
I'd rather be this ambulant metamorphosis than to have that old opinion about everything

NoCforMe

Quote from: jj2007 on April 11, 2023, 08:20:12 PM
I found a nice description here - and it looks like garbage collection is garbage that should be collected and disposed of :cool:

@JJ, after reading your comment and thinking about it, you've won me over to your POV regarding GC.

Basically, it's a crock of shit, for lazy-ass programmers who can't be bothered with trivial "details" like what memory is allocated. Any decent programmer knows that they need to keep track of lots of things: program states, flag settings, execution order, and yes, memory allocation. Apparently garbage-collection enthusiasts want The Programming Environment to take care of that for them without having to think about it (like all that fancy-schmancy stuff in C++) so they can think about More Important Things ...

Allocate memory. Use it. When no longer needed, deallocate it. You shouldn't even need to do anything stupid like NULL-out pointers: just be aware of whether those blocks of memory are allocated or not. (Otherwise you're using program exceptions as a crutch.)
Assembly language programming should be fun. That's why I do it.