Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
367 views
in Technique[技术] by (71.8m points)

java - Sandbox for memory

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.

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.

Is there way to do so?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

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. 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

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

  3. Thread - it could have spawned some threads that persist

  4. 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`

see HotReloader


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...