System.out打印日志导致内存溢出

客户反馈上传200条数据时程序响应慢,问题源于一个大循环内的日志打印。原来外层循环遍历10万+用户数据,内层循环200次,原本不应造成延迟。测试发现,即使少量的日志打印也会显著降低执行效率,例如4条打印语句使执行时间延长约26倍。深入源码分析揭示,`System.out.println`操作导致了此性能问题。
摘要由CSDN通过智能技术生成

起因 : 由于客户反馈说通过系统上传了200条数据 , 点击上传 , 然后半天没有反应(30分钟左右) ;
原因 : 在做数据对比的时候 , 采用的双重For的方式 , 结果数据量非常大 , 外层循环是数据库中所有的用户数据(10_0000+) , 内层是200 , 按理说是不会出问题的 , 以前做过10000*10000左右的For循环也没有执行这么长时间 , 经过排查 , 问题出在了打印日志上…
**测试 : **
**测试代码 : **

import java.util.ArrayList;
import java.util.List;

public class DateTest {
    public static void main(String[] args) {
        List<String> strList = new ArrayList<>();
        long startTime = System.currentTimeMillis();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        for (int i = 0; i < 1000; i++) {
            for (int j = 0; j < 10000; j++) {
                strList.add("" + i +"--"+ j); //  ①
                System.out.println("程序执行--" + i + "--" + j); // ②
                System.out.println
这段代码有什么问题 import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;import org.apache.commons.compress.archivers.zip.ZipFile;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;public class UnzipFolder { public static void main(String[] args) { String folderPath = "/path/to/folder"; // 文件夹路径 String password = "password"; // 解压密码 File folder = new File(folderPath); if (folder.exists() && folder.isDirectory()) { File[] files = folder.listFiles(); for (File file : files) { if (file.getName().endsWith(".zip")) { // 如果是zip文件 unzip(file, password); } } } else { System.out.println("Folder does not exist or is not a directory."); } } public static void unzip(File file, String password) { byte[] buffer = new byte[1024]; try { ZipFile zipFile = new ZipFile(file); zipFile.setPassword(password); String destDirPath = file.getParent(); for (ZipArchiveEntry entry : zipFile.getEntries()) { String entryName = entry.getName(); InputStream inputStream = zipFile.getInputStream(entry); File destFile = new File(destDirPath, entryName); if (entry.isDirectory()) { destFile.mkdirs(); } else { FileOutputStream outputStream = new FileOutputStream(destFile); int len; while ((len = inputStream.read(buffer)) > 0) { outputStream.write(buffer, 0, len); } outputStream.close(); } inputStream.close(); } zipFile.close(); System.out.println("Unzip " + file.getName() + " successfully."); } catch (IOException e) { System.out.println("Unzip " + file.getName() + " failed."); e.printStackTrace(); } }}
最新发布
06-04
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值