问题
I have been writing a Minecraft replica for OpenGL practice (as many do I guess), however after writing the basic rendering API I noticed the real Minecraft uses a lot or memory - around 800MB! I can fully understand why this is with all the blocks it has to remember along with mobs and probably terrain data for the generator...I asked myself "This block is exactly the same as that block..can they be in the code?" and remembered C++ has pointers, so I have attempted to do the same in Java the only way I could think of, creating one static instance of each block and not using the new
keyword, is this the best way? It definitely seems to help..I would still like it to be better if that is possible?
Here is the class in question..
public abstract class Block {
public static DirtBlock Dirt = new DirtBlock();
public static GrassBlock Grass = new GrassBlock();
public static RedstoneOreBlock RedstoneOre = new RedstoneOreBlock();
public static TNTBlock TNT = new TNTBlock();
public static MonsterSpawnerBlock Monserspawner = new MonsterSpawnerBlock();
public static BedrockBlock Bedrock = new BedrockBlock();
public static StoneBlock Stone = new StoneBlock();
public static GlassBlock Glass = new GlassBlock();
public static SandBlock Sand = new SandBlock();
public static WaterBlock Water = new WaterBlock();
public static SnowBlock Snow = new SnowBlock();
public static SnowGrassBlock SnowyGrass = new SnowGrassBlock();
public static IceBlock Ice = new IceBlock();
public static CoalBlock Coal = new CoalBlock();
Current memory usage is about 200MB for a 100 chunk world with each chunk made up of 16 blocks wide by 64 high and 16 deep, 1,638,400 blocks total - about 128 bytes per block.
回答1:
Yes, this is a good way. It is called Flyweight pattern. But instead of static fields consider using factory of objects. This will make your code more testable in the long run.
In computer programming, flyweight is a software design pattern. A flyweight is an object that minimizes memory use by sharing as much data as possible with other similar objects; it is a way to use objects in large numbers when a simple repeated representation would use an unacceptable amount of memory. Often some parts of the object state can be shared, and it is common practice to hold them in external data structures and pass them to the flyweight objects temporarily when they are used.
A classic example usage of the flyweight pattern is the data structures for graphical representation of characters in a word processor. It might be desirable to have, for each character in a document, a glyph object containing its font outline, font metrics, and other formatting data, but this would amount to hundreds or thousands of bytes for each character. Instead, for every character there might be a reference to a flyweight glyph object shared by every instance of the same character in the document; only the position of each character (in the document and/or the page) would need to be stored internally.
From Wikipedia, http://en.wikipedia.org/wiki/Flyweight_pattern
来源:https://stackoverflow.com/questions/17155662/pointers-decreasing-memory-consumption