当前位置: 首页 > 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);}
}

相关文章:

  • 圆周期性显示和消失——瞬态实现(CAD c#二次开发、插件定制)
  • NebulaGraph 图数据库介绍
  • 伏羲万能报名平台小程序系统源码支持核销/快递发货/配送自提/网盘卡密发货
  • 实测推荐:一款能看4K直播的万能播放器,支持多端同步
  • 【51单片机5毫秒定时器】2022-6-1
  • 使用Charles中文版抓包工具进行高效的API调试与性能优化
  • day43/60
  • 桌面小屏幕实战课程: DesktopScreen 12 WiFi AP
  • RPC(Remote Procedure Call)技术解析
  • 面试破局:告别流水账,用“故事思维”重塑自我介绍
  • 视频关键帧提取
  • recipes的版本比较老如何更新到新版本?
  • 【计网】期末复习知识总结
  • 基于STM32的智能书房系统的设计
  • 49-有效的字母异位词
  • chili3d笔记23 正交投影3d重建笔记4 点到线2
  • QT实现一个三轴位移台的控制界面
  • WinAppDriver 自动化测试:C#篇
  • Nginx配置文件介绍和基本使用
  • Instagram和facebook广告对比解析