What algorithms are used?
Java uses TLAB (Thread Local Allocation Buffers) for “normal” sized objects. This means each thread grab some Eden space and turns this grab of memory into individual objects. Thus small objects are typically sequential in memory for that thread, except if a new chunk of memory needs to be grabbed.
Large objects such as large arrays are allocated directly into tenured space, finding a free space which is enough for object allocated (in a single threaded manner)
Which random number generators, if any, does a JVM use for determining allocation address when allocating a memory block
It doesn’t use any random number generators for allocation. Nor would you want it to. It would be much slower and add needless complexity.
for a variable, e.g. int a;
Variables are not allocated only Objects are allocated. Variables can appear on the stack or in an objects and are usually laid out in the simplest manner possible.
when it runs the bytecode corresponding to ‘new int()’?
You can’t do new int()
BTW: There is a common misconception that if you print a new Object()
and get something like Object@b7f86352
that the hexidecimal is an address. This is not the case. The address of the object can be used as a seed for a random number generated and stored in the header, but this number has nothing to do with the actual memory address and it doesn’t change as the object is moved around in memory.
5
solved How ‘random’ is allocation of memory when I say “new Integer” in Java?