New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Garbage collector does not appear to work, marks everything(?) instead #1068
Comments
Yes, your understanding is correct. The memory for that string could be freed (as the text of the book says) but our actual clox implementation doesn't free in this case for the reason you describe. |
Well, but in this case doesn't it render the entire garbage collector worthless? Could you share an example piece of code in which the garbage collector does free something at runtime?
To my understanding, pretty much everything we want to free is a constant at one point, ergo they're always marked for the reason stated above. |
It doesn’t. If you create other heap objects, the GC will free them when they are out of scope. One exception is with global variables as they are never ‘out of scope’, another exception is with strings because Lox interns every string, no string will be garbage collected. It seems you have a confusion on string interning. |
I can see that now. With this in mind I managed to create a sample that actually works: class A
{
}
A(); // instance 1
A(); // instance 2 - GC is triggered at this point and frees instance 1 Thank you both, and thanks for the amazing book. |
Reproduction
According to the book, running the following sample code, should clear "first value", however, it does not.
I added
collectGarbage()
at line 552 invm.c
, so that the garbage collector runs every time OP_SET_GLOBAL is executed.It does run, yet still marks "first value". Here's the relevant output:
What (I think) is the problem
In my own garbage collector, made mostly according to the book, I have a similar problem. Here's the cause:
In the collectGarbage->markRoots loop we mark the entire stack, including the global <script> function that gets pushed onto it at the start of execution:
After marking our objects, in traceRefrences we do:
and blacken object does the following to our global function:
This way, we mark all constants in the global function. We also mark all constants in functions declared in the global function and so on and so on. Therefore, the garbage collector marks every single constant in the entire program, including both "first value" and "updated".
Ending remarks
This may be a misunderstanding on my part (in which case, please prove me wrong). After running a direct example from the book, using code that was also 1:1 from the book, I believe it was justified to open an issue here.
The text was updated successfully, but these errors were encountered: