Skip to content

Commit ba029ed

Browse files
committed
各种学习
1 parent ceee054 commit ba029ed

11 files changed

Lines changed: 339 additions & 1 deletion

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package JVM;
2+
3+
import typeInfo.compilerAPI.JavaCompilerTask;
4+
5+
/**
6+
* 方法区测试
7+
* jdk1.7及以前 永久区(Perm),-XX:PermSize ,-XX:MaxPermSize
8+
* jdk1.8及以后,元数据区,-XX:MetaspaceSize,-XX:MaxMetaspaceSize
9+
*
10+
* @author :chenxin
11+
* @date :Created in 2020/3/26 13:36
12+
* @description:方法区测试
13+
* @modified By:
14+
* @version: $
15+
*/
16+
public class MethodAreaTest {
17+
public static void main(String[] args) {
18+
new MethodAreaTest().loadClass();
19+
}
20+
21+
void loadClass() {
22+
String className = "Hello";
23+
for (int i = 0; i < 10000; i++) {
24+
String realName = className + i;
25+
System.out.println(i);
26+
StringBuilder s = new StringBuilder();
27+
s.append("public class " + realName + "{\n");
28+
s.append("public static void main(String[] args){\n");
29+
s.append("System.out.println(\"Hello World\");\n");
30+
s.append("}\n");
31+
s.append("}");
32+
Class<?> compile = JavaCompilerTask.compile(realName, s.toString());
33+
}
34+
}
35+
36+
37+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package JVM.gc;
2+
3+
/**
4+
* 来自于《实战Java虚拟机》
5+
* -Xms5M -Xmx20M -XX:+PrintGCDetails -XX:+PrintCommandLineFlags -XX:+UseSerialGC
6+
* @author Tom
7+
*
8+
*/
9+
public class HeapAlloc {
10+
11+
public static void main(String[] args) {
12+
13+
printMemoryInfo();
14+
byte[] b = new byte[1*1024*1024];
15+
System.out.println("分配1MB空间");
16+
17+
printMemoryInfo();
18+
b = new byte[4*1024*1024];
19+
System.out.println("分配4MB空间");
20+
21+
printMemoryInfo();
22+
}
23+
24+
public static void printMemoryInfo()
25+
{
26+
System.out.print("maxMemory=");
27+
System.out.println(Runtime.getRuntime().maxMemory()/1024.0/1024.0 + " MB");
28+
System.out.print("freeMemory=");
29+
System.out.println(Runtime.getRuntime().freeMemory()/1024.0/1024.0 + " MB");
30+
System.out.print("totalMemory=");
31+
System.out.println(Runtime.getRuntime().totalMemory()/1024.0/1024.0 + " MB");
32+
}
33+
34+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package JVM.gc;
2+
3+
/**
4+
* 来自于《实战Java虚拟机》
5+
* -Xmx20m -Xms20m -Xmn1m -XX:SurvivorRatio=2 -XX:+PrintGCDetails -XX:+UseSerialGC
6+
* 新生代1M,eden/s0=2, eden 512KB, s0=s1=256KB
7+
* 新生代无法容纳1M,所以直接放老年代
8+
*
9+
* -Xmx20m -Xms20m -Xmn7m -XX:SurvivorRatio=2 -XX:+PrintGCDetails -XX:+UseSerialGC
10+
* 新生代7M,eden/s0=2, eden=3.5M, s0=s1=1.75M
11+
* 所以可以容纳几个数组,但是无法容纳所有,因此发生GC
12+
*
13+
* -Xmx20m -Xms20m -Xmn15m -XX:SurvivorRatio=8 -XX:+PrintGCDetails -XX:+UseSerialGC
14+
* 新生代15M,eden/s0=8, eden=12M, s0=s1=1.5M
15+
*
16+
* -Xmx20m -Xms20m -XX:NewRatio=2 -XX:+PrintGCDetails -XX:+UseSerialGC
17+
* 新生代是6.6M,老年代13.3M
18+
* @author Tom
19+
*
20+
*/
21+
public class NewSizeDemo {
22+
23+
public static void main(String[] args) {
24+
25+
byte[] b = null;
26+
for(int i=0;i<10;i++)
27+
{
28+
b = new byte[1*1024*1024];
29+
}
30+
}
31+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package JVM.gc.reference;
2+
3+
import java.lang.ref.PhantomReference;
4+
import java.lang.ref.ReferenceQueue;
5+
6+
/**
7+
* 虚引用测试
8+
*
9+
* @author :chenxin
10+
* @date :Created in 2020/3/26 15:01
11+
* @description:虚引用测试
12+
* @modified By:
13+
* @version: $
14+
*/
15+
public class PhantomReferenceTest {
16+
public static void main(String[] args) {
17+
ReferenceQueue<StringBuilder> queue = new ReferenceQueue<StringBuilder>();
18+
// final CheckReferenceQueue checkReferenceQueue = new CheckReferenceQueue(queue);
19+
StringBuilder s1 = new StringBuilder();
20+
for (int i = 0; i < 10000; i++) {
21+
s1.append("00000000000");
22+
}
23+
24+
// PhantomReference<StringBuilder> s2 = new PhantomReference<StringBuilder>(s1);
25+
26+
s1 = null;
27+
}
28+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package JVM.gc.reference;
2+
3+
import java.lang.ref.SoftReference;
4+
5+
/**
6+
* 软引用测试
7+
*
8+
* @author :chenxin
9+
* @date :Created in 2020/3/26 14:38
10+
* @description:软引用测试
11+
* @modified By:
12+
* @version: $
13+
*/
14+
public class SoftReferenceTest {
15+
public static void main(String[] args) {
16+
StringBuilder s1 = new StringBuilder();
17+
for (int i = 0; i < 10000; i++) {
18+
s1.append("0000");
19+
}
20+
21+
SoftReference<StringBuilder> s2 = new SoftReference<StringBuilder>(s1);
22+
23+
s1 = null;
24+
25+
System.out.println(s2.get().length());
26+
27+
System.gc();
28+
//软引用,内存不紧张,则不会回收
29+
System.out.println(s2.get().length());
30+
31+
final byte[] bytes = new byte[(int) (1024 * 1024 * 3)];
32+
33+
System.gc();
34+
35+
System.out.println(s2.get() == null);
36+
37+
}
38+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package JVM.gc.reference;
2+
3+
/**
4+
* 强引用测试
5+
*
6+
* @author :chenxin
7+
* @date :Created in 2020/3/26 14:32
8+
* @description:强引用测试
9+
* @modified By:
10+
* @version: $
11+
*/
12+
public class StrongReferenceTest {
13+
14+
public static void main(String[] args) {
15+
StringBuilder s1 = new StringBuilder();
16+
for (int i = 0; i < 10000; i++) {
17+
s1.append("00000000000000000000");
18+
}
19+
StringBuilder s2 = s1;
20+
//s1为null但是s2依然占据着内存
21+
s1 = null;
22+
// s2 = null;
23+
System.gc();
24+
//垃圾回收,当时不能回收强类型的引用,内存被占用 ,下面语句报错
25+
26+
final byte[] bytes = new byte[1024 * 1024 * 3];
27+
28+
}
29+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package JVM.gc.reference;
2+
3+
import java.lang.ref.SoftReference;
4+
import java.lang.ref.WeakReference;
5+
6+
/**
7+
* 弱引用测试
8+
*
9+
* @author :chenxin
10+
* @date :Created in 2020/3/26 14:51
11+
* @description:弱引用测试
12+
* @modified By:
13+
* @version: $
14+
*/
15+
public class WeakReferenceTest {
16+
public static void main(String[] args) {
17+
StringBuilder s1 = new StringBuilder();
18+
for (int i = 0; i < 10000; i++) {
19+
s1.append("00000000000");
20+
}
21+
22+
WeakReference<StringBuilder> s2 = new WeakReference<StringBuilder>(s1);
23+
24+
s1 = null;
25+
26+
System.out.println(s2.get().length());
27+
//弱引用只要gc就会被回收
28+
System.gc();
29+
System.out.println(s2 == null);
30+
Byte[] bytes = new Byte[(int) (1024 * 1024 * 3.5)];
31+
32+
33+
}
34+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package JVM.jmx.MBean;
2+
3+
/* Hello.java - MBean implementation for the Hello World MBean.
4+
This class must implement all the Java methods declared in the
5+
HelloMBean interface, with the appropriate behavior for each one. */
6+
7+
public class Hello implements HelloMBean {
8+
public void sayHello() {
9+
System.out.println("hello, world");
10+
}
11+
12+
public int add(int x, int y) {
13+
return x + y;
14+
}
15+
16+
/*
17+
* Getter for the Name attribute. The pattern shown here is frequent: the getter
18+
* returns a private field representing the attribute value. In our case, the
19+
* attribute value never changes, but for other attributes it might change as
20+
* the application runs. Consider an attribute representing statistics such as
21+
* uptime or memory usage, for example. Being read-only just means that it can't
22+
* be changed through the management interface.
23+
*/
24+
public String getName() {
25+
return this.name;
26+
}
27+
28+
/*
29+
* Getter for the CacheSize attribute. The pattern shown here is frequent: the
30+
* getter returns a private field representing the attribute value, and the
31+
* setter changes that field.
32+
*/
33+
public int getCacheSize() {
34+
return this.cacheSize;
35+
}
36+
37+
/*
38+
* Setter for the CacheSize attribute. To avoid problems with stale values in
39+
* multithreaded situations, it is a good idea for setters to be synchronized.
40+
*/
41+
public synchronized void setCacheSize(int size) {
42+
this.cacheSize = size;
43+
44+
/*
45+
* In a real application, changing the attribute would typically have effects
46+
* beyond just modifying the cacheSize field. For example, resizing the cache
47+
* might mean discarding entries or allocating new ones. The logic for these
48+
* effects would be here.
49+
*/
50+
System.out.println("Cache size now " + this.cacheSize);
51+
}
52+
53+
private final String name = "Reginald";
54+
private int cacheSize = DEFAULT_CACHE_SIZE;
55+
private static final int DEFAULT_CACHE_SIZE = 200;
56+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package JVM.jmx.MBean;
2+
3+
public interface HelloMBean {
4+
// operations
5+
6+
public void sayHello();
7+
8+
public int add(int x, int y);
9+
10+
// attributes
11+
12+
// a read-only attribute called Name of type String
13+
public String getName();
14+
15+
// a read-write attribute called CacheSize of type int
16+
public int getCacheSize();
17+
18+
public void setCacheSize(int size);
19+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package JVM.jmx.MBean;
2+
3+
/* Main.java - main class for Hello World example. Create the
4+
HelloWorld MBean, register it, then wait forever (or until the
5+
program is interrupted). */
6+
7+
import java.lang.management.*;
8+
import javax.management.*;
9+
10+
public class Main {
11+
/*
12+
* For simplicity, we declare "throws Exception". Real programs will usually
13+
* want finer-grained exception handling.
14+
*/
15+
public static void main(String[] args) throws Exception {
16+
// Get the Platform MBean Server
17+
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
18+
19+
// Construct the ObjectName for the MBean we will register
20+
ObjectName name = new ObjectName("com.example.mbeans:type=Hello");
21+
22+
// Create the Hello World MBean
23+
Hello mbean = new Hello();
24+
25+
// Register the Hello World MBean
26+
mbs.registerMBean(mbean, name);
27+
28+
// Wait forever
29+
System.out.println("Waiting forever...");
30+
Thread.sleep(Long.MAX_VALUE);
31+
}
32+
}

0 commit comments

Comments
 (0)