java - 在 Java 中,是否可以增加 JVM 的可用内存和/或杀死其他 Java 程序?

标签 java memory process jvm

我对高级 Java 缺乏经验,请多多包涵。

我对 Java 实现可称为“自主”的功能的能力感到好奇。假设我们有两个 Java 程序正在运行。并且一个程序确定另一个程序正在占用内存,因此会终止该程序和/或为 JVM 分配更多内存。

我知道在 Java 中你可以看到可用内存是多少(见 How to do I check CPU and Memory Usage in Java?),但是如果我们想深入挖掘呢?

谢谢。

最佳答案

You Asked:-is it possible to increase the JVM's available memory and/or kill other Java programs?

  1. 是的,可以增加jvm的堆大小喜欢:

          java -Xmx512M ClassName //512M = memory you want to increase
    
  2. 杀死一个进程,例如:taskkill /F /IM <processname>.exe

注意
->但这不是一个好主意,因为堆可能是固定大小的,或者可能会根据垃圾收集器的策略进行扩展。
->打开你的命令提示符类型taskkill/?了解详情。

3 .并且您可以查看 jvm 当前保留的 total memoryfree memorymax memory 以这种方式。

          int  MegaBytes = 1024*1024 ;

          long  freeMemory = Runtime.getRuntime().freeMemory() / MegaBytes;
          long  totalMemory = Runtime.getRuntime().totalMemory() / MegaBytes;
          long  maxMemory = Runtime.getRuntime().maxMemory() / MegaBytes;

          System.out.println("Memory used by JVM: " + (maxMemory - freeMemory));
          System.out.println("freeMemory in JVM: " + freeMemory);
          System.out.println("totalMemory in JVM : " + totalMemory);
          System.out.println("maxMemory in JVM: " + maxMemory);

更多信息please check here .

关于java - 在 Java 中,是否可以增加 JVM 的可用内存和/或杀死其他 Java 程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17204757/

相关文章:

java - 耶拿 TDB : Querying a named model

java - Spring 数据 JPA 存储库匹配所有列或整个 pojo

java - 如何调用泛型类的父构造函数

dll - 如何查找加载到进程中的 DLL 及其位置等

c# - Process.Start(...) 抛出 "The system cannot find the file specified"

python - 检查第二个脚本是否正在运行或已经完成

java密码字段: dot code

java - 将样式表应用到 xml 文档的内存消耗最少的方法是什么?

memory - 什么是 "tagged memory"?

C:数组元素可用性差异?