-
Notifications
You must be signed in to change notification settings - Fork 0
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
Use WeakRef
to wrap parent and children Logger
s
#33
Comments
So currently This isn't exactly correct, the original intention is that the key allows you reference a specific logger so you can re-use if necessary. However, this caused a memory leak because there was no way to deallocate a child - which would happen if we always used a dynamic key. To allow child loggers to be automatically garbage collected, we removed the child tracking. So to regain this functionality, we can use the new feature called The main idea is that ChatGPT explained that you can do this: class Logger {
private children: Map<string, WeakRef<LoggerChild>>;
private registry: FinalizationRegistry<string>;
constructor() {
this.children = new Map();
this.registry = new FinalizationRegistry((key: string) => {
// Callback to remove the reference from the map
this.children.delete(key);
});
}
getChild(key: string): LoggerChild {
let childRef = this.children.get(key);
let childLogger = childRef?.deref();
if (!childLogger) {
// Create a new child logger
childLogger = new LoggerChild(key);
// Store a weak reference to it
childRef = new WeakRef(childLogger);
this.children.set(key, childRef);
// Register the child logger for cleanup
this.registry.register(childLogger, key);
}
return childLogger;
}
}
class LoggerChild {
constructor(private key: string) {
// LoggerChild implementation
}
// Additional methods for LoggerChild
} Notice the usage of |
So I got this working, but testing this is difficult. The But I tested this with just a regular prototyping script and
And after running it I can see a deletion is occurring.
|
So I'll add it in for the ESM version and also the CSM version. @tegefaulkes can monitor that the memory situation to ensure that this didn't bring in a regression. |
is the |
It's necessary for GC of the key. |
Specification
Previously the each
Logger
kept a reference to it's parent and created children. This caused a memory leak where all derivedLogger
s formed a giant object group.To fix this we removed the ability to track the children, allowing the children to be properly freed when done with. However we needed to remove the ability to reduce duplicate logger instances.
Moving forward we want to maintain the original functionality but wrap the
Logger
references withWeakRef()
so they can still be garbage collected when needed. This means that at any time the reference can no longer exist. You can get the original reference withweakRefLogger.deref()
, this can return theLogger
orUndefined
if it was already GC'ed.Ultimately the children
Logger
will be tracked withRecord<string, WeakRef<Logger>
. When creating a child we check this record for the existingLogger
and deref it. If it still exist then that is returned. If it was GC'ed then a new one is created and it is replaced in the record.Additional context
Tasks
WeakRef()
to allow garbage collection.The text was updated successfully, but these errors were encountered: