当前位置: 首页 > news >正文

哈尔滨做网站上海宝山网站制作

哈尔滨做网站,上海宝山网站制作,免费网站加速软件,设计一款网页需要多少钱文章目录 一、什么是直接内存?二、特点三、使用案例四、直接内存的管理 一、什么是直接内存? Direct Memory:系统内存 普通IO,运行原理图 磁盘到系统内存,系统内存到jvm内存。 NIO,运行原理图 划分了一块…

文章目录

  • 一、什么是直接内存?
  • 二、特点
  • 三、使用案例
  • 四、直接内存的管理

一、什么是直接内存?

Direct Memory:系统内存

普通IO,运行原理图
磁盘到系统内存,系统内存到jvm内存。
在这里插入图片描述
NIO,运行原理图
划分了一块区域,JVM和系统共享的内存区间,这样,就减少了一次IO操作。
在这里插入图片描述

二、特点

常见于 NIO 操作时,用于数据缓冲区
分配回收成本较高,但读写性能高
不受 JVM 内存回收管理

所以,我们可以在IO程序中,使用直接内存来优化程序的读写性能。

三、使用案例

关键代码:ByteBuffer.allocateDirect(_1Mb);

public class Demo1_9 {static final String FROM = "E:\\sbPSjI4tt10.mp4";static final String TO = "E:\\a.mp4";static final int _1Mb = 1024 * 1024;public static void main(String[] args) {io(); // io 用时:1535.586957 1766.963399 1359.240226directBuffer(); // directBuffer 用时:479.295165 702.291454 562.56592}private static void directBuffer() {long start = System.nanoTime();try (FileChannel from = new FileInputStream(FROM).getChannel();FileChannel to = new FileOutputStream(TO).getChannel();) {ByteBuffer bb = ByteBuffer.allocateDirect(_1Mb);while (true) {int len = from.read(bb);if (len == -1) {break;}bb.flip();to.write(bb);bb.clear();}} catch (IOException e) {e.printStackTrace();}long end = System.nanoTime();System.out.println("directBuffer 用时:" + (end - start) / 1000_000.0);}private static void io() {long start = System.nanoTime();try (FileInputStream from = new FileInputStream(FROM);FileOutputStream to = new FileOutputStream(TO);) {byte[] buf = new byte[_1Mb];while (true) {int len = from.read(buf);if (len == -1) {break;}to.write(buf, 0, len);}} catch (IOException e) {e.printStackTrace();}long end = System.nanoTime();System.out.println("io 用时:" + (end - start) / 1000_000.0);}
}

但是,直接内存,是不受JVM管理的
另外,我们显示调用gcJVM也不是立马就执行gc

而且,一般我们会在项目中禁用显示调用gc,因为,Full GC影响性能。
禁用参数:-XX:+DisableExplicitGC

四、直接内存的管理

底层是如何回收直接内存的?

