- 浏览: 62427 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
a78113534:
用eclipse编译成jar导入到android里用了,没有成 ...
Spads 公式解析系统 - Java -
Surmounting:
ddbbccaa 写道2.1的题意无法看明白。(你真有工作责任 ...
面试软件设计人员的方法,附面试题。 我的面试注重实效 -
ddbbccaa:
2.1的题意无法看明白。(你真有工作责任心吗?)
面试软件设计人员的方法,附面试题。 我的面试注重实效 -
Surmounting:
尔今尔后 写道这种题 纯粹是 自杀式面试是受试者自杀,还是面试 ...
面试软件设计人员的方法,附面试题。 我的面试注重实效 -
尔今尔后:
这种题 纯粹是 自杀式面试
面试软件设计人员的方法,附面试题。 我的面试注重实效
这些日子要用爪哇语言(Java)做内存数据中心。于是把用 Java 监控运行环境硬件资源的内容复习了一下。爪哇类库提供了 java.util.Runtim 类,主要负责调用爪哇虚拟机(JavaVM)外部的基层操作系统功能、处理基于一种叫钩子的原理的程序、获取系统资源信息以及控制调试信息生成。本文单独利用其获取系统资源信息的功能。
java.util.Runtim 类具有以下几个方法和获取系统资源信息有关。以下代码可不是简简单单从标准类库里边复制出来的哦。全球目前独此一份。
/** * 返回爪哇(Java)虚拟机可用线程数。 * * <p>该值在特定的虚拟机调用期间可能发生更改。因此,对可用处理器数目很敏感的 * 应用程序应该不定期地轮询该属性,并相应地调整其资源用法。</p> * * @return 虚拟机可用的最大处理器数目;从不小于 1 * @since 1.4 */ public native int availableProcessors(); /** * 返回爪哇(Java)虚拟机中的空闲内存量。调用 <code>gc</code> 方法可能导致 * <code>freeMemory</code> 返回值的增加。 * * @return 供将来分配对象使用的当前可用内存的近似总量,以字节为单位。 */ public native long freeMemory(); /** * 返回爪哇(Java)虚拟机中的内存总量。此方法返回的值可能随时间的推移而变化,这 * 取决于主机环境。 * <p> * 注意,保存一个给定类型的对象所需的内存量可能取决于实现方式。 * * @return 目前为当前和后续对象提供的内存总量,以字节为单位。 */ public native long totalMemory(); // // /** * 返回爪哇(Java)虚拟机能够尝试使用的最大内存量。如果内存本身没有限制,则 * 返回值 {@link java.lang.Long#MAX_VALUE} 。 </p> * * @return 虚拟机能够尝试使用的最大内存量,以字节为单位。 * @since 1.4 */ public native long maxMemory(); /** * 运行垃圾回收器。 * 调用此方法意味着爪哇(Java)虚拟机做了一些努力来回收未用对象,以便能够快速地 * 重用这些对象当前占用的内存。当控制从方法调用中返回时,虚拟机已经尽最大努力回收 * 了所有丢弃的对象。 * <p> * 名称 <code>gc</code> 代表“垃圾回收器”。虚拟机根据需要在单独的线程中自动执行 * 回收过程,甚至不用显式调用 <code>gc</code> 方法。 * <p> * 方法 {@link System#gc()} 是调用此方法的一种传统而便捷的方式 */ public native void gc();
我们可以看到这些都是本地方法。这意味着将 Runtime 对象远程传递之后,将不能得到正确执行结果。
这些方法用起来都很简单,文档注释也写得比较明白。
在高可用数据中心中,我认为应该根据可用 CPU 线数决定程序开启的线程数
。此线程数为 CPU 可用线数的某倍数。此倍数应通过实际经验所得。然后程序通过监控 CPU 可用线数,来控制线程池保留数量。
内存的控制,我想应该是在内存超出警戒线时发出警报
,以向运营人员申请增加内存数据中心服务器。同时,应该在内存过满之前,由程序执行垃圾回收
,以消除并无引用的老生代对象。
如果各位有对内存数据中心的想法、建议或者质疑,欢迎来一起讨论。
下边是我编写的一个系统资源测试程序。程序里边使用了 Runtime 类关于系统资源的所有方法。
package cn.spads.test.grammar; import java.util.LinkedList; import java.util.Random; /** * 本类用于示范使用 Runtime 检查系统运行情况。 * 将 Runtime 作为可变成员,是为多系统公用检查预留的设计。 * @author Shane Loo Li */ public class PerformanceMonitor { /** * 此量控制程序运行时间。此值越大,此演示程序运行时间越长。 */ static public int runLoopTimes = 55; /** * 此为每次检测间隔的时间片数。此值越大,间隔时间越长。 */ static public int waitTime = 1500000; static public void main(String[] arguments) throws Exception { Runtime context = Runtime.getRuntime(); final PerformanceMonitor monitor = new PerformanceMonitor(context); final LinkedList<String> pretendedMemory = new LinkedList<String>(); new Thread( new Runnable() { public void run() { for (int j = -1; ++j != runLoopTimes; ) { // 检查系统情况 monitor.checkAll(); // 每次检查运行情况之后,都会休息 1000 个时间片 for (int i = -1; ++i != waitTime; ) Thread.yield(); // 每次检查之后,会制造一些对象,记录其中一部分,并删除些老对象 for (int i = -1; ++i != 20000; ) { StringBuilder builder = new StringBuilder(); Random ran = new Random(); for (int index = -1; ++index != 100; ) builder.append((char) (ran.nextInt(26) + 64)); String garbage = new String(builder.toString()); garbage = garbage.substring(garbage.length()); pretendedMemory.add(builder.toString()); } int deleteCount = new Random().nextInt(15000); for (int i = -1; ++i != deleteCount; ) pretendedMemory.removeFirst(); System.out.println("-----------"); } } } ).start(); } private Runtime context; private double maxFreeMemory; private long lastFreeMemory; private long lastTotalMemory; private long lastMaxMemory; private double lastMemoryRate; public PerformanceMonitor(Runtime context) { this.context = context; } public void checkAll() { this.monitorMemory(); this.monitorCpu(); } /** * 本方法比较当前空余内存与历史记录最大空余内存的关系。若空余内存过小,则执行垃圾回收。 */ public void monitorMemory() { // 求空余内存,并计算空余内存比起最大空余内存的比例 long freeMemory = this.context.freeMemory(); if (freeMemory > this.maxFreeMemory) this.maxFreeMemory = Long.valueOf(freeMemory).doubleValue(); double memoryRate = freeMemory / this.maxFreeMemory; System.out.println("There are " + memoryRate * 100 + "% free memory."); // 如果内存空余率在变小,则一切正常;否则需要报告内存变化情况 if (memoryRate >= this.lastMemoryRate) this.reportMemoryChange(); // 如果内存空余率很低,则执行内存回收 if (freeMemory / this.maxFreeMemory < 0.3) { System.out.print("System will start memory Garbage Collection."); System.out.println(" Now we have " + freeMemory / 1000 + " KB free memory."); this.context.gc(); System.out.println("After the Garbage Collection, we have " + this.context.freeMemory() / 1000 + " KB free memory."); } // 记录内存信息 this.recordMemoryInfo(memoryRate); } /** * 报告内存变化情况 */ private void reportMemoryChange() { System.out.print("Last freeMemory = " + this.lastFreeMemory / 1000 + " KB,"); System.out.println(" now it is " + this.context.freeMemory() / 1000 + " KB."); System.out.print("Last totalMemory = " + this.lastTotalMemory / 1000 + " KB,"); System.out.println(" now it is " + this.context.totalMemory() / 1000 + " KB."); System.out.print("Last maxMemory = " + this.lastMaxMemory / 1000 + " KB,"); System.out.println(" now it is " + this.context.maxMemory() / 1000 + " KB."); } /** * 记录本次内存信息。 */ private void recordMemoryInfo(double memoryRate) { this.lastFreeMemory = this.context.freeMemory(); this.lastMaxMemory = this.context.maxMemory(); this.lastTotalMemory = this.context.totalMemory(); this.lastMemoryRate = memoryRate; } /** * 监测 CPU 的方法。 */ public void monitorCpu() { int cpuCount = this.context.availableProcessors(); if (cpuCount > 1) System.out.println("CPU have " + cpuCount + " processors."); } }
我运行这段程序,得到结果报告。通过观察结果报告,我得到了一些新结论。
首先, maxMemory 在一台机器只运行一个爪哇(Java)虚拟机的前提下,一般来说并不会变动。
其次,新生代内存会很快地自动回收,这体现在 freeMemory 总是不断少量自动增加上。
最后,爪哇(Java)虚拟机会在内存不足时,自己申请新的内存。这个内存空间也能够根据报告大概看出一点原则:既不是完全通过可用内存比例,也不是完全通过可用内存数量。
我自己运行的报告附在下边
There are 100.0% free memory. Last freeMemory = 0 KB, now it is 124371 KB. Last totalMemory = 0 KB, now it is 126353 KB. Last maxMemory = 0 KB, now it is 1875378 KB. CPU have 8 processors. ----------- There are 79.91675736339026% free memory. CPU have 8 processors. ----------- There are 82.1353751773145% free memory. Last freeMemory = 99921 KB, now it is 102695 KB. Last totalMemory = 126353 KB, now it is 126353 KB. Last maxMemory = 1875378 KB, now it is 1875378 KB. CPU have 8 processors. ----------- There are 100.0% free memory. Last freeMemory = 102695 KB, now it is 140522 KB. Last totalMemory = 126353 KB, now it is 159383 KB. Last maxMemory = 1875378 KB, now it is 1875378 KB. CPU have 8 processors. ----------- There are 82.4930651724349% free memory. CPU have 8 processors. ----------- There are 65.90519105111919% free memory. CPU have 8 processors. ----------- There are 88.50612783993465% free memory. Last freeMemory = 92611 KB, now it is 124371 KB. Last totalMemory = 159383 KB, now it is 159383 KB. Last maxMemory = 1875378 KB, now it is 1875378 KB. CPU have 8 processors. ----------- There are 71.68576727159902% free memory. CPU have 8 processors. ----------- There are 54.86540670326339% free memory. CPU have 8 processors. ----------- There are 100.0% free memory. Last freeMemory = 77098 KB, now it is 172330 KB. Last totalMemory = 159383 KB, now it is 225443 KB. Last maxMemory = 1875378 KB, now it is 1875378 KB. CPU have 8 processors. ----------- There are 86.18976806966614% free memory. CPU have 8 processors. ----------- There are 72.37916940114857% free memory. CPU have 8 processors. ----------- There are 58.56890497502629% free memory. CPU have 8 processors. ----------- There are 46.292794574206056% free memory. CPU have 8 processors. ----------- There are 92.98754812452182% free memory. Last freeMemory = 79776 KB, now it is 160245 KB. Last totalMemory = 225443 KB, now it is 225443 KB. Last maxMemory = 1875378 KB, now it is 1875378 KB. CPU have 8 processors. ----------- There are 79.17694945600425% free memory. CPU have 8 processors. ----------- There are 65.36668502988196% free memory. CPU have 8 processors. ----------- There are 51.556035296554015% free memory. CPU have 8 processors. ----------- There are 37.745845146519564% free memory. CPU have 8 processors. ----------- There are 23.935246478001996% free memory. System will start memory Garbage Collection. Now we have 41247 KB free memory. After the Garbage Collection, we have 312897 KB free memory. CPU have 8 processors. ----------- There are 100.0% free memory. Last freeMemory = 312897 KB, now it is 292267 KB. Last totalMemory = 380108 KB, now it is 380108 KB. Last maxMemory = 1875378 KB, now it is 1875378 KB. CPU have 8 processors. ----------- There are 91.1770148111291% free memory. CPU have 8 processors. ----------- There are 84.11856206174096% free memory. CPU have 8 processors. ----------- There are 75.29548107031924% free memory. CPU have 8 processors. ----------- There are 68.2370940141088% free memory. CPU have 8 processors. ----------- There are 59.414100613590705% free memory. CPU have 8 processors. ----------- There are 52.35564786420257% free memory. CPU have 8 processors. ----------- There are 43.53256687278083% free memory. CPU have 8 processors. ----------- There are 34.70958168390994% free memory. CPU have 8 processors. ----------- There are 27.6511289345218% free memory. System will start memory Garbage Collection. Now we have 80815 KB free memory. After the Garbage Collection, we have 281843 KB free memory. CPU have 8 processors. ----------- There are 89.37604181852511% free memory. Last freeMemory = 281843 KB, now it is 261217 KB. Last totalMemory = 380108 KB, now it is 380108 KB. Last maxMemory = 1875378 KB, now it is 1875378 KB. CPU have 8 processors. ----------- There are 80.55442250030752% free memory. CPU have 8 processors. ----------- There are 73.49712485596086% free memory. CPU have 8 processors. ----------- There are 64.67547542837015% free memory. CPU have 8 processors. ----------- There are 57.618177784023494% free memory. CPU have 8 processors. ----------- There are 48.79652835643279% free memory. CPU have 8 processors. ----------- There are 41.739230712086126% free memory. CPU have 8 processors. ----------- There are 32.91758128449542% free memory. CPU have 8 processors. ----------- There are 24.095931856904713% free memory. System will start memory Garbage Collection. Now we have 70424 KB free memory. After the Garbage Collection, we have 258135 KB free memory. CPU have 8 processors. ----------- There are 81.32306278893708% free memory. Last freeMemory = 258135 KB, now it is 237681 KB. Last totalMemory = 380108 KB, now it is 380108 KB. Last maxMemory = 1875378 KB, now it is 1875378 KB. CPU have 8 processors. ----------- There are 72.5752415442342% free memory. CPU have 8 processors. ----------- There are 65.57687068029719% free memory. CPU have 8 processors. ----------- There are 56.828918049238894% free memory. CPU have 8 processors. ----------- There are 49.83056634581206% free memory. CPU have 8 processors. ----------- There are 41.08271772895181% free memory. CPU have 8 processors. ----------- There are 32.33487732373877% free memory. CPU have 8 processors. ----------- There are 25.33650645980177% free memory. System will start memory Garbage Collection. Now we have 74050 KB free memory. After the Garbage Collection, we have 229217 KB free memory. CPU have 8 processors. ----------- There are 71.73676393326296% free memory. Last freeMemory = 229217 KB, now it is 209663 KB. Last totalMemory = 380108 KB, now it is 380108 KB. Last maxMemory = 1875378 KB, now it is 1875378 KB. CPU have 8 processors. ----------- There are 63.37379248412019% free memory. CPU have 8 processors. ----------- There are 55.01088399093938% free memory. CPU have 8 processors. ----------- There are 46.64788243242348% free memory. CPU have 8 processors. ----------- There are 38.28488087390758% free memory. CPU have 8 processors. ----------- There are 31.59450152482077% free memory. CPU have 8 processors. ----------- There are 23.231593031639967% free memory. System will start memory Garbage Collection. Now we have 67898 KB free memory. After the Garbage Collection, we have 203384 KB free memory. CPU have 8 processors. ----------- There are 61.86563040788292% free memory. Last freeMemory = 203384 KB, now it is 180813 KB. Last totalMemory = 380108 KB, now it is 380108 KB. Last maxMemory = 1875378 KB, now it is 1875378 KB. CPU have 8 processors. -----------
我最近(2012-11-6 Tuesday 至 2012-11-20 Tuesday)参加了一个博客大赛。欢迎来给我投一票~
投票地址: http://blog.51cto.com/contest2012/5523233
每个 IP 每天可以投一票哦~
本文也发表在我的其他空间。
CSDN : http://blog.csdn.net/shanelooli/article/details/8176938
开源中国: http://my.oschina.net/shane1984/blog/88803
51CTO : http://shanelooli.blog.51cto.com/5523233/1058490
发表评论
-
CIKERS Shane 20190620
2019-06-20 13:44 312<div class="iteye-blog- ... -
Spads 出品字符串高效提取数值方法 Shane 末日圣诞奉献
2012-12-26 11:27 1472Spads 出品,作者 WangXP 与 Shane目录--- ... -
Spads 公式解析系统 - Java
2012-11-03 01:27 1871很多网络应用中,涉及到一些内部运算的具体求值逻辑并不能够在开发 ... -
刚有人问面试题咋答,一时兴起回了下,关于对 Struts2, Spring 和 Hibernate 的理解
2012-10-30 15:30 1980Struts2, Spring 和 Hibernate ... -
爪哇(Java)自定义的二个字符串高效处理方法,在静寂一个半月之后
2012-10-29 13:38 1493以下是测试程序运行结果: source = a b c d ... -
安装 Eclipse 4.2 实录
2012-07-30 21:36 1099首先,我访问了 Eclipse 的官方网站。http://ww ... -
MySQL 存储过程初研究
2012-07-13 16:45 1107最近在做一个移动设备多类型登录的统一用户系统。其中记录用户资料 ... -
MySQL 存储过程初研究
2012-07-13 16:43 0最近在做一个移动设备多类型登录的统一用户系统。其中记录用户资料 ... -
如何看待编程模式 + 观察者模式含义解释
2012-06-26 15:53 763本来是回帖,但还是发 ...
相关推荐
ArcGIS Runtime SDK for Java 100.12.0 的...如需在生产环境中使用ArcGIS Runtime SDK for Java,请联系ESRI公司获得授权,现在也有国产替代产品GeoScne Runtime SDK for Java,可以联系易智瑞公司获得相关资源和授权。
Java SE程序 Runtime类Java SE程序 Runtime类Java SE程序 Runtime类Java SE程序 Runtime类Java SE程序 Runtime类Java SE程序 Runtime类Java SE程序 Runtime类Java SE程序 Runtime类Java SE程序 Runtime类Java SE程序...
JRE 1.4.2 JRE(Java Runtime Environment,Java运行环境),运行JAVA程序所必须的环境的集合,包含JVM标准实现及Java核心类库。
Java Runtime Environment-6.0.26.exe
Java Runtime For MacOS X 10.7
JRE(Java Runtime Environment)——windows Java运行环境 64bit
JRE1.8.0(Java Runtime Environment)——Java运行环境32bit
Java Runtime Environment 1.6.0.7 (32-bit
利用RunLoop监控卡顿的demo,对应博客:https://blog.csdn.net/dolacmeng/article/details/89343075
onnx的java包,pom引入后可以试验java调用onnx文件进行cpu模型推理。 具体见https://github.com/microsoft/onnxruntime/blob/master/java/src/test/java/sample/ScoreMNIST.java 和 ...
Java SE Runtime Environment(运行时环境)包含了运行以 Java 编程语言编写的程序所必需的 Java 虚拟机、运行时类库和 Java 应用程序启动器。 Java 平台的安全性 一个由角色提供的安全信息的描述。适用于开发人员...
利用ArcGIS Runtime API for Java把空间数据从CGCS2000转WGS84和BD09。提供了完整的环境构建和代码示例。开箱即用,并对构建运行环境的操作步骤提供离线和在线两种方式。
java runtime environment 1.8.0_45 64bit.rar
Java SE Runtime Environment(运行时环境)包含了运行以 Java 编程语言编写的程序所必需的 Java 虚拟机、运行时类库和 Java 应用程序启动器。 Java 平台的安全性 一个由角色提供的安全信息的描述。适用于开发人员...
JRE 版本号为8u251,macOS 64位版本,包含dmg安装程序和压缩包。 Java Runtime Environment 是运行基于Java语言编写的程序所不可缺少的运行环境,用于解释执行Java的字节码文件。
在arcgis开发者官网下载的rumtime 100.13.1开发包java版本,由于官网抽风经常上不去,我下载了上传造福大家
标签:bytedeco、onnxruntime、中英对照文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,...
官方离线安装包,亲测可用。使用rpm -ivh [rpm完整包名] 进行安装
java runtime environment 1.8.0_45 64bit jre1.8.0_45 64bit jre-8u261
Java Runtime Environment,Java运行环境64位安装包,官网下载。 Java SE Runtime Environment 8 Downloads Do you want to run Java™ programs, or do you want to develop Java programs? If you want to run ...