-
-
Notifications
You must be signed in to change notification settings - Fork 113
Allow manual heap deallocation with zerolib #79
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
base: master
Are you sure you want to change the base?
Conversation
|
Thanks! I'll think about this a bit. I've been scratching my head on how to best approach this too. Repurposing |
|
Perhaps this could involve the NativeMemory class (as without a GC, all zerolib memory is native memory), which has dedicated methods for alloc and free. Although the API ergonomics of those functions leave much to be desired.. |
I don't think there is a solution that satisfies both. The closest thing C# has to manual memory management is the dispose pattern, which uses the Btw, I also considered this approach: int id = GC.GetGeneration(obj);
GC.Collect(id, GCCollectionMode.Optimized);It is more explicit, but the implementation is complicated and it has a much higher overhead in .NET than
While it might be useful to provide access to the already implemented alloc/free functions via the |
|
How about adding a Method to |
|
Why not just unsafe static bool Free(void* ptr)
{
...
}? Since it ins't .NET, I see no point in creating an "GC" class for this. There is no GC here. |
See here:
|
|
Shouldn't this be void Free? |
it could return an boolean indicating if the memory was freed or not. |
|
Well if there are memory leaks why not implement a profiler that constantly tracks allocations and deallocations? I mean profilers are possible with native aot now for .NET |
|
Freeing an object that's on the call stack turns your code into a minefield. Your Also looking through the standard .NET framework, many functions use |
This is no different from C/C++, which also has manual memory management. Originally, I was looking for a dotnet method that takes a
Yes, you should not call anything on the freed object. That's a responsibility the developer takes on when working with Zerolib. Zerolib will only work with specially tailored code that follows its constraints. The goal is that the same code should also work perfectly fine when compiled and run with dotnet. The reverse is not necessarily true - you can write code that will work fine with dotnet, but will crash with Zerolib. |
|
Is it possible to use libc's |
|
@iahung2 do you mean consuming windows API implementations? One question that I have idk If it is a practical question or not: But is it possible to reimplement these methods in C# by peeking the windows API implementations? |
My question is simple. I have a class called |
|
@iahung2 as far as I know, is you can't, allocating in heap isn't preferred and instead of reference types use structs as @MichalStrehovsky suggested to use stack allocation instead of heap, as heap allocation will eventually run out of memory due to no GC, you can try to see if free deallocates or not but this was my understanding from this statement. https://twitter.com/MStrehovsky/status/1728378901188235301?t=NnuY-TJHNsztAasdfLQAxQ&s=19 |
|
@Tajbiul-Rawol My intuition tells me that |
|
@iahung2 I agree. The tool feels very limited in what it can do, maybe because it is a brand new tool, but with no manual memory collection system for heap it is severely limiting. @MichalStrehovsky hope you change your decision and implement something to handle deallocations. |
|
I'm going to give heap allocation for nostdlib a shot by overriding the .Net new() operator, as per my comment here: #138 (comment) But if anyone wants to beat me to it, please feel free 😉 |
|
Here’s another idea: how about overriding GC.KeepAlive() to free an object? (Instead of GC.SuppressFinalize) It might sound odd at first, but when you think about it, the purpose of KeepAlive is to ensure that the object stays alive until KeepAlive is called. Nobody calling that method should have the expectation that the object would live any longer, so in theory, it should be safe to release at that point. |
|
Ultimately it comes down to the meaning of the method because no new methods will be added and it will be based on an existing api. |
|
you can import the LocalFree function and pass it an argument in the form of &classInstance (this way you will pass the MethodTable of the instance.) |
|
Don't mean to be "that guy", but what stops someone from forking this project (which doesn't look like it's worked on very much), Michal is a very busy guy and this is just his personal project. Someone could create a fork and then those that are serious about it and want to advance it can do it at a faster pace and make it more useable other than a personal toy project. |
|
@MichalStrehovsky will this issue be resolved or should this be closed? |
Currently, when using
--stdlib:zero, all heap-allocated data are memory leaks since there is no GC.This PR adds the ability to manually free heap-allocated objects using the
GC.SuppressFinalizemethod. Why this method?object.GC.SuppressFinalizeare no-ops unless the type has a finalizer.IDisposableinterface,GC.SuppressFinalizeis typically called at the end when all managed resources have been released. It makes sense to deallocate the object itself at that point.Example:
It is also possible to have nested objects if the
IDisposableinterface is implemented correctly. Arrays also work, but can't implementIDisposable, so they have to be freed by callingGC.SuppressFinalizedirectly.I haven't tested the Linux and UEFI code. Feel free to edit this PR as needed.