本文介绍了指针 - 减少内存消耗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我一直在为OpenGL练习编写Minecraft副本(我猜多少),但是在编写基本渲染API之后,我注意到真正的Minecraft使用了很多或内存 - 大约800MB!我完全可以理解为什么这与它必须记住的所有块以及生成器的怪物和可能的地形数据...我问自己这个块与那个块完全相同......它们是否在代码中? 并记得C ++有指针,所以我试图在Java中做同样的事情我想到的唯一方法,创建每个块的一个静态实例而不使用 new 关键字这是最好的方法吗?它似乎肯定有帮助..如果有可能,我仍然希望它更好吗? 这是有问题的课程..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();对于100块大小的世界,当前内存使用量约为200MB,每个块由16个块宽组成64个高16个深,1,638,400个块 - 每个块大约128个字节。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.推荐答案 是,这是一个好方法。它被称为 Flyweight 模式。但是,考虑使用对象的工厂而不是静态字段。从长远来看,这将使您的代码更易于测试。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. flyweight模式的典型示例用法是用于字处理器中字符的图形表示的数据结构。对于文档中的每个字符,可能需要包含字体轮廓,字体度量和其他格式化数据的字形对象,但是对于每个字符,这将达到数百或数千个字节。相反,对于每个字符,可能存在对文档中相同字符的每个实例共享的flyweight字形对象的引用;只需要在内部存储每个字符的位置(在文档和/或页面中)。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.来自维基百科, http://en.wikipedia.org/wiki/Flyweight_pattern 这篇关于指针 - 减少内存消耗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!