简体   繁体   English

我应该在Java程序中使用多少内存?

[英]How much memory should I use in my Java program?

I am making a Java program. 我正在制作一个Java程序。 It involves making a image with size up to 9933 * 14043 pixels (which is A0 size and 300 ppi). 它涉及制作尺寸高达9933 * 14043像素(A0尺寸和300 ppi)的图像。 The image is 24 bit, so it would take up about 400mb of space. 图像为24位,因此占用大约400mb的空间。 The BufferedImage class some how take more RAM than the bitmap's actual size, so the image would comsume about 600 mb of RAM. BufferedImage类有些如何占用比位图实际大小更多的RAM,因此图像将占用大约600 MB的RAM。 With other data, the app would at max take about 700 mb of ram when rendering the large image. 使用其他数据时,应用程序在渲染大图像时最多会占用大约700 mb的RAM。 I haven't had any problem with it so far. 到目前为止我没有任何问题。 However, if the end user doesn't have enough free ram, the JVM will not be able to allocate the memory for the bitmap and will throw an OutOfMemoryError. 但是,如果最终用户没有足够的可用内存,则JVM将无法为位图分配内存并将抛出OutOfMemoryError。

So what should I do? 所以我该怎么做?

I came up with something: 我想出了一些东西:

  1. Catch the error and throw prompt to the user. 捕获错误并向用户抛出提示。
  2. Wait some time until here's enough memory. 等一段时间,直到这里有足够的记忆。 If the waiting last too long, throw prompt. 如果等待的时间太长,请提示。
  3. Write my own bitmap class, and write the image by part with an FileOutputStream. 编写我自己的位图类,并使用FileOutputStream按部分编写图像。 The .bmp format is not terribly complicated. .bmp格式并不十分复杂。 (Actually I already wrote and optimized most of it.) By rendering the bitmap by part, the whole image doesn't have to stay in RAM. (实际上我已经编写并优化了大部分内容。)通过部分渲染位图,整个图像不必保留在RAM中。 The size of the parts can be changed dynamically according to the available memory size. 可以根据可用的内存大小动态更改部件的大小。 However this is kind of reinventing the wheel and takes a significant amount of work. 然而,这有点重新发明轮子并需要大量的工作。 Also, the part of the image that involves text must be placed into a BufferedImage and then converted to my class (because I don't want to look into the true font format). 此外,涉及文本的图像部分必须放入BufferedImage,然后转换为我的类(因为我不想查看真正的字体格式)。 Anyway, if the Java BufferedImage class works in my case, I wouldn't go down this way. 无论如何,如果Java BufferedImage类在我的情况下工作,我不会这样做。

I doubt that anyone has less than a gig of ram nowadays. 我怀疑现在任何人都不到一阵公羊。 So you can check if the user has enough memory with Runtime.getRuntime().maxMemory() , and if they don't just show an error and close. 因此,您可以检查用户是否有足够的内存与Runtime.getRuntime().maxMemory() ,如果他们不只是显示错误并关闭。 Here's an example that uses JOptionPane in the case of an error: 这是一个在出现错误时使用JOptionPane的示例:

long memory = Runtime.getRuntime().maxMemory(); //in bytes
long required = 700 * 1024 * 1024; //700MB, in bytes
if(memory < required) {
    JOptionPane.showMessageDialog(null, "You don't have enough memory. (700MB required)", "Error", JOptionPane.ERROR_MESSAGE);
    System.exit(0);
}

maxMemory() returns the maximum amount of memory the JVM will attempt to use (in bytes). maxMemory()返回JVM将尝试使用的最大内存量(以字节为单位)。

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

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