我需要存储大量 RGB 颜色对象。对于某些常见用途,这些占用了我的应用程序总内存的 8% 到 12%。我目前将其定义如下:
class MyColor {
byte red;
byte green;
byte blue;
}
我假设(大多数)JVM 实际上为每个条目使用一个 int。最简单的替代方法是:
class MyColor {
byte [] color = new byte[3];
private static final int red = 0;
private static final int green = 1;
private static final int blue = 2;
}
这会把整个数组放在一个 int 中吗?或者它是一个 int[3] 在幕后?如果是第一个,那就太好了。如果是第二种,那么最好的是:
class MyColor {
int color;
private static final int red_shift = 0;
private static final int green_shift = 8;
private static final int blue_shift = 16;
}
或者有更好的方法吗?
更新:我还将有一个 getRed()、setRed(int)、... 作为访问器。我只是列出了类的数据组件以使其更小。 大小是这里的关键问题。代码不会花费大量时间访问这些值,因此性能不是大问题。
更新 2: 我使用 SizeofUtil 运行了它(在下面引用 - 谢谢)。我使用如下代码完成此操作:
protected int create() {
MyColor[] aa = new MyColor[100000];
for (int ind=0; ind<100000; ind++)
aa[ind] = new MyColor2();
return 2;
}
}.averageBytes());
这就是它变得奇怪的地方。首先,如果我不执行 for 循环,那么它只会创建数组(所有值为空),然后它会报告 400016 字节或 4 字节/数组元素。我在 64 位系统上,所以我很惊讶这不是 800000(Java 在 64 位操作系统上有 32 位地址空间吗?)。
但是奇怪的部分来了。 for 循环的总数是:
- 2800016.0
- 2600008.0
- 2800016.0
首先令人惊讶的是,byte[3] 的第二种方法使用更少的内存!有没有可能 JVM 看到声明中的 byte[3],只是内联分配它?
其次,每个对象的内存是 (2,800,000 - 400,000)/100,000 = 24。我会为第一种方法购买它,其中每个字节都是 native 64 位 int。 3 * 8 字节 = 24 字节。但是对于第三种情况,它是一个整数?这没有意义。
在这里写代码以防我遗漏了什么:
package net.windward;
import java.util.Arrays;
public class TestSize {
public static void main(String[] args) {
new TestSize().runIt();
}
public void runIt() {
System.out.println("The average memory used by MyColor1 is " + new SizeofUtil() {
protected int create() {
MyColor1[] aa = new MyColor1[100000];
for (int ind = 0; ind < 100000; ind++)
aa[ind] = new MyColor1();
return 1;
}
}.averageBytes());
System.out.println("The average memory used by MyColor2 is " + new SizeofUtil() {
protected int create() {
MyColor2[] aa = new MyColor2[100000];
for (int ind = 0; ind < 100000; ind++)
aa[ind] = new MyColor2();
return 2;
}
}.averageBytes());
System.out.println("The average memory used by MyColor3 is " + new SizeofUtil() {
protected int create() {
MyColor3[] aa = new MyColor3[100000];
for (int ind = 0; ind < 100000; ind++)
aa[ind] = new MyColor3();
return 1;
}
}.averageBytes());
System.out.println("The average memory used by Integer[] is " + new SizeofUtil() {
protected int create() {
Integer[] aa = new Integer [100000];
for (int ind = 0; ind < 100000; ind++)
aa[ind] = new Integer(ind);
return 1;
}
}.averageBytes());
}
public abstract class SizeofUtil {
public double averageBytes() {
int runs = runs();
double[] sizes = new double[runs];
int retries = runs / 2;
final Runtime runtime = Runtime.getRuntime();
for (int i = 0; i < runs; i++) {
Thread.yield();
long used1 = memoryUsed(runtime);
int number = create();
long used2 = memoryUsed(runtime);
double avgSize = (double) (used2 - used1) / number;
// System.out.println(avgSize);
if (avgSize < 0) {
// GC was performed.
i--;
if (retries-- < 0)
throw new RuntimeException("The eden space is not large enough to hold all the objects.");
} else if (avgSize == 0) {
throw new RuntimeException("Object is not large enough to register, try turning off the TLAB with -XX:-UseTLAB");
} else {
sizes[i] = avgSize;
}
}
Arrays.sort(sizes);
return sizes[runs / 2];
}
protected long memoryUsed(Runtime runtime) {
return runtime.totalMemory() - runtime.freeMemory();
}
protected int runs() {
return 11;
}
protected abstract int create();
}
class MyColor1 {
byte red;
byte green;
byte blue;
MyColor1() {
red = green = blue = (byte) 255;
}
}
class MyColor2 {
byte[] color = new byte[3];
private static final int red = 0;
private static final int green = 1;
private static final int blue = 2;
MyColor2() {
color[0] = color[1] = color[2] = (byte) 255;
}
}
class MyColor3 {
int color;
private static final int red_shift = 0;
private static final int green_shift = 8;
private static final int blue_shift = 16;
MyColor3() {
color = 0xffffff;
}
}
}
最佳答案
因为四个 byte
适合一个 int
,你可以使用一个 int
来表示你的颜色(并且仍然有额外的空间用于byte
如果你想添加,比方说,alpha,稍后)。对一小组方法进行示例(未经测试,只是为了让您明白这一点):
public int toIntColor(byte r, byte g, byte b) {
int c = (int) r;
c = (c << 8) | g;
c = (c << 8) | b;
return c;
}
并取回字节:
public byte red(int c) {
return c >> 16 & 0xFF;
}
public byte green(int c) {
return c >> 8 & 0xFF;
}
public byte blue(int c) {
return c & 0xFF;
}
关于java - 在 Java 中存储颜色 - byte;byte;byte vs. byte[3] vs int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20443508/