国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

flink源碼分析-獲取JVM最大堆內(nèi)存

這篇具有很好參考價(jià)值的文章主要介紹了flink源碼分析-獲取JVM最大堆內(nèi)存。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

flink版本: flink-1.11.2

代碼位置:?org.apache.flink.runtime.util.EnvironmentInformation#getMaxJvmHeapMemory

如果設(shè)置了-Xmx參數(shù),就返回這個(gè)參數(shù),如果沒設(shè)置就返回機(jī)器物理內(nèi)存的1/4.? 這里主要看各個(gè)機(jī)器內(nèi)存的獲取方法。

	/**
	 * The maximum JVM heap size, in bytes.
	 *
	 * <p>This method uses the <i>-Xmx</i> value of the JVM, if set. If not set, it returns (as
	 * a heuristic) 1/4th of the physical memory size.
	 *
	 * @return The maximum JVM heap size, in bytes.
	 */
	public static long getMaxJvmHeapMemory() {
		final long maxMemory = Runtime.getRuntime().maxMemory();
		if(maxMemory != Long.MAX_VALUE) {
			// we have the proper max memory
			return maxMemory;
		} else {
			// max JVM heap size is not set - use the heuristic to use 1/4th of the physical memory
			final long physicalMemory = Hardware.getSizeOfPhysicalMemory();
			if(physicalMemory != -1) {
				// got proper value for physical memory
				return physicalMemory / 4;
			} else {
				throw new RuntimeException(
					"Could not determine the amount of free memory.\n" + "Please set the maximum memory for the JVM, e.g. -Xmx512M for 512 megabytes.");
			}
		}
	}

進(jìn)入getSizeOfPhysicalMemory()方法,里面有獲取各種操作系統(tǒng)物理內(nèi)存的方法:

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.flink.runtime.util;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.flink.util.OperatingSystem;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Convenience class to extract hardware specifics of the computer executing the running JVM.
 */
public class Hardware {

	private static final Logger LOG = LoggerFactory.getLogger(Hardware.class);

	private static final String LINUX_MEMORY_INFO_PATH = "/proc/meminfo";

	private static final Pattern LINUX_MEMORY_REGEX = Pattern.compile("^MemTotal:\\s*(\\d+)\\s+kB$");

	// ------------------------------------------------------------------------
	
	/**
	 * Gets the number of CPU cores (hardware contexts) that the JVM has access to.
	 * 
	 * @return The number of CPU cores.
	 */
	public static int getNumberCPUCores() {

		// TODO_MA 注釋: 獲取 Cpu Cores
		return Runtime.getRuntime().availableProcessors();
	}

	/**
	 * Returns the size of the physical memory in bytes.
	 * 
	 * @return the size of the physical memory in bytes or {@code -1}, if
	 *         the size could not be determined.
	 */
	public static long getSizeOfPhysicalMemory() {
		// first try if the JVM can directly tell us what the system memory is
		// this works only on Oracle JVMs
		try {
			Class<?> clazz = Class.forName("com.sun.management.OperatingSystemMXBean");
			Method method = clazz.getMethod("getTotalPhysicalMemorySize");
			OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();

			// someone may install different beans, so we need to check whether the bean
			// is in fact the sun management bean
			if (clazz.isInstance(operatingSystemMXBean)) {
				return (Long) method.invoke(operatingSystemMXBean);
			}
		}
		catch (ClassNotFoundException e) {
			// this happens on non-Oracle JVMs, do nothing and use the alternative code paths
		}
		catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
			LOG.warn("Access to physical memory size: " +
					"com.sun.management.OperatingSystemMXBean incompatibly changed.", e);
		}

		// we now try the OS specific access paths
		switch (OperatingSystem.getCurrentOperatingSystem()) {
			case LINUX:
				return getSizeOfPhysicalMemoryForLinux();

			case WINDOWS:
				return getSizeOfPhysicalMemoryForWindows();

			case MAC_OS:
				return getSizeOfPhysicalMemoryForMac();

			case FREE_BSD:
				return getSizeOfPhysicalMemoryForFreeBSD();

			case UNKNOWN:
				LOG.error("Cannot determine size of physical memory for unknown operating system");
				return -1;

			default:
				LOG.error("Unrecognized OS: " + OperatingSystem.getCurrentOperatingSystem());
				return -1;
		}
	}

	/**
	 * Returns the size of the physical memory in bytes on a Linux-based
	 * operating system.
	 * 
	 * @return the size of the physical memory in bytes or {@code -1}, if
	 *         the size could not be determined
	 */
	private static long getSizeOfPhysicalMemoryForLinux() {
		try (BufferedReader lineReader = new BufferedReader(new FileReader(LINUX_MEMORY_INFO_PATH))) {
			String line;
			while ((line = lineReader.readLine()) != null) {
				Matcher matcher = LINUX_MEMORY_REGEX.matcher(line);
				if (matcher.matches()) {
					String totalMemory = matcher.group(1);
					return Long.parseLong(totalMemory) * 1024L; // Convert from kilobyte to byte
				}
			}
			// expected line did not come
			LOG.error("Cannot determine the size of the physical memory for Linux host (using '/proc/meminfo'). " +
					"Unexpected format.");
			return -1;
		}
		catch (NumberFormatException e) {
			LOG.error("Cannot determine the size of the physical memory for Linux host (using '/proc/meminfo'). " +
					"Unexpected format.");
			return -1;
		}
		catch (Throwable t) {
			LOG.error("Cannot determine the size of the physical memory for Linux host (using '/proc/meminfo') ", t);
			return -1;
		}
	}

	/**
	 * Returns the size of the physical memory in bytes on a Mac OS-based
	 * operating system
	 * 
	 * @return the size of the physical memory in bytes or {@code -1}, if
	 *         the size could not be determined
	 */
	private static long getSizeOfPhysicalMemoryForMac() {
		BufferedReader bi = null;
		try {
			Process proc = Runtime.getRuntime().exec("sysctl hw.memsize");

			bi = new BufferedReader(new InputStreamReader(proc.getInputStream()));

			String line;
			while ((line = bi.readLine()) != null) {
				if (line.startsWith("hw.memsize")) {
					long memsize = Long.parseLong(line.split(":")[1].trim());
					bi.close();
					proc.destroy();
					return memsize;
				}
			}

		} catch (Throwable t) {
			LOG.error("Cannot determine physical memory of machine for MacOS host", t);
			return -1;
		} finally {
			if (bi != null) {
				try {
					bi.close();
				} catch (IOException ignored) {}
			}
		}
		return -1;
	}

	/**
	 * Returns the size of the physical memory in bytes on FreeBSD.
	 * 
	 * @return the size of the physical memory in bytes or {@code -1}, if
	 *         the size could not be determined
	 */
	private static long getSizeOfPhysicalMemoryForFreeBSD() {
		BufferedReader bi = null;
		try {
			Process proc = Runtime.getRuntime().exec("sysctl hw.physmem");

			bi = new BufferedReader(new InputStreamReader(proc.getInputStream()));

			String line;
			while ((line = bi.readLine()) != null) {
				if (line.startsWith("hw.physmem")) {
					long memsize = Long.parseLong(line.split(":")[1].trim());
					bi.close();
					proc.destroy();
					return memsize;
				}
			}
			
			LOG.error("Cannot determine the size of the physical memory for FreeBSD host " +
					"(using 'sysctl hw.physmem').");
			return -1;
		}
		catch (Throwable t) {
			LOG.error("Cannot determine the size of the physical memory for FreeBSD host " +
					"(using 'sysctl hw.physmem')", t);
			return -1;
		}
		finally {
			if (bi != null) {
				try {
					bi.close();
				} catch (IOException ignored) {}
			}
		}
	}

	/**
	 * Returns the size of the physical memory in bytes on Windows.
	 * 
	 * @return the size of the physical memory in bytes or {@code -1}, if
	 *         the size could not be determined
	 */
	private static long getSizeOfPhysicalMemoryForWindows() {
		BufferedReader bi = null;
		try {
			Process proc = Runtime.getRuntime().exec("wmic memorychip get capacity");

			bi = new BufferedReader(new InputStreamReader(proc.getInputStream()));

			String line = bi.readLine();
			if (line == null) {
				return -1L;
			}

			if (!line.startsWith("Capacity")) {
				return -1L;
			}

			long sizeOfPhyiscalMemory = 0L;
			while ((line = bi.readLine()) != null) {
				if (line.isEmpty()) {
					continue;
				}

				line = line.replaceAll(" ", "");
				sizeOfPhyiscalMemory += Long.parseLong(line);
			}
			return sizeOfPhyiscalMemory;
		}
		catch (Throwable t) {
			LOG.error("Cannot determine the size of the physical memory for Windows host " +
					"(using 'wmic memorychip')", t);
			return -1L;
		}
		finally {
			if (bi != null) {
				try {
					bi.close();
				} catch (Throwable ignored) {}
			}
		}
	}

	// --------------------------------------------------------------------------------------------

	private Hardware() {}
}

另外注意try catch的這種用法:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-616760.html

		try (BufferedReader lineReader = new BufferedReader(new FileReader(LINUX_MEMORY_INFO_PATH))) {
			String line;
			while ((line = lineReader.readLine()) != null) {
				Matcher matcher = LINUX_MEMORY_REGEX.matcher(line);
				if (matcher.matches()) {
					String totalMemory = matcher.group(1);
					return Long.parseLong(totalMemory) * 1024L; // Convert from kilobyte to byte
				}
			}
			// expected line did not come
			LOG.error("Cannot determine the size of the physical memory for Linux host (using '/proc/meminfo'). " +
					"Unexpected format.");
			return -1;
		}
		catch (NumberFormatException e) {
			LOG.error("Cannot determine the size of the physical memory for Linux host (using '/proc/meminfo'). " +
					"Unexpected format.");
			return -1;
		}

到了這里,關(guān)于flink源碼分析-獲取JVM最大堆內(nèi)存的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 用jprofiler來(lái)分析 jvm 堆 內(nèi)存泄露,fullgc

    用jprofiler來(lái)分析 jvm 堆 內(nèi)存泄露,fullgc

    jvm 命令和工具_(dá)個(gè)人渣記錄僅為自己搜索用的博客-CSDN博客 ? 方法1: 重新設(shè)置堆后,重啟,復(fù)現(xiàn). ? 方法2:? 切割 ?官網(wǎng)文檔 JProfiler Help - HPROF snapshots heap walker教學(xué): 用jprofile查看hprof文件_hprof jprofile_java老張的博客-CSDN博客 通識(shí)教學(xué): JVM監(jiān)控及診斷工具GUI篇之JProfiler_每天都要進(jìn)步一

    2024年02月08日
    瀏覽(24)
  • SpringBoot自主監(jiān)控,獲取服務(wù)信息、JVM、CPU、內(nèi)存、磁盤、堆、線程、GC等

    SpringBoot自主監(jiān)控,獲取服務(wù)信息、JVM、CPU、內(nèi)存、磁盤、堆、線程、GC等

    1. 簡(jiǎn)介 ??在日常開發(fā)中一些關(guān)鍵的業(yè)務(wù)服務(wù),期望在高并發(fā)狀態(tài)下可以正常工作,或在異常情況時(shí)可以記錄當(dāng)時(shí)的性能信息,所以就需要進(jìn)行監(jiān)控。常見的監(jiān)控例如: Prometheus 可以實(shí)現(xiàn)這個(gè)需求,如果需要更加簡(jiǎn)單方便的自主監(jiān)控能力,可以引入本博客中的方案。 2. 相關(guān)博

    2024年02月15日
    瀏覽(26)
  • 一文深度講解JVM 內(nèi)存分析工具 MAT及實(shí)踐(建議收藏)

    一文深度講解JVM 內(nèi)存分析工具 MAT及實(shí)踐(建議收藏)

    1. 前言 熟練掌握 MAT 是 Java 高手的必備能力,但實(shí)踐時(shí)大家往往需面對(duì)眾多功能,眼花繚亂不知如何下手,小編也沒有找到一篇完善的教學(xué)素材,所以整理本文幫大家系統(tǒng)掌握 MAT 分析工具。 本文詳細(xì)講解 MAT 眾多內(nèi)存分析工具功能,這些功能組合使用異常強(qiáng)大,熟練使用幾

    2024年02月09日
    瀏覽(27)
  • JVM逃逸分析原理解析:優(yōu)化Java程序性能和內(nèi)存利用效率

    在Java開發(fā)中,性能和內(nèi)存利用效率一直是開發(fā)者關(guān)注的焦點(diǎn)。為了提高Java程序的執(zhí)行效率,JVM引入了逃逸分析技術(shù)。本文將詳細(xì)解析JVM逃逸分析的原理,幫助讀者深入理解其工作機(jī)制。 逃逸分析是一種用于確定對(duì)象在方法的生命周期內(nèi)是否逃逸出方法外部范圍的技術(shù)。在

    2024年01月20日
    瀏覽(87)
  • JVM內(nèi)存模型/運(yùn)行時(shí)數(shù)據(jù)區(qū)域

    JVM內(nèi)存模型/運(yùn)行時(shí)數(shù)據(jù)區(qū)域

    java虛擬機(jī)管理這塊內(nèi)存,所以我們也叫運(yùn)行時(shí)數(shù)據(jù)區(qū)域 這里按線程是否共享來(lái)分類,所謂線程不共享就是每個(gè)線程里面都會(huì)配一套 程序計(jì)數(shù)器 棧, 互相不干涉。 而方法區(qū)和堆是線程所有共享 意味著只有一個(gè)(這里注意堆是實(shí)際概念 方法區(qū)是一個(gè)虛擬概念) 注意:程序計(jì)

    2024年01月18日
    瀏覽(25)
  • Kotlin協(xié)程的JVM實(shí)現(xiàn)源碼分析(上)

    本文從協(xié)程的啟動(dòng) launch 源碼入手分析,協(xié)程JVM實(shí)現(xiàn)分為兩篇: 協(xié)程啟動(dòng)和執(zhí)行源碼分析 無(wú)棧協(xié)程 和 Continuation 基本環(huán)境: IntelliJ IDEA 2023.3.2 Kotlin 1.8.20 kotlinx-coroutines-core 1.7.3 gradle 8.2 以 GlobalScope.launch 啟動(dòng)協(xié)程分析: 調(diào)用關(guān)系: CoroutineScope.launch - StandaloneCoroutine.start - Corou

    2024年01月19日
    瀏覽(24)
  • Kotlin協(xié)程的JVM實(shí)現(xiàn)源碼分析(下)

    Kotlin協(xié)程的JVM實(shí)現(xiàn)源碼分析(下)

    協(xié)程 根據(jù) 是否保存切換 調(diào)用棧 ,分為: 有棧協(xié)程(stackful coroutine) 無(wú)棧協(xié)程(stackless coroutine) 在代碼上的區(qū)別是:是否可在普通函數(shù)里調(diào)用,并暫停其執(zhí)行。 Kotlin協(xié)程,必須在掛起函數(shù)中調(diào)用和恢復(fù),屬于 無(wú)棧協(xié)程 。 常見的語(yǔ)言,協(xié)程實(shí)現(xiàn): 有棧協(xié)程 :Go、Lua 無(wú)棧

    2024年01月23日
    瀏覽(17)
  • JVM—內(nèi)存管理(運(yùn)行時(shí)數(shù)據(jù)區(qū))、垃圾回收

    JVM—內(nèi)存管理(運(yùn)行時(shí)數(shù)據(jù)區(qū))、垃圾回收

    當(dāng)JVM類加載器加載完字節(jié)碼文件之后,會(huì)交給執(zhí)行引擎執(zhí)行,在執(zhí)行的過程中會(huì)有一塊JVM內(nèi)存區(qū)域來(lái)存放程序運(yùn)行過程中的數(shù)據(jù),也就是我們圖中放的運(yùn)行時(shí)數(shù)據(jù)區(qū),那這一塊運(yùn)行時(shí)數(shù)據(jù)區(qū)究竟幫我們做了哪些工作?我們常說(shuō)的線上內(nèi)存泄漏和內(nèi)存溢出是因?yàn)槭裁矗课覀兘駜?/p>

    2024年02月13日
    瀏覽(29)
  • 區(qū)分什么是Java內(nèi)存模型(JMM)和 JVM運(yùn)行時(shí)數(shù)據(jù)區(qū)

    區(qū)分什么是Java內(nèi)存模型(JMM)和 JVM運(yùn)行時(shí)數(shù)據(jù)區(qū)

    Java的內(nèi)存區(qū)域和內(nèi)存模型是不一樣的東西,內(nèi)存區(qū)域是指 JVM 運(yùn)行時(shí)將數(shù)據(jù)分區(qū)域存儲(chǔ),強(qiáng)調(diào)對(duì)內(nèi)存空間的劃分 。 而內(nèi)存模型(Java Memory Model,簡(jiǎn)稱 JMM )是 定義了線程和主內(nèi)存之間的抽象關(guān)系,即 JMM 定義了 JVM 在計(jì)算機(jī)內(nèi)存(RAM)中的工作方式 ,如果我們要想深入了解Java并

    2024年02月11日
    瀏覽(31)
  • 【編程實(shí)踐】JVM內(nèi)存管理的核心數(shù)據(jù)結(jié)構(gòu)和算法思想&代碼示例說(shuō)明

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包