&引用;OutOfMemoryError:Java堆空间;我的程序出错了

&引用;OutOfMemoryError:Java堆空间;我的程序出错了,java,linux,amazon-ec2,out-of-memory,heap,Java,Linux,Amazon Ec2,Out Of Memory,Heap,请看一下下面的代码 public void createHash() throws IOException { System.out.println("Hash Creation Started"); StringBuffer hashIndex = new StringBuffer(""); AmazonS3 s3 = new AmazonS3Client(new ClasspathPropertiesFileCredentialsProvider());

请看一下下面的代码

public void createHash() throws IOException
{
    System.out.println("Hash Creation Started");

    StringBuffer hashIndex = new StringBuffer("");


    AmazonS3 s3 = new AmazonS3Client(new ClasspathPropertiesFileCredentialsProvider());
    Region usWest2 = Region.getRegion(Regions.US_EAST_1);
    s3.setRegion(usWest2);

    strBuffer = new StringBuffer("");


    try
    {
        //List all the Buckets
        List<Bucket>buckets = s3.listBuckets();

        for(int i=0;i<buckets.size();i++)
        {
            System.out.println("- "+(buckets.get(i)).getName());
        }


        //Downloading the Object
        System.out.println("Downloading Object");
        S3Object s3Object = s3.getObject(new GetObjectRequest("JsonBucket", "Articles_4.json"));
        System.out.println("Content-Type: "  + s3Object.getObjectMetadata().getContentType());


        //Read the JSON File
        BufferedReader reader = new BufferedReader(new InputStreamReader(s3Object.getObjectContent()));
        while (true) {
            String line = reader.readLine();
            if (line == null) break;

           // System.out.println("    " + line);
            strBuffer.append(line);

        }

        JSONTokener jTokener = new JSONTokener(strBuffer.toString());
        jsonArray = new JSONArray(jTokener);

        System.out.println("Json array length: "+jsonArray.length());


        for(int i=0;i<jsonArray.length();i++)
        {
            JSONObject jsonObject1 = jsonArray.getJSONObject(i);

            //Add Title and Body Together to the list
            String titleAndBodyContainer = jsonObject1.getString("title")+" "+jsonObject1.getString("body");


            //Remove full stops and commas
            titleAndBodyContainer = titleAndBodyContainer.replaceAll("\\.(?=\\s|$)", " ");
            titleAndBodyContainer = titleAndBodyContainer.replaceAll(",", " ");
            titleAndBodyContainer = titleAndBodyContainer.toLowerCase();


            //Create a word list without duplicated words
            StringBuilder result = new StringBuilder();

            HashSet<String> set = new HashSet<String>();
            for(String s : titleAndBodyContainer.split(" ")) {
                if (!set.contains(s)) {
                    result.append(s);
                    result.append(" ");
                    set.add(s);
                }
            }
            //System.out.println(result.toString());


            //Re-Arranging everything into Alphabetic Order
            String testString = "acarus acarpous accession absently missy duckweed settling";
            String testHash = "058 057 05@ 03o dwr 6ug i^&";
            String[]finalWordHolder = (result.toString()).split(" ");
            Arrays.sort(finalWordHolder);


            //Navigate through text and create the Hash
            for(int arrayCount=0;arrayCount<finalWordHolder.length;arrayCount++)
            {

                Iterator iter = completedWordMap.entrySet().iterator();

                while(iter.hasNext())
                {
                    Map.Entry mEntry = (Map.Entry)iter.next();
                    String key = (String)mEntry.getKey();
                    String value = (String)mEntry.getValue();

                    if(finalWordHolder[arrayCount].equals(value))
                    {
                        hashIndex.append(key); //Adding Hash Keys
                        //hashIndex.append(" ");
                    } 
                }

            }

            //System.out.println(hashIndex.toString().trim());

            jsonObject1.put("hash_index", hashIndex.toString().trim()); //Add the Hash to the JSON Object
            jsonObject1.put("primary_key", i); //Create the primary key
            jsonObjectHolder.add(jsonObject1); //Add the JSON Object to the JSON collection

            System.out.println("JSON Number: "+i);
        }

        System.out.println("Hash Creation Completed");
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}
public void createHash()引发IOException
{
System.out.println(“哈希创建已启动”);
StringBuffer hashIndex=新的StringBuffer(“”);
AmazonS3 s3=新的AmazonS3客户端(新的类路径属性FileCredentialsProvider());
Region usWest2=Region.getRegion(Regions.US_EAST_1);
s3.设置区域(usWest2);
strBuffer=新的StringBuffer(“”);
尝试
{
//列出所有的桶
ListBucket=s3.ListBucket();

对于(int i=0;i构造
StringBuffer
对象以在
JSONTokener
内部传递它是一个非常糟糕的主意。此类具有直接来自
Reader
InputStream
的构造函数,因此您的代码应该是这样的:


JSONTokener jTokener=newjsontokener(newbufferedreader(newinputstreamreader)(s3Object.getObjectContent()))

您的java堆内存已经用完。在32位系统上,您可以将堆内存增加到4gb。如果您在64位系统上,您可以更高。如果您在32位系统上要求超过4gb,您将从java获得无效值,它将退出

下面是如何在64位系统上使用cmd命令将内存堆设置为6gb:

java -Xmx6144M -d64

您正在循环之外声明hashIndex

StringBuffer hashIndex = new StringBuffer("");

...

for(int i=0;i<jsonArray.length();i++) {

    hashIndex.append(...);
stringbufferhashindex=newstringbuffer(“”);
...

对于(int i=0;我认为所有建议增加堆大小的答案都应该删除。这样的建议就像指示断肢的受害者在哪里获取更多血液。这是否意味着这会导致内存问题?只是好奇而已?加载了所有数据的字符串缓冲区肯定会占用额外的空间。答案是一个组合关于你和@Lance Java。如果我能选择两者作为选择答案,我很高兴:)增加Java堆空间绝不是一个解决办法,只是暂时的权宜之计。@Hexafrance:我不喜欢提供否决票,但我同意你的看法。我正在浏览600亿条记录,因此Java堆大小意味着“什么都没有”@gloryofsucces我没有否决这个答案,尽管我会看到背后的理由。技术正确性+1。答案是你和@Andremoniy的组合。如果我能选择两者作为选择答案,我很高兴:)