From e76d89f77490c16b5295c72fadbb8526c1f3881a Mon Sep 17 00:00:00 2001 From: ehlxr Date: Thu, 12 Aug 2021 23:15:14 +0800 Subject: [PATCH] update at 2021-08-12 23:15:14 by ehlxr --- src/main/java/io/github/ehlxr/http/Main.java | 62 +++++----- .../java/io/github/ehlxr/test/OOMTest.java | 56 +++++++++ .../io/github/ehlxr/test/StudentTrace.java | 111 ++++++++++++++++++ src/main/java/io/github/ehlxr/test/Test.java | 5 +- 4 files changed, 202 insertions(+), 32 deletions(-) create mode 100644 src/main/java/io/github/ehlxr/test/OOMTest.java create mode 100644 src/main/java/io/github/ehlxr/test/StudentTrace.java diff --git a/src/main/java/io/github/ehlxr/http/Main.java b/src/main/java/io/github/ehlxr/http/Main.java index 571e709..7deb58c 100644 --- a/src/main/java/io/github/ehlxr/http/Main.java +++ b/src/main/java/io/github/ehlxr/http/Main.java @@ -26,9 +26,9 @@ package io.github.ehlxr.http; import java.io.IOException; import java.net.URI; -import java.net.http.HttpClient; -import java.net.http.HttpRequest; -import java.net.http.HttpResponse; +// import java.net.http.HttpClient; +// import java.net.http.HttpRequest; +// import java.net.http.HttpResponse; import java.time.Duration; /** @@ -38,34 +38,34 @@ import java.time.Duration; public class Main { public static void main(String[] args) throws IOException, InterruptedException { - HttpClient client = HttpClient.newBuilder() - .version(HttpClient.Version.HTTP_1_1) - .followRedirects(HttpClient.Redirect.NORMAL) - .connectTimeout(Duration.ofSeconds(20)) - // .proxy(ProxySelector.of(new InetSocketAddress("proxy.example.com", 80))) - // .authenticator(Authenticator.getDefault()) - .build(); - - HttpRequest request = HttpRequest.newBuilder() - .uri(URI.create("https://ehlxr.me/")) - .timeout(Duration.ofMinutes(2)) - .header("Content-Type", "application/json") - .GET() - // .POST(HttpRequest.BodyPublishers.ofFile(Paths.get("file.json"))) - .build(); - - // Synchronous Example - // HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); - // System.out.println(response.statusCode()); - // System.out.println(response.body()); - - - // Asynchronous Example - - client.sendAsync(request, HttpResponse.BodyHandlers.ofString()) - .thenApply(HttpResponse::body) - .thenAccept(body -> System.out.println("response body is:\n" + body)) - .join(); + // HttpClient client = HttpClient.newBuilder() + // .version(HttpClient.Version.HTTP_1_1) + // .followRedirects(HttpClient.Redirect.NORMAL) + // .connectTimeout(Duration.ofSeconds(20)) + // // .proxy(ProxySelector.of(new InetSocketAddress("proxy.example.com", 80))) + // // .authenticator(Authenticator.getDefault()) + // .build(); + // + // HttpRequest request = HttpRequest.newBuilder() + // .uri(URI.create("https://ehlxr.me/")) + // .timeout(Duration.ofMinutes(2)) + // .header("Content-Type", "application/json") + // .GET() + // // .POST(HttpRequest.BodyPublishers.ofFile(Paths.get("file.json"))) + // .build(); + // + // // Synchronous Example + // // HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); + // // System.out.println(response.statusCode()); + // // System.out.println(response.body()); + // + // + // // Asynchronous Example + // + // client.sendAsync(request, HttpResponse.BodyHandlers.ofString()) + // .thenApply(HttpResponse::body) + // .thenAccept(body -> System.out.println("response body is:\n" + body)) + // .join(); } } diff --git a/src/main/java/io/github/ehlxr/test/OOMTest.java b/src/main/java/io/github/ehlxr/test/OOMTest.java new file mode 100644 index 0000000..acfcc49 --- /dev/null +++ b/src/main/java/io/github/ehlxr/test/OOMTest.java @@ -0,0 +1,56 @@ +/* + * The MIT License (MIT) + * + * Copyright © 2021 xrv + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package io.github.ehlxr.test; + +import java.util.ArrayList; +import java.util.Random; + +/** + * -Xms600m -Xmx600m -XX:SurvivorRatio=8 -XX:+PrintGCDetails -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/Users/ehlxr/Downloads/hprof/1.hprof + * + * @author ehlxr + * @since 2021-08-08 21:26. + */ +public class OOMTest { + public static void main(String[] args) { + ArrayList list = new ArrayList<>(); + while (true) { + try { + Thread.sleep(5); + } catch (InterruptedException e) { + e.printStackTrace(); + } + list.add(new Picture(new Random().nextInt(100 * 50))); + } + } +} + +class Picture { + private byte[] pixels; + + public Picture(int length) { + this.pixels = new byte[length]; + } +} diff --git a/src/main/java/io/github/ehlxr/test/StudentTrace.java b/src/main/java/io/github/ehlxr/test/StudentTrace.java new file mode 100644 index 0000000..4cdd665 --- /dev/null +++ b/src/main/java/io/github/ehlxr/test/StudentTrace.java @@ -0,0 +1,111 @@ +package io.github.ehlxr.test; + +import java.util.ArrayList; +import java.util.List; + +/** + * 有一个学生浏览网页的记录程序,它将记录 每个学生访问过的网站地址。 + * 它由三个部分组成:Student、WebPage和StudentTrace三个类 + *

