JAVA獲取JVM內存空間和物理內存空間

一、獲取JVM內存空間

????????系統環境:WIN

????????JDK版本:1.8re

????????直接調用Runtime中相應的方法即可:

public long maxMemory()
?
Returns the maximum amount of memory that the Java virtual machine will attempt to use.?
If there is no inherent limit then the value Long.MAX_VALUE will be returned.
?
Returns:
? ? the maximum amount of memory that the virtual machine will attempt to use, measured in bytes
Since:
? ? 1.4?
????????以上為Java8的API,maxMemory( ) 返回Java虛擬機當前狀態能使用的最大內存大小。如果沒有參數限制,將會返回Long.MAX_VALUE這個值。敲黑板!Long.MAX_VALUE這個值是什么呢,這個Long.MAX_VALUE是JVM能夠在系統中可以擴展到的最大的內存大小。

public long totalMemory()
?
Returns the total amount of memory in the Java virtual machine.?
The value returned by this method may vary over time, depending on the host environment.
?
Note that the amount of memory required to hold an object of any given type may be implementation-dependent.
?
Returns:
? ? the total amount of memory currently available for current and future objects, measured in bytes.?
????????totalMemory( ) 返回Java虛擬機中的總內存。這個方法返回的值可能隨時間而變化,這取決于宿主操作系統環境和JVM的內存占用情況。 需要注意的是, Note that the amount of memory required to hold an object of any given type may be implementation-dependent 。不同依賴實現的虛擬機需要的能Hold住任何類型對象所需的內存大小都不太一樣。因為這取決于對象在JVM運行時是如何建立在內存中的,不同的虛擬機實現都不太一樣,拿最常用的HotSpot來說,一個對象包括:對象頭(Header)、實例數據(Instance Data)、對齊填充(Padding),而且在JAVA中需要檢測對象占用的內存大小,不像C中那么簡單sizeof( ) 就完事了。?


public long freeMemory()
?
Returns the amount of free memory in the Java Virtual Machine.?
Calling the gc method may result in increasing the value returned by freeMemory.
?
Returns:
? ? an approximation to the total amount of memory currently available for future allocated objects, measured in bytes.?
????????freeMemory( ) 返回Java虛擬機中空閑內存的數量。這個空閑是相對于totalMemory來說的而不是maxMemory,調用GC方法可能會增大freeMemory的返回值。

二、獲取操作系統內存空間

????????依靠sun.management.ManagementFactoryHelper,不多說,看代碼。


import java.lang.management.ManagementFactory;
import com.sun.management.OperatingSystemMXBean;
?
/**
?* JAVA獲取JVM內存空間和物理內存空間
?* @author jiangyuqin
?*
?*/
public class MonitorInfoTest {
?
?? ?public static void main(String[] args) {
?
?? ??? ?// 虛擬機級內存情況查詢
?? ??? ?long vmFree = 0;
?? ??? ?long vmUse = 0;
?? ??? ?long vmTotal = 0;
?? ??? ?long vmMax = 0;
?? ??? ?int byteToMb = 1024 * 1024;
?? ??? ?Runtime rt = Runtime.getRuntime();
?? ??? ?vmTotal = rt.totalMemory() / byteToMb;
?? ??? ?vmFree = rt.freeMemory() / byteToMb;
?? ??? ?vmMax = rt.maxMemory() / byteToMb;
?? ??? ?vmUse = vmTotal - vmFree;
?? ??? ?System.out.println("JVM內存已用的空間為:" + vmUse + " MB");
?? ??? ?System.out.println("JVM內存的空閑空間為:" + vmFree + " MB");
?? ??? ?System.out.println("JVM總內存空間為:" + vmTotal + " MB");
?? ??? ?System.out.println("JVM總內存空間為:" + vmMax + " MB");
?
?? ??? ?System.out.println("======================================");
?? ??? ?// 操作系統級內存情況查詢
?? ??? ?OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
?? ??? ?String os = System.getProperty("os.name");
?? ??? ?long physicalFree = osmxb.getFreePhysicalMemorySize() / byteToMb;
?? ??? ?long physicalTotal = osmxb.getTotalPhysicalMemorySize() / byteToMb;
?? ??? ?long physicalUse = physicalTotal - physicalFree;
?? ??? ?System.out.println("操作系統的版本:" + os);
?? ??? ?System.out.println("操作系統物理內存已用的空間為:" + physicalFree + " MB");
?? ??? ?System.out.println("操作系統物理內存的空閑空間為:" + physicalUse + " MB");
?? ??? ?System.out.println("操作系統總物理內存:" + physicalTotal + " MB");
?? ??? ?
?? ??? ?// 獲得線程總數
?? ??? ?ThreadGroup parentThread;
?? ??? ?int totalThread = 0;
?? ??? ?for (parentThread = Thread.currentThread().getThreadGroup(); parentThread
?? ??? ??? ??? ?.getParent() != null; parentThread = parentThread.getParent()) {
?? ??? ??? ?totalThread = parentThread.activeCount();
?? ??? ?}
?? ??? ?System.out.println("獲得線程總數:" + totalThread);
?? ?}
}
運行時加上參數:-Xms512m -Xmx1024m?

?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/386756.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/386756.shtml
英文地址,請注明出處:http://en.pswp.cn/news/386756.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

CMU Database Systems - Sorting,Aggregation,Join

Sorting 排序如果可在內存里面排,用經典的排序算法就ok,比如快排 問題在于,數據表中的的數據是很多的,沒法一下都放到內存里面進行排序 所以就需要用到,外排,多路并歸排序 看下最簡單的,2路并歸…

springboot線程池的使用和擴展

實戰環境 windowns10;jdk1.8;springboot 1.5.9.RELEASE;開發工具:IntelliJ IDEA; 實戰源碼 本次實戰的源碼可以在我的GitHub下載,地址:gitgithub.com:zq2599/blog_demos.git,項目主…

統計單詞個數

我是抄題解狂魔 /* 1.s.substr(x,len) 在s中取出從x位置開始,長度為len的字符串,并返回string類型的字符串。 2.s.find(a) 在s中查找字符串a,并返回起始下標(從0開始),若不存在,返回1844674407370955161&am…

通過Rancher安裝K8s

說明 我們用kubernetes去管理Docker集群,即可以將Docker看成Kubernetes內部使用的低級別組件。另外,kubernetes不僅僅支持Docker,還支持Rocket,這是另一種容器技術。希望我這篇文章中簡單的描述能讓你對兩者有所理解和認識。 機…

35. 搜索插入位置-LeetCode

心得:這個題也是二分查找,但是有個小技巧:當left>right的時候 left就是要插入的位置。 代碼: 1 class Solution {2 public int searchInsert(int[] nums, int target) {3 if(numsnull||nums.length0)4 …

Kubectl指令集

1 Kubectl指令集 1.1 Master查詢節點信息 [rootmaster1 kubernetes-1.10]# kubectl get nodes 1.2 查詢所有Pod信息 [rootmaster1 ~]# kubectl get pods --namespacekube-system 1.3 查詢故障的Pod信息 [rootmaster1 ~]# kubectl get pods -n kube-sys…

SQL基礎培訓實戰教程[全套]

學習簡介:林楓山根據網上搜索資料進行參考,編寫制作的SQL Server實操學習教程,歡迎下載學習。 下載鏈接目錄如下: 進度0-SQL基礎語法 下載學習文檔 進度1-建數據表-美化版-2018-6-12 下載學習文檔 進度2-關于主鍵-美化…

K8S儀表板Service unavailable故障的解決辦法

K8S儀表板Service unavailable故障的解決辦法 (使用Rancher部署Kubernetes后訪問儀表板提示Service unavailable的問題) 一、逐項檢查: 1、操作系統Kernel版本(3.10以上) 2、檢查OS版本(Ubuntu16.04.x、…

實驗五報告

一、實驗結論&#xff1a; 1. 二分查找&#xff1a;補足程序ex1_1.cpp// 練習&#xff1a;使用二分查找&#xff0c;在一組有序元素中查找數據項 // 形參是數組&#xff0c;實參是數組名 #include <stdio.h> const int N5; int binarySearch(int x[], int n, int item…

關于瀏覽器內核

介紹一下對瀏覽器內核的理解主要分成兩個部分&#xff1a;渲染引擎(Render Engine)和JS引擎。常見的瀏覽器內核有哪些&#xff1f;Trident內核&#xff1a;IE&#xff0c;360&#xff0c;搜過瀏覽器&#xff1b;Gecko內核&#xff1a;Netscape6及以上版本&#xff0c;Presto內核…

docker 全部殺掉

殺死所有正在運行的容器 docker kill $(docker ps -a -q) 刪除所有已經停止的容器 docker rm $(docker ps -a -q) 刪除所有未打 dangling 標簽的鏡像 docker rmi $(docker images -q -f danglingtrue) 刪除所有鏡像 docker rmi $(docker images -q) 強制刪除鏡像名稱中包含“do…

實驗五 網絡編程與安全-----實驗報告

一、實驗五 網絡編程與安全-1 1.實驗要求&#xff1a; 兩人一組結對編程&#xff1a; &#xff08;1&#xff09;參考http://www.cnblogs.com/rocedu/p/6766748.html#SECDSA &#xff1b; &#xff08;2&#xff09;結對實現中綴表達式轉后綴表達式的功能 MyBC.java&#xff1b…

K8S的HelloWorld之旅

安裝kubectl。使用Google提供商&#xff08;如Google Container Engine或Amazon Web Services&#xff09;創建Kubernetes群集。本教程創建一個 外部負載均衡器&#xff0c;它需要一個云提供商。配置kubectl與Kubernetes API服務器通信。有關說明&#xff0c;請參閱云提供商的文…

思維構造——cf1090D

/* 只要找到兩個沒有關系的點即可 */ #include<bits/stdc.h> using namespace std; #define maxn 100005 long long n,m; int a[maxn],b[maxn]; vector<int>G[maxn]; int main(){cin>>n>>m;if(n1){puts("NO");return 0;}if(n*(n-1)/2<m)…

