简体   繁体   English

优化Java中的OBJ文件(3D模型)加载

[英]Optimizing OBJ file (3d model) loading in java

Before I begin I apologize for my lack of comments in my code. 在开始之前,我对代码中缺少注释表示歉意。 I am currently making a OBJ file loader (in java.) Although my code works as expected for small files, when files become large (for example I am currently attempting to load a obj file which has 25,958 lines) my entire system crashes. 我目前正在制作OBJ文件加载器(以Java语言编写)。尽管我的代码可以按预期工作于小文件,但是当文件变大时(例如,我当前正在尝试加载包含25958行的obj文件),整个系统崩溃。 I recently migrated my entire project over from C++ which could load this model quickly. 最近,我从C ++迁移了整个项目,可以快速加载此模型。 I utilized a profiler alongside a debugger to determine where the entire process crashes my system. 我利用探查器和调试器来确定整个过程在哪里使系统崩溃。 I noticed a few things; 我注意到了几件事; first, it was hanging at the initiation process; 首先,它挂在启动过程中; second, my heap was nearly completely used up (I used up about 90% of the heap.) My code can be found here: http://pastebin.com/VjN0pzyi I was curious about methods I could employ to optimize this code. 其次,我的堆几乎用完了(我用掉了大约90%的堆。)我的代码可以在这里找到: http : //pastebin.com/VjN0pzyi我对我可以用来优化此代码的方法感到好奇。

When you're really low on memory, everything slows down a lot. 当您真正的内存不足时,一切都会减慢很多。 I guess you should improve you coding skills, things like 我想您应该提高编码技能,例如

startChar = line[i].toCharArray()[k];

probably don't get optimized to 可能没有被优化为

startChar = line[i].charAt(k);

automagically. 自动地。 Maybe interning your strings could save a lot of memory, try String.intern or Guava Interner . 也许对您的字符串进行实习可以节省很多内存,请尝试使用String.intern或Guava Interner

The Hotspot loves short methods, so refactor. 热点喜欢简短的方法,因此可以重构。 The code as it is hard to read and I guess that given its size no optimizations get done at all! 由于很难阅读代码,因此我认为鉴于其大小,根本无法完成优化!

I know this is an old question, but I wanted to throw in my two cents on your performance issues. 我知道这是一个老问题,但是我想在性能问题上投入2美分。 You're saying that your code not only runs slow, but it takes up 90% of the heap. 您是说您的代码不仅运行缓慢,而且占用了90%的堆。 I think saying 90% is an egregious exaggeration, but this still allows me to point out the biggest flaw with Java game development. 我认为说90%过于夸张,但这仍然让我指出了Java游戏开发的最大缺陷。 Java does not support value types, such as structs. Java不支持值类型,例如结构。 That means that in order to gain speed you're required to avoid OOP, because every time you instance a class for your loader it is allocated onto the heap. 这意味着为了提高速度,您需要避免OOP,因为每次为装载程序实例化一个类时,该类都会分配到堆上。 You must then invariably wait for GC to kick in to get rid of the clutter and left over instances that your loader created. 然后,您必须始终等待GC启动,以摆脱混乱并遗留您的加载程序创建的实例。 Now take a language like C# as an example of how to create a real language . 现在以C#之类的语言为例, 介绍如何创建一种真实的语言 C# fully supports structs. C#完全支持结构。 You could replace every class of your loader with them. 您可以用它们替换装载机的每个类。 Faces, groups, Vertex, Normal, classes are then treated as value types; 然后将面,组,顶点,法线,类视为值类型; they are deleted when the stack unwinds. 它们在堆栈展开时被删除。 No garbage is generated, or at least very little if you're required to use a class or two. 不会产生垃圾,如果需要使用一两个类,则不会产生垃圾。

In my opinion, don't use Java for game development. 我认为,请勿将Java用于游戏开发。 I used it for years before discovering C#. 在发现C#之前,我已经使用了多年。 Strictly my opinion, here, but Java is a horrible language; 在这里,我的观点很严格,但是Java是一种可怕的语言。 I will never use it again. 我将不再使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM