-
Notifications
You must be signed in to change notification settings - Fork 164
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
Memory leak #262
Comments
Hey Ivan, I did some more digging on this and from what I can tell it appears that |
Although it didn't fix the problem of the memory leak, the GC not cleaning up runtime.SetFinalizer(self, func(self *Instance) {
self.Exports.exports = nil
C.wasm_instance_delete(self.inner())
}) |
Congrats for the fix! And thanks for digging the problem! Can you open a PR please? |
OK so finalizers aren't running because the document says:
There is no guarantee it will run. I'll implement the |
@prep Can you try this patch #277 and tell me if it fixes your problem. I've found several problems |
First, this patch provides new `Close` methods on some “top types” (`Store`, `Module`, `Instance` etc.). The `Close` methods are redundant to the runtime finalizers, but they are useful to force to destruct a specific type manually before the Go GC collect it. Note that calling `Close` will remove the runtime finalizer attached to the object. Note that runtime finalizers call the `Close` method if defined on the current object. Second, this patch forces the destructors (now the `Close` methods) to destruct the children attached to the current object. Let's see it in details: * `Module` now has two new fields `importTypes *importTypes` and `exportTypes *exportTypes`. The value is null, except if the `Imports` or `Exports` methods are called. They will store their respective results in their respective fields. The goal of this change is twofold: 1. Avoiding computing the same import and export types multiple times each time one of the method is called, 2. By holding a reference to the private `importTypes` and `exportTypes`, `Module` can free them. Before that, the objects were orphan and the garbage collector had difficulties to collect them. * `Instance` now forces to free its `Exports`, * `Exports` now force to free all the items of its `exports` map field. Before this patch, only the `_inner` C pointer was freed, but not the map. I have no idea why the map wasn't collected by the Go GC. Running the script written by @prep in wasmerio#262 shows that there is no more memory leaks.
Apologies for creating another issue.
Describe the bug
I think there might be a memory leak when creating instances.
A clear and concise description of what the bug is
When I create an instance with a small module in an endless loop, the memory as is shown by Activity Monitor on macOS is ever increasing at a higher rate than Go's memory stats show.
Steps to reproduce
The example in #148 seems to show the problem, but the version below shows Go's internal memory stats:
Expected behavior
I expect the memory usage to grown a bit and then even out to a stable number.
Actual behavior
The memory usage keeps increasing.
Additional context
I've confirmed that the instance's
runtime.SetFinalizer()
hook is properly called, so the instance appears to be deleted by the embedded call towasm_instance_delete()
as expected.From the above program:
From Activity Monitor on macOS:

The text was updated successfully, but these errors were encountered: