Never read the specs, but IIRC, it's a byte because you can't address a bit in memory. Though multiple booleans are in fact put togther into one byte to save space.
hmmm... I have an array with 4k booleans, it's 4kb in
memory.
the size is left to the writers of the particular JVM. It wouldn't be that tough to pack a byte but on the other hand it might be a slowdown.
Getting the exact size will be tough though. there are other factors to consider if you want to get to the byte level. for example, do you know the state of the stack just prior and just after your memory allocation size check? Do you know whats happening out in the heap with other threads allocating and deallocating memory?
How do you know for certain that part of what you are seeing isn't a side effect of a different thread within that particular jvm?
But if it really concerns you then make your own booleans bit sized (by declaring a short, and then packing/testing the bits yourself). Nothing stopping you from doing that.
But if it really concerns you then make your own booleans bit sized (by declaring a short, and then packing/testing the bits yourself). Nothing stopping you from doing that.
long freemem;
System.gc();
freemem = Runtime.getRuntime().freeMemory();
boolean[][] path = new boolean[main.mapSize][main.mapSize];
System.gc();
freemem = (freemem-Runtime.getRuntime().freeMemory());
return freemem;
... does the trick pretty much. There aren't much threads running anyways. I don't have much memory on my phone and my map is pretty big, but I don't have much speed eather so I'll find another solution. Thanx 4 your reply.
hmmm... I have an array with 4k booleans, it's 4kb in memory.
This implementation will give you the fastest possible access to each boolean. You can trade the fast access against a denser storage using a little bit-fiddling. Make it an int array and store 32 booleans as bits in each integer. The size of the array will shrink 32 times. You can also have a look at BitSet. I think it uses a dense memory representation but I'm not sure.
There is actually no boolean type. A Java expression that operates on boolean values is compiled to use the int data type to represent boolean variables. This means a boolean is usually a 32-bit variable. However arrays of booleans are treated as arrays of bytes, so booleans are 8 bits in an array.
This really comes as a chock to me. When did they
disappear?
If you look at the bytecode, you can see java handles ints and booleans the same.
{
int x = 0;
boolean b = false;
}
with javac this compiles to:
0 iconst_0
1 istore_1
2 iconst_0
3 istore_2
The i at the beginning of the instructions stands for integer, so obviously its treating it as an integer. Its even loading the same value into the local variable like it handles strings that are the same.
This topic has
35
replies
on
3
pages.
1
|
2
|
3
|
Next »