【问题标题】:How to fix MEMORY LIMIT EXCEEDED in my Java code?如何在我的 Java 代码中修复 MEMORY LIMIT EXCEEDED?
【发布时间】:2019-04-24 02:21:17
【问题描述】:

我正在尝试在平台 lightoj.com 中提交问题。 问题链接:https://vjudge.net/problem/LightOJ-1088

我正在尝试为这个问题提交我的 java 解决方案。 我做错了什么,它使用的内存比预期的多吗? 我不明白为什么它给出了超出内存限制的判决。

这是我的 JAVA 代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;


public class Main {

    static class FastReader { 
        BufferedReader br; 
        StringTokenizer st; 

        public FastReader() 
        { 
            br = new BufferedReader(new
                    InputStreamReader(System.in)); 
        } 

        String next() 
        { 
            while (st == null || !st.hasMoreElements()) 
            { 
                try
                { 
                    st = new StringTokenizer(br.readLine()); 
                } 
                catch (IOException  e) 
                { 
                    e.printStackTrace(); 
                } 
            } 
            return st.nextToken(); 
        } 

        int nextInt() 
        { 
            return Integer.parseInt(next()); 
        } 

        long nextLong() 
        { 
            return Long.parseLong(next()); 
        } 

        double nextDouble() 
        { 
            return Double.parseDouble(next()); 
        } 

        String nextLine() 
        { 
            String str = ""; 
            try
            { 
                str = br.readLine(); 
            } 
            catch (IOException e) 
            { 
                e.printStackTrace(); 
            } 
            return str; 
        } 
    }

    static int l,r,mid,i1,i2,now;

    static int get(int a, int b, int[] arr, int n) {
        if(arr[n-1]<a || arr[0]>b) return 0;
        l = 0;
        r = n-1;
        while(l<=r) {
            mid = (l+r)/2;
            if(arr[mid]==a) {
                now=mid;
                break;
            }
            if(arr[mid]<a) l = mid+1;
            else {
                r = mid-1;
                now = mid;
            }
        }
        i1 = now;
        l=0;
        r=n-1;
        while(l<=r) {
            mid = (l+r)/2;
            if(arr[mid]==b) {
                now = mid;
                break;
            }
            if(arr[mid]<b) {
                now = mid;
                l = mid + 1;
            } else {
                r = mid - 1;
            }
        }
        i2=now;
        return i2-i1+1;
    }

    public static void main(String[] args) {

        FastReader sc = new FastReader();
        OutputStream outputstream = System.out;
        PrintWriter out = new PrintWriter(outputstream);

        int t = sc.nextInt();
        int n,q;
        int a,b;
        int[] arr = new int[100000];
        long ans;
        int cas = 0;
        while(t--!=0) {
            out.println("Case " + ++cas + ":");
            n = sc.nextInt();
            q = sc.nextInt();
            for(int i = 0; i < n; i++) arr[i] = sc.nextInt();
            while(q--!=0) {
                a = sc.nextInt();
                b = sc.nextInt();
                ans = get(a,b,arr,n);
                out.println(ans);
            }
        }
        out.close();

    }

}

【问题讨论】:

  • 如果你在本地运行这个程序会发生什么?
  • 注意问题中指定的内存限制。通常有时间限制和性能限制。
  • 本地运行完美,因为我不使用像他们这样的庞大数据集。给定的内存限制为 32 MB,时间限制为 2 秒。
  • 可能是您创建了太多垃圾。我建议尝试创建更少的对象。
  • 我只有一个长度为 100000 的整数数组的对象。这太多了吗?没有 PrintWriter、FastReader 和这个,我没有再创建任何对象。

标签: java


【解决方案1】:

我遇到了类似的问题。对我来说,使用以下程序被接受了。

  1. 在for或while循环内分配测试用例的所有变量
  2. 打印数据后使这些变量指向空
  3. 致电System.gc(); 这样多余的内存就会被 java 垃圾收集器清除。

对于您的问题,请执行以下操作。

public static void main(String[] args) {

    FastReader sc = new FastReader();
    OutputStream outputstream = System.out;
    PrintWriter out = new PrintWriter(outputstream);

    int t = sc.nextInt();
    int n,q;
    int a,b;
    long ans;
    int cas = 0;
    while(t--!=0) {
            out.println("Case " + ++cas + ":");
            n = sc.nextInt();
            q = sc.nextInt();
            int[] arr = new int[n];
            for(int i = 0; i < n; i++) arr[i] = sc.nextInt();
            while(q--!=0) {
                a = sc.nextInt();
                b = sc.nextInt();
                ans = get(a,b,arr,n);
                out.println(ans);
            }
            arr=null;
            System.gc();
        }
        out.close();
}

【讨论】:

    猜你喜欢
    • 2020-05-21
    • 1970-01-01
    • 2022-11-07
    • 1970-01-01
    • 2015-12-04
    • 1970-01-01
    • 2016-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多