添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

以前的上传代码中使用了

URL url = new **URL**(urlStr);
conn = (HttpURLConnection) url.openConnection();
....省略
out = conn.getOutputStream();
conn.setRequestMethod("POST");
conn.connect();
byte[] bufferOut = new byte[1024 * 1024];
int bytes = 0;
 while ((bytes = in.read(bufferOut)) != -1) {
     out.write(bufferOut, 0, bytes);

顺着OOM时候的堆栈,查看源码。
write的时候 PosterOutputStream作为ByteArrayOutPutStream的子类,直接使用了super.write,所以直接查看ByteArrayOutPutStream#write(byte b[], int off, int len)即可
在这里插入图片描述
write的时候,将目标数据(数组)写入到ByteArrayOutputStream#buf中,若buf不够大,则扩容至2倍。
注意:扩容时,需要3倍的内存才能成功扩容。

ByteArrayOutPutStream#write源码

public synchronized void write(byte b[], int off, int len) {
    if ((off < 0) || (off > b.length) || (len < 0) ||
        ((off + len) - b.length > 0)) {
        throw new IndexOutOfBoundsException();
    **ensureCapacity**(count + len);
    System.arraycopy(b, off, buf, count, len);
    count += len;
 private void ensureCapacity(int minCapacity) {
    // overflow-conscious code
    if (minCapacity - buf.length > 0)
        grow(minCapacity);
private void grow(int minCapacity) {
    // overflow-conscious code
    int oldCapacity = buf.length;
    int newCapacity = oldCapacity << 1;//增长为2倍
    if (newCapacity - minCapacity < 0)
        newCapacity = minCapacity;
    if (newCapacity - MAX_ARRAY_SIZE > 0)
        newCapacity = hugeCapacity(minCapacity);
    buf = Arrays.copyOf(buf, newCapacity);//新的数组最少需要旧数组两倍的内存

为何会使用到PosterOutputStream

getOutPutStream的时候,若不是streaming,就使用PosterOutputStream
#TODO 链接

public boolean streaming() {
        return this.fixedContentLength != -1 || this.fixedContentLengthLong != -1L || this.chunkLength != -1;

解决方式ByteArrayOutPutStream#write引起的OOM

1.设置超大内存。按照最坏情况估计,设置为最大上传文件的3倍内存。(ps:这里仅仅考虑了扩容时的内存,需要再添加一些内存为其他数据)
2.使用conn.setFixedLengthStreamingMode或者conn.setChunkedStreamingMode,避免使用ByteArrayOutPutStream。ps:需要目标服务器支持。

java版本:java8
启动参数:-XX:+UseConcMarkSweepGC -Xmx400m -Xms400m -Xmn30m -XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:G:/学习/gclog.log -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=G:/学习/dump.hprof

参数说明:
-Xmx400m -Xms400m 最大堆内存 400M,最小堆内存400M, 老年代=400m-30m=370m
-Xmn30m 新生代30M 默认 SurvivorRatio 8, eden:s0:s1为8:1:1,所以新生代为9,即30m*0.9=27m
MetaspaceSize 为本地内存。 非堆。

打算上传的a.apk只有345M ,堆内存400M,老年代370M,看起来是够的

    public static void main(String[] args) throws IOException {
        File file = new File("G:/学习/a.apk");
        System.out.println(file.length()/1024/1024);
        FileInputStream fileInputStream = new FileInputStream(file);
        OutputStream out = new ByteArrayOutputStream();
        byte[] bytesRead = new byte[1024*1024*8];
        int n = 0;
        int times = 0;
        while ((n = fileInputStream.read(bytesRead)) != -1) {
            try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); }
            System.out.println(++times*8 + "m");
            out.write(bytesRead, 0, n);
        System.out.println("----"+((ByteArrayOutputStream) out).size()/1024/1024);

使用jmap -heap jpsid查看堆内存

使用JVisualVM查看堆内存增长

在这里插入图片描述
在128M 即将申请256M内存之前,先尝试回收内存。回收后
137.8M, 370-137.8=242.2M, 老年代仍小于256M ,因此OOM。
在这里插入图片描述
gc日志
gclog.log中
[ParOldGen: 253984K->141506K(378880K) 可以看出,老年代内存从248M回收到了137.8M。

2019-10-23T16:57:15.205+0800: 51.679: [Full GC (Allocation Failure) [PSYoungGen: 2312K->0K(27136K)] [ParOldGen: 253984K->141506K(378880K)] 256296K->141506K(406016K), [Metaspace: 9290K->9290K(1058816K)], 0.0327092 secs] [Times: user=0.08 sys=0.00, real=0.03 secs] 
2019-10-23T16:57:15.238+0800: 51.712: [GC (Allocation Failure) [PSYoungGen: 0K->0K(27136K)] 141506K->141506K(406016K), 0.0109249 secs] [Times: user=0.05 sys=0.00, real=0.01 secs] 
2019-10-23T16:57:15.249+0800: 51.723: [Full GC (Allocation Failure) [PSYoungGen: 0K->0K(27136K)] [ParOldGen: 141506K->141136K(378880K)] 141506K->141136K(406016K), [Metaspace: 9290K->9147K(1058816K)], 0.0228731 secs] [Times: user=0.05 sys=0.00, real=0.02 secs] 
 PSYoungGen      total 27136K, used 707K [0x00000000fe200000, 0x0000000100000000, 0x0000000100000000)
  eden space 23552K, 3% used [0x00000000fe200000,0x00000000fe2b0c38,0x00000000ff900000)
  from space 3584K, 0% used [0x00000000ffc80000,0x00000000ffc80000,0x0000000100000000)
  to   space 3584K, 0% used [0x00000000ff900000,0x00000000ff900000,0x00000000ffc80000)
 ParOldGen       total 378880K, used 141136K [0x00000000e7000000, 0x00000000fe200000, 0x00000000fe200000)
  object space 378880K, 37% used [0x00000000e7000000,0x00000000ef9d4238,0x00000000fe200000)
 Metaspace       used 9159K, capacity 9426K, committed 9984K, reserved 1058816K
  class space    used 1064K, capacity 1120K, committed 1280K, reserved 1048576K

OutputStream OutOfMemoryError when sending HTTP
Understanding the Java Garbage Collection Log
URLConnection 使用流的问题

本文产生的原因上传一个大文件文件的时候报了OOM查看代码以前的上传代码中使用了URL url = new **URL**(urlStr);conn = (HttpURLConnection) url.openConnection();....省略out = conn.getOutputStream();conn.setRequestMethod("POST");conn.con... ByteArrayOutputStream是字节数组输出流,在内存中创建了一个字节数组,所有发送到输出流的数据都会保存到该字节数组的缓冲区中.     1.ByteArrayOutputStream的构造方法: public ByteArrayOutputStream() {} public ByteArrayOutputStream(int size) {} 第一个构造方法默认创...
1. ByteArrayOutputStream使用说明 字节数组输出流在内存中创建一个字节数组缓冲区,所有发送到输出流的数据保存在该字节数组缓冲区中。创建字节数组输出流对象有以下几种方式。 下面的构造方法创建一个32字节(默认大小)的缓冲区。 OutputStream bOut = new ByteArrayOutputStream(); 另一个构造方法创建一个大小为n字节的缓冲区。 Outp...
ByteArrayOutputStream 对byte类型数据进行写入的类 相当于一个中间缓冲层,将类写入到文件等其他outputStream。它是对字节进行操作,属于内存操作流 源码解析: public class ByteArrayOutputStream extends OutputStream { 从上述代码中可以看出ByteArrayOutputStream继承了Out...
一、Maven编译过程中出现java.lang.OutOfMemoryError: Java heap space 错误,提示如下:                java.lang.OutOfMemoryError: Java heap space                  at java.util.Arrays.copyOf(Arrays.java:2786)
1. 使用内存缓存:可以将网络请求的结果缓存到内存中,避免频繁的网络请求,从而减少内存占用。 2. 使用分页加载:如果数据量较大,可以采用分页加载的方式,每次只加载部分数据,避免一次性加载过多数据导致OOM。 3. 使用压缩算法:可以对网络请求的数据进行压缩,减少数据传输的大小,从而减少内存占用。 4. 及时释放资源:在网络请求完成后,及时释放相关资源,避免内存泄漏。 5. 优化代码:可以对代码进行优化,减少不必要的内存占用,如避免创建过多的对象等。