Jvm adopts the mechanism of complementary storage for variables.

in jvm, the range of short is much smaller than that of int, but it takes up the same size of slot as int. Is the storage mode of short a legendary complement, in order to improve the query speed

?
Jvm
Jul.14,2022

the slot you mentioned is the variable slot in the local variable table. The virtual machine specification does not specify the amount of memory space that slot should occupy, but directively says that each slot should store a boolean , byte , char , short , int , float , reference , or returnAddress type data. The slot size should be less than or equal to the word length of the operating system, that is, the bus width. For example, a 32-bit machine has a word length of 32 bits.
take a 32-bit machine as an example. CPU reading data from main memory will read 32bit at one time, and it is atomic, so the size of a slot is set to 32 bits, short occupies 16bit in heap memory, and padding to 32 bits in stack memory by means of alignment.

imagine storing two short in one slot, so it is very difficult for the first data not to affect the second data when calculating.


first of all, memory usage is related to version and jvm implementation.

if you just declare a single variable
such as

short a;

it can be considered that there is no difference between short and int in operation and memory use, because of jvm alignment, memory consumption may be 4 bytes, but
if serialization or array allocation, memory footprint and storage space are not the same.

refer to the following code to compare the difference in file size between the two stores


import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class MemTest {
    public static void main(String[] args) {
        new MemTest().save(new Address());
    }

    public void save(Address address) {

        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("address2.ser"))) {

            oos.writeObject(address);
            System.out.println("Done");

        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }
}

class Address  implements Serializable{
    
    //int a[]= new int[1000];
    short a[]= new short[1000];
    public Address(){
        for(int i=0; i<1000;iPP){
            a[i]=1024;
        }
    }
}
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b37060-2c064.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b37060-2c064.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?