+ * -XX:+HeapDumpBeforeFullGC -XX:HeapDumpPath=/Users/ehlxr/Downloads/hprof/student.hprof + * + * @author shkstart + * @create 16:11 + */ +public class StudentTrace { + static List webpages = new ArrayList(); + + + public static void createWebPages() { + for (int i = 0; i < 100; i++) { + WebPage wp = new WebPage(); + wp.setUrl("http://www." + Integer.toString(i) + ".com"); + wp.setContent(Integer.toString(i)); + webpages.add(wp); + } + } + + public static void main(String[] args) { + createWebPages();//创建了100个网页 + //创建3个学生对象 + Student st3 = new Student(3, "Tom"); + Student st5 = new Student(5, "Jerry"); + Student st7 = new Student(7, "Lily"); + + for (int i = 0; i < webpages.size(); i++) { + if (i % st3.getId() == 0) + st3.visit(webpages.get(i)); + if (i % st5.getId() == 0) + st5.visit(webpages.get(i)); + if (i % st7.getId() == 0) + st7.visit(webpages.get(i)); + } + webpages.clear(); + System.gc(); + + } +} + +class Student { + private int id; + private String name; + private List history = new ArrayList<>(); + + public Student(int id, String name) { + super(); + this.id = id; + this.name = name; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public List getHistory() { + return history; + } + + public void setHistory(List history) { + this.history = history; + } + + public void visit(WebPage wp) { + if (wp != null) { + history.add(wp); + } + } +} + + +class WebPage { + private String url; + private String content; + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } +} diff --git a/src/main/java/io/github/ehlxr/test/Test.java b/src/main/java/io/github/ehlxr/test/Test.java index 5333b9c..ca2a20f 100644 --- a/src/main/java/io/github/ehlxr/test/Test.java +++ b/src/main/java/io/github/ehlxr/test/Test.java @@ -24,6 +24,7 @@ package io.github.ehlxr.test; +import java.io.IOException; import java.util.Arrays; import java.util.LinkedHashMap; import java.util.List; @@ -34,7 +35,7 @@ import java.util.stream.Collectors; * Created by ehlxr on 2016/12/15. */ public class Test { - public static void main(String[] args) { + public static void main(String[] args) throws IOException { // String s0 = "kvill"; // String s1 = "kvill"; // String s2 = "kvill"; @@ -84,5 +85,7 @@ public class Test { System.out.println(s1 == s2); System.out.println(s == t); // false System.out.println(s.intern() == t.intern()); // true + + System.in.read(); } }