誤刪docker0網橋之后怎么辦呢?

誤刪docker0網橋之后怎么辦呢&#xff1f; 今天&#xff0c;在搭建k8s node節點環境的時候&#xff0c;好巧不巧&#xff0c;執行了如下命令&#xff1a; 1 2 [roothxin221 ~]# ifconfig docker0 down &>/dev/null [roothxin221 ~]# brctl delbr docker0 &>/de…

boost.asio學習

https://mmoaay.gitbooks.io/boost-asio-cpp-network-programming-chinese/content/Chapter1.html轉載于:https://www.cnblogs.com/hshy/p/10930398.html

Harbor:私有企業級Registry倉庫--快速搭建

前言 Harbor可以通過Docker Composer的方式來部署&#xff0c;如果有正常運行的k8s環境&#xff0c;也可以使用k8s來部署Harbor&#xff0c;本文采用 Docker Composer的方式。 準備 假定Linux系統為Centos 7。 docker &#xff0c;默認安裝即可 yum -y install docker 1 dock…

java-Mysql學生管理系統

Window1//主方法 package stu_zizhu1; import java.awt.Button; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Point; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JBu…

Docker版本Jenkins的使用

Docker版本Jenkins的使用 低調的微胖關注贊賞支持 Docker版本Jenkins的使用 12018.05.15 18:21:50字數 1202閱讀 22588 一. 什么是Jenkins Jenkins是當前非常流行的一款持續集成工具&#xff0c;可以幫助大家把更新后的代碼自動部署到服務器上運行。 二. 為什么用docker版…

小程序 setData 中的坑,其實好像...

最近這段時間在寫微信小程序&#xff0c;有一個頁面需要動態修改 data 中的數據&#xff0c;而這里似乎是個坑。 1、正常修改 正常修改很簡單&#xff0c;當觸發 change 事件時&#xff0c;數據和頁面都會同時發生改變。這個也不用多說&#xff0c;很簡單的例子。 2、如何修改對…