Skip to content
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

[Bug]: 尝试DisposeDomain(),提示异常 #294

Closed
Ramoshuai opened this issue May 11, 2024 · 7 comments
Closed

[Bug]: 尝试DisposeDomain(),提示异常 #294

Ramoshuai opened this issue May 11, 2024 · 7 comments
Labels
bug Something isn't working.

Comments

@Ramoshuai
Copy link

Ramoshuai commented May 11, 2024

⚙️ OS

Windows (Default)

⚙️ Architecture

Any (Default)

⚙️ .NET Version

.NET6(LTS)

📄 请描述您的问题(Describe the bug).

尝试DisposeDomain(),提示
找不到 RuntimeMetadataVersion 的值。找不到包含 System.Object 的程序集,或未通过选项为 RuntimeMetadataVersion 指定值。

DotNetCore.Natasha.CSharp.Compiler.Domain 8.0.0
DotNetCore.Natasha.CSharp.Template.Core 8.0.0

NatashaManagement.Preheating<NatashaDomainCreator>(true,true);
string code = "var b=2; return arg+b;";
var func = NDelegate.RandomDomain().Func<int, int>(code);
func(1);
func.DisposeDomain();

还有个问题,如果code一样情况下,NDelegate.RandomDomain().Func<int, int>(code)会每次都去创建。
这个是需要自行做缓存控制,还是说后续版本考虑内部优化

⭕ BUG 重现(To Reproduce).

No response

📞 您的联系方式(Contact Details).

No response

@Ramoshuai Ramoshuai added the bug Something isn't working. label May 11, 2024
@NMSAzulX
Copy link
Collaborator

未检测到合适的 ISSUE 推荐给您。感谢您的反馈!

该条自动推荐信息来自于 nms-bot.

@NMSAzulX
Copy link
Collaborator

试试这个。

var func = NDelegate
    .RandomDomain()
    .ConfigBuilder(opt => opt.UseSmartMode())

或者

var func = NDelegate
    .RandomDomain()
    .ConfigBuilder(opt => opt.ConfigLoadContext(ctx=>ctx.AddReferenceAndUsingCode<object>()))

@NMSAzulX
Copy link
Collaborator

还有个问题,如果 code 一样情况下,NDelegate.RandomDomain().Func<int, int>(code)会每次都去创建。

var func = NDelegate.RandomDomain().Func<int, int>(code);

把 func 存起来用就可以了。这个需要自己缓存起来。

@Ramoshuai
Copy link
Author

我尝试
NDelegate.RandomDomain().ConfigBuilder(opt => opt.UseSmartMode())
现在调用func.DisposeDomain(); 是不会报错了,但是发现个奇怪的地方

1.第一个版本,我尝试
var func = NDelegate.RandomDomain().Func<int, int>(code);
不去主动释放,发现CLR加载后没有卸载,意味着创建完没有释放,随着调用次数的增多,内存一直增长,执行时间也会变长。这个是我这边调用的问题,如果用这种方式的话,自己做好缓存规避

2.当我用上面的方法初始化编译单元,并且主动DisposeDomain的时候,发现内存会一直增长

            NatashaManagement.Preheating<NatashaDomainCreator>(true,true);
            string code = "var b=2; return arg+b;";
            while (true)
            {
                var startTime = DateTime.Now;
                var func = NDelegate.RandomDomain().ConfigBuilder(opt => opt.UseSmartMode()).Func<int, int>(code);
                func.DisposeDomain();
                Console.WriteLine(DateTime.Now.Subtract(startTime));
            }

3.当我只使用
var func = NDelegate.RandomDomain().ConfigBuilder(opt => opt.UseSmartMode()).Func<int, int>(code);
而不再主动 DisposeDomain,我发现 内存保持94M左右不再增长。

@NMSAzulX
Copy link
Collaborator

@Ramoshuai 卸载原理是,没有元数据保持引用。如果 func 被存起来,或者有相关元数据参与引用会导致无法卸载。

image

回收检测目前每出现问题,内存有下降,考虑是否是上述问题。

@NMSAzulX
Copy link
Collaborator

另外卸载域与当前方法有关,不可以在main方法里做测试。

@Ramoshuai
Copy link
Author

另外卸载域与当前方法有关,不可以在main方法里做测试。

测试了下可以了,感谢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working.
Projects
None yet
Development

No branches or pull requests

2 participants