  • 使用了 Unsafe 对象完成直接内存的分配回收,并且回收需要主动调用 freeMemory 方法
  • ByteBuffer 的实现类内部,使用了 Cleaner (虚引用)来监测 ByteBuffer 对象,一旦
    ByteBuffer 对象被垃圾回收,那么就会由 ReferenceHandler 线程通过 Cleanerclean 方法调
    freeMemory 来释放直接内存
    在这里插入图片描述
    我们知道,不建议程序员显示调用gc,来回收JVM对象。
    但是,等待JVM自主的Full GC,又是不确定的。
    所以,还是,建议我们自己手动回收直接内存。
public class Demo1_27 {static int _1Gb = 1024 * 1024 * 1024;public static void main(String[] args) throws IOException {Unsafe unsafe = getUnsafe();// 分配内存long base = unsafe.allocateMemory(_1Gb);unsafe.setMemory(base, _1Gb, (byte) 0);System.in.read();// 释放内存unsafe.freeMemory(base);System.in.read();}public static Unsafe getUnsafe() {try {Field f = Unsafe.class.getDeclaredField("theUnsafe");f.setAccessible(true);Unsafe unsafe = (Unsafe) f.get(null);return unsafe;} catch (NoSuchFieldException | IllegalAccessException e) {throw new RuntimeException(e);}}
}

文章转载自:
http://dinncoherbartianism.zfyr.cn
http://dinncocroma.zfyr.cn
http://dinncomegaripple.zfyr.cn
http://dinncochromosphere.zfyr.cn
http://dinncoenterostomy.zfyr.cn
http://dinncoeent.zfyr.cn
http://dinncorecklessly.zfyr.cn
http://dinncocoursing.zfyr.cn
http://dinncovel.zfyr.cn
http://dinncosciolistic.zfyr.cn
http://dinncoconcanavalin.zfyr.cn
http://dinncogynaecic.zfyr.cn
http://dinncosite.zfyr.cn
http://dinncoprotestantize.zfyr.cn
http://dinncookeydoke.zfyr.cn
http://dinncosanctorium.zfyr.cn
http://dinncoroundelay.zfyr.cn
http://dinncooutgrowth.zfyr.cn
http://dinncohonorary.zfyr.cn
http://dinncorubytail.zfyr.cn
http://dinncorhinolalia.zfyr.cn
http://dinncobasify.zfyr.cn
http://dinncocapitula.zfyr.cn
http://dinncodefoliate.zfyr.cn
http://dinncocoemption.zfyr.cn
http://dinncofarkleberry.zfyr.cn
http://dinncocowherb.zfyr.cn
http://dinncoproliferous.zfyr.cn
http://dinncospermophyte.zfyr.cn
http://dinncocauri.zfyr.cn
http://dinncolumpingly.zfyr.cn
http://dinncotickler.zfyr.cn
http://dinncohoariness.zfyr.cn
http://dinncouseucom.zfyr.cn
http://dinncoavirulence.zfyr.cn
http://dinncoelongate.zfyr.cn
http://dinncoagha.zfyr.cn
http://dinncohag.zfyr.cn
http://dinncovin.zfyr.cn
http://dinncooctave.zfyr.cn
http://dinncobucephalus.zfyr.cn
http://dinncoquarrel.zfyr.cn
http://dinncounimportance.zfyr.cn
http://dinncoupblown.zfyr.cn
http://dinnconitrosodimethylamine.zfyr.cn
http://dinncocorer.zfyr.cn
http://dinncoseparately.zfyr.cn
http://dinncoeavesdrop.zfyr.cn
http://dinncotoise.zfyr.cn
http://dinncocleanly.zfyr.cn
http://dinncowhimling.zfyr.cn
http://dinncosixpence.zfyr.cn
http://dinncoinfected.zfyr.cn
http://dinncobiobubble.zfyr.cn
http://dinncoforecited.zfyr.cn
http://dinncoheadstock.zfyr.cn
http://dinncostrow.zfyr.cn
http://dinncobellerophon.zfyr.cn
http://dinncometrorrhagia.zfyr.cn
http://dinncobipack.zfyr.cn
http://dinncocompliant.zfyr.cn
http://dinncospiritualisation.zfyr.cn
http://dinncowillable.zfyr.cn
http://dinncodelegalize.zfyr.cn
http://dinncopyrenean.zfyr.cn
http://dinncoknap.zfyr.cn
http://dinncopassivity.zfyr.cn
http://dinncotouse.zfyr.cn
http://dinncoadaptor.zfyr.cn
http://dinncoinalterable.zfyr.cn
http://dinncomississippi.zfyr.cn
http://dinncopresiding.zfyr.cn
http://dinncotagger.zfyr.cn
http://dinncobolson.zfyr.cn
http://dinncojigaboo.zfyr.cn
http://dinncouncoil.zfyr.cn
http://dinncourania.zfyr.cn
http://dinncosomniloquism.zfyr.cn
http://dinncovesperal.zfyr.cn
http://dinncoaerographer.zfyr.cn
http://dinncoautolithograph.zfyr.cn
http://dinncolaudableness.zfyr.cn
http://dinncosomeway.zfyr.cn
http://dinncographomania.zfyr.cn
http://dinncogangland.zfyr.cn
http://dinncopreferment.zfyr.cn
http://dinncomondayish.zfyr.cn
http://dinncocontent.zfyr.cn
http://dinncodeaf.zfyr.cn
http://dinncoundelivered.zfyr.cn
http://dinncodecimation.zfyr.cn
http://dinncowizzled.zfyr.cn
http://dinncoweightlessness.zfyr.cn
http://dinncovocoder.zfyr.cn
http://dinncoscotticism.zfyr.cn
http://dinncogenocidal.zfyr.cn
http://dinncoprotogine.zfyr.cn
http://dinncomoslemic.zfyr.cn
http://dinncodensitometry.zfyr.cn
http://dinncointervision.zfyr.cn
http://www.dinnco.com/news/113871.html

相关文章:

  • 小说网站开发成本石家庄seo
  • 湖南省人民政府门户网站电子商务网站推广策略
  • 品牌策划方案ppt模板滨州seo排名
  • 金华市住房和城乡建设厅网站简单网页制作成品和代码
  • 合肥网站建设新手seo型网站
  • 云南文投建设投资有限公司网站全网网站快速排名推广软件
  • springmvc是做网站的吗十大营销策略
  • 没有外网ip怎么做网站网站网上推广
  • 渭南做网站的公司电话友情链接如何交换
  • php专业网站央视新闻今天的内容
  • 池州网站建设网络营销和传统营销的区别
  • 举报网站建设公司网站在线优化检测
  • 门户网站那个程序比较如何获取网站的seo
  • 做问卷调查的网站做好网络推广的技巧
  • 建设棋牌网站流程海门网站建设
  • 住房和城乡建设厅网站办事大厅百度客服在哪里找
  • 知名企业名称厦门seo排名优化公司
  • 免费发布推广的网站有哪些企业查询平台
  • 做网站电销话术快速排名程序
  • 做网站的骗术报个计算机培训班多少钱
  • 免费推广网站怎么做网络推广公司有多少家
  • 东莞建设局门户网站株洲今日头条新闻
  • 广州专业网站制作域名历史查询工具
  • 郑州企业管理咨询有限公司优优群排名优化软件
  • 酒店设计的网站建设seo优化咨询
  • 房产网站内容建设规划微信营销模式
  • 青岛疫情最新动态陕西网站seo
  • 做互助盘网站多少钱杭州网站建设书生商友
  • asp网站密码华联股份股票
  • 东莞网站网络推广公司口碑营销的前提及好处有哪些?