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

java解决超大二维矩阵数组引起的内存占用过大问题

内存咱不行,咱换磁盘,吃磁盘IO

这里文件一定要删除,不然写入数据会有脏数据问题,他不会清除之前已有的数据。还有就是他的存储量也有上限单个文件2GB

这里我用了一个10000*10000的数组弄了点模拟值,画了个图,完美的解决了我们之前矩阵过大无法计算的问题

public static void main(String[] args) throws Exception {Date date = new Date();File file = new File("C:\\Users\\Desktop\\临时文件\\a");file.delete();file.createNewFile();MMF mmf = new MMF(10000, 10000, "C:\\Users\\18833\\Desktop\\临时文件\\a");for (int i = 1500; i < 2000; i++) {System.out.println("存入数组" + i);for (int j = 1500; j < 2000; j++) {mmf.set(i, j, 255);}}int width = 10000;int height = 10000;BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);Graphics2D g2d = image.createGraphics();for (int i = 0; i < 10000; i++) {System.out.println("画入指定位置" + i);for (int j = 0; j < 10000; j++) {if (mmf.get(i, j) != 0) {image.setRGB(i, j, Color.red.getRGB());}}}g2d.dispose();try {ImageIO.write(image, "png", new File("C:\\Users\\Desktop\\临时文件\\output.png"));} catch (IOException e) {System.err.println("保存图片时出错: " + e.getMessage());}System.out.println("耗时:"+(new Date().getTime() - date.getTime())/1000);
}
package com.hydf.upar.service;import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;public class MMF {private final int rows;private final int cols;private final MappedByteBuffer buffer;public MMF(int rows, int cols, String path) throws Exception {this.rows = rows;this.cols = cols;try (RandomAccessFile file = new RandomAccessFile(path, "rw")) {long size = (long) rows * cols * Integer.BYTES;buffer = file.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, size);}}public int get(int row, int col) {int pos = (row * cols + col) * Integer.BYTES;return buffer.getInt(pos);}public void set(int row, int col, int value) {int pos = (row * cols + col) * Integer.BYTES;buffer.putInt(pos, value);}
}
http://www.lqws.cn/news/527743.html

相关文章:

  • 深入解析synchronized实现原理
  • 【2-入门与调试设置】1.坐标辅助器与轨道控制器
  • 英特尔汽车业务败走中国,喊出“All in”才过两个月
  • 观测云产品更新 | 外部数据源、日志、监控、事件、基础设施等
  • TCP 协议安全性全面分析:漏洞、应用场景与防护策略
  • 芯谷科技--降压型DC-DC转换器D4005
  • [OS_27] 现代应用程序架构
  • ESP32 VSCODE进入menuconfig时ESP-IDF idf.py menuconfig卡进度条,setuptools版本太高解决方法
  • 小程序学习笔记:实现上拉触底加载随机颜色案例全解析
  • 深度剖析 Apache Pulsar:架构、优势与选型指南
  • 图像质量对比感悟
  • [论文阅读] 人工智能 + 软件工程 | AI 与敏捷开发的破局之路:从挫败到成功的工作坊纪实
  • 推荐一个前端基于vue3.x,vite7.x,后端基于springboot3.4.x的完全开源的前后端分离的中后台管理系统基础项目(纯净版)
  • HTML 按钮单击事件示例
  • 2-深度学习挖短线股-4-预测数据计算
  • 前端项目3-01:登录页面
  • 实测推荐:一款能看4K直播的万能播放器,支持多端同步
  • 全面比较帮你确定何时选择SLM而非LLM
  • C# .NET Framework 中的高效 MQTT 消息传递
  • React HOC(高阶组件-补充篇)
  • Django 零基础起步:开发你的网站第一步
  • IDE如何快速切换JLINK版本
  • Redis 持久化
  • Axure版AntDesign 元件库-免费版
  • 广州华锐互动:技术与创意双驱动的 VR 先锋​
  • Python 中的 random 模块
  • 49-有效的字母异位词
  • 设计模式精讲 Day 14:命令模式(Command Pattern)
  • Web基础关键_001_HTML(一)
  • docker环境下java参数传递与获取