Skip to content

Commit

Permalink
release 2.0.0 (2024-10-15)
Browse files Browse the repository at this point in the history
  • Loading branch information
fanyong920 committed Oct 15, 2024
1 parent dce81d8 commit 93778af
Show file tree
Hide file tree
Showing 419 changed files with 21,529 additions and 15,481 deletions.
60 changes: 38 additions & 22 deletions 1.1.5版本之前的内存问题解决方案.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@


# 解决思路:

通过获取 PID ,执行shell命令杀死进程。

如果单单在需要 LINUX 上获取 PID,可以不导入 JNA 包,LINUX上获取 PID 可以 通过反射得到。

更高版本的 JDK ,通过JDK 自带的方法就能获取。

## step 1 导入jna包

```xml
<dependency>
<groupId>net.java.dev.jna</groupId>
Expand All @@ -17,11 +28,13 @@ public interface Kernel32 extends StdCallLibrary {
## step 3 写获取进程id的方法

````java
/**
* 获取进程id
* @param process chrome进程 可以通过{@link com.ruiyun.jvppeteer.core.browser.Browser#process()} 获取}
* @return 进程id
*/
import com.ruiyun.jvppeteer.core.Browser;

/**
* 获取进程id
* @param process chrome进程 可以通过{@link com.ruiyun.jvppeteer.core.Browser#process()} 获取}
* @return 进程id
*/
public static String getProcessId(Process process) {
long pid = -1;
Field field;
Expand All @@ -31,7 +44,7 @@ public static String getProcessId(Process process) {
field.setAccessible(true);
pid = BrowserRunner.Kernel32.INSTANCE.GetProcessId((Long) field.get(process));
} catch (Exception e) {
LOGGER.error("Failed to get processId on Windows platform.",e);
LOGGER.error("Failed to get processId on Windows platform.", e);
}
} else if (Platform.isLinux() || Platform.isAIX()) {
try {
Expand All @@ -48,7 +61,7 @@ public static String getProcessId(Process process) {
field.setAccessible(true);
pid = (Integer) field.get(process);
} catch (Throwable e) {
LOGGER.error("Failed to get processId on Linux or Aix platform.",e);
LOGGER.error("Failed to get processId on Linux or Aix platform.", e);
}
}
return String.valueOf(pid);
Expand Down Expand Up @@ -88,17 +101,18 @@ public boolean kill() {
}
````
## 完整代码示例

````java

package com.ruiyun.example;

import com.ruiyun.jvppeteer.core.Constant;
import com.ruiyun.jvppeteer.common.Constant;
import com.ruiyun.jvppeteer.core.Puppeteer;
import com.ruiyun.jvppeteer.core.browser.Browser;
import com.ruiyun.jvppeteer.core.browser.BrowserRunner;
import com.ruiyun.jvppeteer.core.page.Page;
import com.ruiyun.jvppeteer.options.LaunchOptions;
import com.ruiyun.jvppeteer.options.LaunchOptionsBuilder;
import com.ruiyun.jvppeteer.core.Browser;
import com.ruiyun.jvppeteer.core.BrowserRunner;
import com.ruiyun.jvppeteer.core.Page;
import com.ruiyun.jvppeteer.entities.LaunchOptions;
import com.ruiyun.jvppeteer.entities.LaunchOptionsBuilder;
import com.ruiyun.jvppeteer.util.StringUtil;
import com.sun.jna.Native;
import com.sun.jna.Platform;
Expand All @@ -119,24 +133,26 @@ public class KillExample {
/**
* 多个browser的时候用pids储存pid
*/
private static Map<String,Process> pids = new HashMap<>();
private static Map<String, Process> pids = new HashMap<>();

public static void main(String[] args) throws IOException, InterruptedException {
LaunchOptions launchOptions = new LaunchOptionsBuilder().withExecutablePath("C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe").withIgnoreDefaultArgs(Collections.singletonList("--enable-automation")).withHeadless(false).build();
Browser browser = Puppeteer.launch(launchOptions);
Page page = browser.newPage();
page.goTo("https://www.baidu.com/?tn=98012088_10_dg&ch=3");
Process process = browser.process();
String processId = getProcessId(process);
KillExample.LOGGER.info("process pid {}",processId);
KillExample.LOGGER.info("process pid {}", processId);
// 做一些其他操作


browser.close(); //可以关闭websocket连接
kill(processId);
}

public interface Kernel32 extends StdCallLibrary {
Kernel32 INSTANCE = Native.load("kernel32", Kernel32.class);

long GetProcessId(Long hProcess);
}

Expand All @@ -149,7 +165,7 @@ public class KillExample {
field.setAccessible(true);
pid = KillExample.Kernel32.INSTANCE.GetProcessId((Long) field.get(process));
} catch (Exception e) {
KillExample.LOGGER.error("Failed to get processId on Windows platform.",e);
KillExample.LOGGER.error("Failed to get processId on Windows platform.", e);
}
} else if (Platform.isLinux() || Platform.isAIX()) {
try {
Expand All @@ -166,19 +182,19 @@ public class KillExample {
field.setAccessible(true);
pid = (Integer) field.get(process);
} catch (Throwable e) {
KillExample.LOGGER.error("Failed to get processId on Linux or Aix platform.",e);
KillExample.LOGGER.error("Failed to get processId on Linux or Aix platform.", e);
}
}
return String.valueOf(pid);
}

public static boolean kill(String pid) {
try {
if("-1".equals(pid)){
if ("-1".equals(pid)) {
LOGGER.warn("Chrome process pid is -1,will not use kill cmd");
return false;
}
if(StringUtil.isEmpty(pid) ){
if (StringUtil.isEmpty(pid)) {
LOGGER.warn("Chrome process pid is empty,will not use kill cmd");
return false;
}
Expand All @@ -189,7 +205,7 @@ public class KillExample {
exec = Runtime.getRuntime().exec(command);
} else if (Platform.isLinux() || Platform.isAIX()) {
command = "kill -9 " + pid;
exec = Runtime.getRuntime().exec(new String[]{"/bin/sh","-c",command});
exec = Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", command});
}
if (exec != null) {
return exec.waitFor(Constant.DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
Expand Down
Loading

0 comments on commit 93778af

Please sign in to comment.