In Java, are static class members shared among programs?

前端 未结 7 1981
独厮守ぢ
独厮守ぢ 2020-12-15 19:38

I imagine that no, they aren\'t, because every process has its own memory space, of course.

But how does the whole JVM thing actually work? Is there

相关标签:
7条回答
  • 2020-12-15 20:36

    Just to follow on to what Kevin mentioned, there are no JVM implementations that I am aware of that allow you to share static variables across JVM instances. As previous answers have stated (correctly) - each JVM instance is it's own process.

    Terracotta (which is a clustering technology, not a JVM) allows for arbitrary sharing of object instances across JVM process boundaries - including sharing static variables. Thus the moniker "network attached memory". For all intents and purposes, when using Terracotta technology in your JVM process, all of the threads in all of the VMs behave as if they were all looking at a single, large, shared heap. (With caveats of course, since that's not physically possible, Terracotta manages it through sophisticated sharing and replication algorithms which involve network io to move data - so therefore there are times when latency and throughput will not compare to native memory access)

    There are abundant examples in the Cookbook section of the Terracotta site - I recommend you start with the Hello Clustered Instance Recipe to get a feel for how it works.

    0 讨论(0)
提交回复
热议问题