The GC will not just fire randomly. If your interrupt doesn’t allocate objects then GC will never fire. Actually, you may write your entire project in a way where GC will almost never need to fire…but this may over complicate your code.
The GC fires when there is garbage to collect. If you don’t create garbage, it won’t collect anything. You can use a pre-allocated buffer for your serial input instead of creating one in the interrupt for beginners.
BTW, it won’t fire every time, it will fire when it determines that there are enough dead references to memory that it needs to clean up. Your other code can have just as much as an effect on this as your interrupt code.
This is bad as you are allocating memory inside your interrupt…which can trigger the GC. Not only that, the object is discarded when interrupt is done (free memory)…this means, a 1000 interrupts/sec will give 1000 object begin allocated and freed every second!
It’s not a good idea to create an object in the interrupts methods.
If I has understand correctly, when an object is not used for a time ( x ms ?) the GC fire this object. In the case of the interrupt the CG use his time plus the time of the interrupt. (not really efficient !)
I will change the RS232 interrupt with create a buffer that memorize the received characters. Perhaps have you an example ?
My question is perhaps not really intelligent, but I suppose the CG clear the unused variables too ?
In this case what’s the difference in memory between an object and a unused variables ?
If you have unused variables the compiler will get rid of them…
The GC is really a reference counter more than a time one. If you don’t have any more references to an object you created, the GC will clean it up. For example, if you create an array in a function then exit the function (without returning the array as a value), then the references to the array become zero and the GC marks it for clean up. That doesn’t mean its going to happen right away, but it will happen eventually.
The way to get around the GC is to create and allocate your objects globally when you start your program. Then you have to minimize the number of times you create new objects (type = new type) and variables. Creating objects in functions that are called often or quickly will incur a lot of GC overhead, since the GC needs to clean up whatever you don’t. This is analogous to the old c/c++ where if you created something with malloc or new, you needed to release it with free or delete (otherwise you had a memory leak). The difference here is you are letting the GC take care of destroying the object.
You are still creating a memory object, which is managed by the memory manager. Since the stack isn’t much more than a bunch of pointers (most structs can’t fit on the stack), it’s still a memory object. These are placed in the generation management system and garbage collected like any other object.
Here is a good reference of how the garbage collector works:
From a purely theoretical perspective it’s worth noting that the NETMF garbage collector is not implemented in quite the same way as the full framework garbage collector.