Answer the question
In order to leave comments, you need to log in
Memory consumption for object methods?
I'm new to Java so sorry if this is a dumb question.
The bottom line is that we are writing a server for an online game, but it doesn't matter.
Let's say there is a class "Player". Objects of this class store all sorts of player characteristics — health, strength, dexterity, etc.:
<br/>
public class Player extends Object {<br/>
public int hp = 0;<br/>
public int str = 0;<br/>
public int dex = 0;<br/>
public int agil = 0;<br/>
// ... и т.п.<br/>
}<br/>
Answer the question
In order to leave comments, you need to log in
The virtual machine (JVM) stores data of class instances and their methods in different areas of memory.
When multiplying the number of instances of one class, the copies of its methods are not multiplied. For more details, see The Structure of the Java Virtual Machine
chapter of the JVM specification: The Java Virtual Machine Specification
I am not a Java expert, but in most OOP languages, methods (all, not just static ones) are stored separately from the data and in a single instance.
Method code is stored once per class rather than being copied for each object.
Static ones are stored in the same place, they just cannot use class fields.
You start thinking about optimization too early, especially such a small one. I assure you that in the end you will face completely different problems. So write a game and don't think ahead of time about it)
You need to think not about the memory occupied by ridiculous 50k objects (you can also push 32GB of RAM onto the server). You need to think about how you will ensure the integrity of your database when the server hangs or restarts. The ability to roll back 5-10 minutes before the freeze can be invaluable.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question