Skip to content

直接使用ObjectOutputStream遇到buffer太小的问题,如何提升I/O效率 #1

Open
@cleanupcrashanrjank

Description

@cleanupcrashanrjank

feature(特征):

["stack_regex":"ObjectOutputStream","problem_type":"io_small_buffer"]

description(解决方案描述)

配合BufferedOutputStream使用

verify(验证结果)

compare ObjectOutputStream ObjectOutputStream+ByteArrayOutputStream
I/O time 499 1
I/O cost time 162.8ms 87.3ms

code(代码)

ByteArrayOutputStream bos = null;
ObjectOutputStream oos = null;
FileOutputStream fos = null;
try{
    bos = new ByteArrayOutputStream(4096);
    oos = new ObjectOutputStream(bos);
    oos.writeObject();
    oos.flush();

    fos = new FileOutputStream(file);
    bos.writeTo(fos);
    bos.flush();
}
catch(Exception ex) {
    ex.printStack()
}
finally {
    if (oos != null) {
        oos.close();
    }
    if (bos != null) { 
        bos.close();
    }
    if (fos != null) {
        fos.close();
    }
}

Metadata

Metadata

Labels

Disk(磁盘)small buffer, main thread; SQL cause the Inefficient I/O(过小Buffer,主线程I/O和低效的SQL)Janky and ANR(卡顿)

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions