Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java3D:如何在使用纹理时避免OutOfMemoryError_Java_Textures_Out Of Memory_Java 3d - Fatal编程技术网

Java3D:如何在使用纹理时避免OutOfMemoryError

Java3D:如何在使用纹理时避免OutOfMemoryError,java,textures,out-of-memory,java-3d,Java,Textures,Out Of Memory,Java 3d,我正在循环中创建几个可视对象: ... Package packet; // a visual packet for(int i = 0; i < numberOfSteps; i++){ packet = new Package(packetSize, packetSize, packetSize); // position, rotate, etc. and add to scene graph } ... 所以我在循环中反复加载相同的纹理,最终导致OutOfMemoryE

我正在循环中创建几个可视对象:

...
Package packet; // a visual packet
for(int i = 0; i < numberOfSteps; i++){
  packet = new Package(packetSize, packetSize, packetSize);

  // position, rotate, etc. and add to scene graph
}
...

所以我在循环中反复加载相同的纹理,最终导致OutOfMemoryError。还有什么方法可以避免/优化这种情况吗?

与其一次又一次地创建新的纹理,不如尝试重新排列代码,使其重用相同的纹理。这样,纹理只需加载一次,因此使用的内存更少。

最明显的优化是缓存您的
缓冲图像

class ImageProvider{

   private static Map<String, Image> images = new HashMap<String, Image>();

   public static Image getImage(String filename){
       if(!images.contains(filename))
           try {
              images.put(filename, ImageIO.read(ImageProvider.class.getResource(filename));
           } catch (IOException ignore){
              //will return null if image cannot be loaded
           }

       return images.get(filename);
   }
}
类图像提供程序{
私有静态映射图像=新HashMap();
公共静态图像getImage(字符串文件名){
如果(!images.contains(文件名))
试一试{
image.put(文件名,ImageIO.read(ImageProvider.class.getResource(文件名));
}捕获(IOException忽略){
//如果无法加载图像,将返回null
}
返回images.get(文件名);
}
}

根据您以后进行的操作,您还可以缓存
ImageComponent2D
对象和/或
Texture2D
对象。

因此,简短的回答是肯定的。您可以优化,除非我错了,否则在理论上您可以重用相同的“包”对象,只需更改其中的一些参数以减少内存使用,就可以多次将该纹理加载到所创建包的内存中

class ImageProvider{

   private static Map<String, Image> images = new HashMap<String, Image>();

   public static Image getImage(String filename){
       if(!images.contains(filename))
           try {
              images.put(filename, ImageIO.read(ImageProvider.class.getResource(filename));
           } catch (IOException ignore){
              //will return null if image cannot be loaded
           }

       return images.get(filename);
   }
}