S
S
soulburner2010-12-25 15:00:44
Java
soulburner, 2010-12-25 15:00:44

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

Accordingly, the number of such objects is the same as the number of players.
Let's say we have 50 thousand players. Memory such objects will eat decently. Therefore, I would like to thoroughly understand the memory consumption of Java.
Accordingly, the question is as follows - if I need to create some methods that operate on the player - should I make them static?
Why I ask - I thought that if I create some non-static method in the Player class, then every time an object of this class is created, space will be allocated in memory for the code of all its methods. Those. if I want a savePlayer() method, then after creating 50k players, I'll have 50k copies of that method's code in memory. Which obviously fail. And that's why I have to create a static method like savePlayer(Player player).
I'm right?
It's just that I decided to check it out. I looked in the Eclipse profiler ... Now I have one Player instance occupies 112 bytes. After that, I added a non-static method to the "player", launched it again ... and strangely - the player instance still takes 112 bytes. I expected to see an increase in the size of the "player" instance by the size of the code of the added method.
Explain plz.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
R
Rafael Osipov, 2010-12-25
@soulburner

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

L
Limosha, 2010-12-25
@Limosha

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.

J
javax, 2010-12-25
@javax

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.

R
reality, 2010-12-25
@reality

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)

V
vanxant, 2010-12-25
@vanxant

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 question

Ask a Question

731 491 924 answers to any question