Will JVM allocate 10 different memory spaces for 10 instances at object creation time (I mean at the time I call the constructor i.e. new MyClass();
它可能会做,但是通过逃逸分析,它可以将它们放在堆栈上或完全消除它们.
or it will load the class definition once in memory and each instance while calling each of those 10 methods,at run time,JVM will allocate the memory?
如果您有一个ClassLoader,您将获得该类的一个实例,但是如果每个实例都有自己的ClassLoader,您将在每个ClassLoader中获得该类的副本.注意:每个ClassLoader可以具有相同名称的不同版本的类.
To clear some misunderstanding,my question is when creating an object,I know that all data members are allocated in heap memory,
它在PermGen(Java< 7)或MetaSpace(Java 8)中存储在堆中的类和方法信息(包括字节代码) 实际的实例在名义上被添加到堆中,但不一定是.
I’m not sure whether the methods which hasn’t been yet called are allocated differently in memory for each object or not?
JVM经历了许多优化阶段,当您调用方法时,它可能会优化它,内联它甚至消除它.您可以通过在命令行上添加-XX:PrintCompilation来查看正在编译(甚至重新优化)的方法.