The consumption of memory for object methods?

0 like 0 dislike
2 views
I am a beginner in Java, this sorry, if the question is stupid.


Here's the thing — we write a server for online games, but it does not matter.


Suppose you have a class "Player". The objects of this class stored all the characteristics of the players — health, strength, agility, etc:


public class Player extends Object {
public int hp = 0;
public int str = 0;
public int dex = 0;
public int agil = 0;
// ... etc.
}


Accordingly, such facilities required by the number as much as the players.


Let's say we have 50 million players. Such memory objects will eat decently. For this, I want thoroughly to understand the amount of memory the Java.


Accordingly, the question is — if I need to create some methods that operates with the player — do I have to make them static?


Why I ask — I thought if I create a not-static method in class Player, then whenever you create an object of that class in the memory will be allocated a place under the code of all its methods. Ie if I want method savePlayer(), creating 50 thousand players, I will get in the memory 50 thousand copies of the code of this method. That is clearly a fail. And in this I have to create a static method of a type savePlayer(Player player).


Am I right?


Just the fact that I decided to check it out. Looked in the Profiler, the Eclipse... Now I have one instance of the Player is 112 bytes. After that, I didn't add static method in the "player" has started again... and it is strange — court player still occupies 112 bytes. I was expecting to see an increase in the size of the instance "player" on the size of the code added to the method.


Explain pliz.
by | 2 views

5 Answers

0 like 0 dislike
Virtual machine (JVM) stores the data instances of classes and their methods in different areas of memory.
\r
By multiplying the number of instances of one class by multiplying copies of his methods is not happening.
\r
Greater detail can be read in Chapter The Structure of the Java Virtual Machine specification JVM: The Java Virtual Machine Specification
by
0 like 0 dislike
I'm not an expert in Java, but in most OOP languages, methods (all, not just static) are stored separately from the data and in a single instance.
by
0 like 0 dislike
The method's code is stored once per class and not copied for each object.
\r
Static is stored in the same place, they just can't use class fields.
by
0 like 0 dislike
You are too early to start thinking about optimization, especially such small ones. I assure you that you will be faced with very different problems. So write the game and don't think ahead of time about this )
by
0 like 0 dislike
You need to think about memory funny 50K objects (on the server can be put 32GB of RAM). You need to think about how you will ensure the integrity of your database when you hang or restart the server. The ability to revert 5-10 minutes to hang before can prove invaluable.
by

Related questions

0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
3 answers
0 like 0 dislike
3 answers
0 like 0 dislike
3 answers
110,608 questions
257,186 answers
0 comments
32,900 users