-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
644 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package jol; | ||
|
||
import org.openjdk.jol.info.ClassLayout; | ||
import org.openjdk.jol.vm.VM; | ||
|
||
import static java.lang.System.out; | ||
|
||
/** | ||
* @author Aleksey Shipilev | ||
*/ | ||
public class JOLSample_01_Age { | ||
|
||
public static void main(String[] args) { | ||
A a = new A(); | ||
for (int i = 0; i < 500; i++) { | ||
var bytes = new byte[1024 * 1024]; | ||
} | ||
out.println(ClassLayout.parseInstance(a).toPrintable()); | ||
long markword = VM.current().getLong(a, 0); | ||
printAgeBit(markword); | ||
} | ||
|
||
public static class A { | ||
boolean f; | ||
} | ||
|
||
public static void printMarkWordBin(long markword) { | ||
out.printf("markword bit: %s\n", toBinaryWithSpaces(markword)); | ||
} | ||
|
||
public static void printMarkBit(long markword) { | ||
out.printf("mark bit: %s\n", toBinaryWithSpaces(markword & 0b11)); | ||
} | ||
|
||
public static void printbiasBit(long markword) { | ||
out.printf("bias bit: %s\n", toBinaryWithSpaces(markword >> 2 & 0b1)); | ||
} | ||
|
||
public static void printAgeBit(long markword) { | ||
out.printf("age bit: %s\n", toBinaryWithSpaces(markword >> 3 & 0b1111)); | ||
} | ||
|
||
public static void printHashcodeBit(long markword) { | ||
out.printf("hash code bit: %s\n", toBinaryWithSpaces((markword >>> 8))); | ||
} | ||
|
||
|
||
public static String toHexWithSpaces(long number) { | ||
String hex = Long.toHexString(number); | ||
// 如果长度是奇数,在前面补零 | ||
if (hex.length() % 2 != 0) { | ||
hex = "0" + hex; | ||
} | ||
// 每两位分隔开 | ||
return hex.replaceAll("(?<=..)(..)", " $1").toUpperCase(); | ||
} | ||
|
||
public static String toBinaryWithSpaces(long number) { | ||
String binary = Long.toBinaryString(number); | ||
// 如果长度不是8的倍数,前面补零 | ||
while (binary.length() % 8 != 0) { | ||
binary = "0" + binary; | ||
} | ||
// 每8位分隔开 | ||
return binary.replaceAll("(.{8})", "$1 "); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package jol; | ||
|
||
import org.openjdk.jol.info.ClassLayout; | ||
import org.openjdk.jol.vm.VM; | ||
|
||
import static java.lang.System.out; | ||
|
||
/** | ||
* @author Aleksey Shipilev | ||
*/ | ||
public class JOLSample_01_Basic { | ||
|
||
public static void main(String[] args) { | ||
A a = new A(); | ||
out.println(ClassLayout.parseInstance(a).toPrintable()); | ||
|
||
long markword = VM.current().getLong(a,0); | ||
printMarkWordBin(markword); | ||
printMarkBit(markword); | ||
printbiasBit(markword); | ||
printAgeBit(markword); | ||
} | ||
|
||
public static class A { | ||
boolean f; | ||
} | ||
|
||
public static void printMarkWordBin(long markword) { | ||
out.printf("markword bit: %s\n", toBinaryWithSpaces(markword)); | ||
} | ||
|
||
public static void printMarkBit(long markword) { | ||
out.printf("mark bit: %s\n", toBinaryWithSpaces(markword & 0b11)); | ||
} | ||
|
||
public static void printbiasBit(long markword) { | ||
out.printf("bias bit: %s\n", toBinaryWithSpaces(markword>>2 & 0b1)); | ||
} | ||
|
||
public static void printAgeBit(long markword) { | ||
out.printf("age bit: %s\n", toBinaryWithSpaces(markword >> 3 & 0b1111)); | ||
} | ||
|
||
public static void printHashcodeBit(long markword) { | ||
out.printf("hash code bit: %s\n", toBinaryWithSpaces((markword >>> 8))); | ||
} | ||
|
||
|
||
|
||
public static String toHexWithSpaces(long number) { | ||
String hex = Long.toHexString(number); | ||
// 如果长度是奇数,在前面补零 | ||
if (hex.length() % 2 != 0) { | ||
hex = "0" + hex; | ||
} | ||
// 每两位分隔开 | ||
return hex.replaceAll("(?<=..)(..)", " $1").toUpperCase(); | ||
} | ||
|
||
public static String toBinaryWithSpaces(long number) { | ||
String binary = Long.toBinaryString(number); | ||
// 如果长度不是8的倍数,前面补零 | ||
while (binary.length() % 8 != 0) { | ||
binary = "0" + binary; | ||
} | ||
// 每8位分隔开 | ||
return binary.replaceAll("(.{8})", "$1 "); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package jol; | ||
|
||
import org.openjdk.jol.info.ClassLayout; | ||
import org.openjdk.jol.vm.VM; | ||
|
||
import static java.lang.System.out; | ||
|
||
/** | ||
* @author Aleksey Shipilev | ||
*/ | ||
public class JOLSample_01_Hashcode { | ||
|
||
public static void main(String[] args) { | ||
A a = new A(); | ||
|
||
int hashcode = a.hashCode(); | ||
out.println(ClassLayout.parseInstance(a).toPrintable()); | ||
out.println("a.hashCode() : " + toBinaryWithSpaces(hashcode)); | ||
long markword = VM.current().getLong(a,0); | ||
printHashcodeBit(markword);//(markword >>> 8) | ||
printMarkWordBin(markword); | ||
} | ||
|
||
public static class A { | ||
boolean f; | ||
} | ||
|
||
public static void printMarkWordBin(long markword) { | ||
out.printf("markword bit: %s\n", toBinaryWithSpaces(markword)); | ||
} | ||
|
||
public static void printMarkBit(long markword) { | ||
out.printf("mark bit: %s\n", toBinaryWithSpaces(markword & 0b11)); | ||
} | ||
|
||
public static void printbiasBit(long markword) { | ||
out.printf("bias bit: %s\n", toBinaryWithSpaces(markword>>2 & 0b1)); | ||
} | ||
|
||
public static void printAgeBit(long markword) { | ||
out.printf("age bit: %s\n", toBinaryWithSpaces(markword >> 3 & 0b1111)); | ||
} | ||
|
||
public static void printHashcodeBit(long markword) { | ||
out.printf("hash code bit: %s\n", toBinaryWithSpaces((markword >>> 8))); | ||
} | ||
|
||
|
||
|
||
public static String toHexWithSpaces(long number) { | ||
String hex = Long.toHexString(number); | ||
// 如果长度是奇数,在前面补零 | ||
if (hex.length() % 2 != 0) { | ||
hex = "0" + hex; | ||
} | ||
// 每两位分隔开 | ||
return hex.replaceAll("(?<=..)(..)", " $1").toUpperCase(); | ||
} | ||
|
||
public static String toBinaryWithSpaces(long number) { | ||
String binary = Long.toBinaryString(number); | ||
// 如果长度不是8的倍数,前面补零 | ||
while (binary.length() % 8 != 0) { | ||
binary = "0" + binary; | ||
} | ||
// 每8位分隔开 | ||
return binary.replaceAll("(.{8})", "$1 "); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import java.util.ArrayList; | ||
|
||
public class SyncTest { | ||
private Object lock = new Object(); | ||
private Object lock1 = new Object(); | ||
|
||
public static void main(String[] args) { | ||
new Thread(()->{ | ||
new SyncTest().testCountMonitorInMethod(); | ||
}).start(); | ||
smallObj(); | ||
} | ||
|
||
public void testCountMonitorInMethod() { | ||
synchronized (lock) { | ||
synchronized (lock1) { | ||
sleep(); | ||
} | ||
} | ||
} | ||
|
||
public static void smallObj() { | ||
ArrayList<byte[]> list = new ArrayList<byte[]>(); | ||
for (int i = 0; i < 100000; i++) { | ||
list.add(new byte[1024*1024/2]); | ||
} | ||
} | ||
|
||
public void sleep(){ | ||
try { | ||
Thread.sleep(1000_1000); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.