记忆沙盒 [英] Sandbox for memory

查看:97
本文介绍了记忆沙盒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java应该没有内存泄漏,但是仍然可能.当我的程序出现内存泄漏时,我可以修复它(我希望如此).但是,当某些第三方程序包具备该功能时,我该怎么办?几乎什么都没有,除非不使用此程序包.

Java supposed not to have memory leaks but it's still possible. When my program has memory leak I can fix it (I hope). But when some third party packages have it what can I do? Practically nothing except don't using this package.

还有其他解决方案吗?我喜欢沙盒的想法.您可以在某个区域内做任何您想做的事,而身体上的"则没有能力在盒子外面打扰其他人.有没有办法为Java中的内存使用创建此类沙箱?想象一下=创建用于内存使用的沙箱,允许某些程序包做任何事情,获取结果,并使用此处留下的任何垃圾删除此沙箱! GC没有并发症,没有清理或处置内存.只需删除并忘记它即可.

Is there another solution? I like idea of sandbox. You are allowed to do whatever you want within some area and you "physical" don't have ability to bother other outside of your box. Is there way to create such sandbox for memory usage in Java? Imagine = create sandbox for memory usage, allow some package do whatever it does, take results and delete this sandbox with whatever garbage was left here! No complications with GC, no cleaning or disposition of memory. Just delete and forget about it.

有办法吗?

推荐答案

最好的方法是启动另一个JVM,并通过套接字与它进行通信(例如),并在完成后杀死JVM.

The best way is to launch another JVM, communicate with it with socket(for example), and kill the JVM after it's done.

讨论我们是否可以在同一JVM中对其进行沙箱测试将很有趣.

It'll be interesting to discuss whether we can sandbox it within the same JVM.

在使用完第3方库之后,您不再引用该库中的任何对象,那么仍在挥之不去的垃圾又是什么呢?

After you are done with using the 3rd party library, and you are no longer referencing any objects from that library, what could be the garbage that's still lingering?

  1. 它们的类-即使您没有引用它们中的任何一个,如果它们由与您的代码相同的类加载器加载,则这些类将持久化.而且它们的静态字段可以引用更多的数据,依此类推

  1. Their classes - even though you are not referening any of them, if they are loaded by the same classloader as your code, these classes will persist. And their static fields could reference more lingering data, and so forth

ThreadLocal-它可能已经设置了一些线程局部变量,而没有清除它们

ThreadLocal - it could have set some thread local variables, and not cleaned them up

线程-它可能已经产生了一些持久存在的线程

Thread - it could have spawned some threads that persist

在某些全球范围内-例如System.setProperty()-它会保留在那里.

In some global place - e.g. System.setProperty() - it'll stay there.

因此,通常来说,这可能很困难.

So in general, it's probably difficult.

但是,我们可以使用单独的类加载器和单独的线程来执行第三方库,并且这种策略在大多数情况下可能可以卸载由第三方创建的所有垃圾.

However, we can use a separate classloader and a separate thread to execute the 3rd party library, and this strategy can probably unload all garbages created by the 3rd party, in most cases.

是否存在用于执行此操作的工具?我对此不太了解.我确实有一些实现热可重装服务器的经验,并且我有一些实用程序类可用于此目的.例如

Are there existing tools for doing that? I'm not too knowledgeable about that. I do have some experience with implementing a hot reloadable server, and I have some utility classes that can be used for this purpose. For example

// wrap 3rd party code, expose it as some java.*.* interface
public class MyWrapper implements Callable<String>
{
    @Override 
    public String call()
    {
        return ThirdParty.query(..);
    }
}



HotReloader hot = new HotReloader();
Callable<String> func = (Callable<String>)hot.getAppInstance("pkg.MyWrapper");
String result = func.call();
// then dereference `hot` and `func`

请参见 HotReloader

这篇关于记忆沙盒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