如何在Java中保持分配内存,直到分配了3GB的内存?

huangapple 未分类评论48阅读模式
英文:

How to keep allocating memory until 3GB have been allocated in Java?

问题

以下是翻译好的内容:

我真的希望我的标题是正确的,但是让我解释一下我在标题中的确切意思。
我需要编写一个程序,创建一个大小为1MB的byte Array,并将其添加到一个ArrayList<byte[]>中。

我需要一直执行这个操作,直到我添加了3GB的数据量。此外,在每次分配后,我应该使用Thread.sleep(20)

我认为我理解了实现的样子,但是我在计算确切的1MB和变量方面遇到了困难。

以下是我的代码样例:

import java.util.ArrayList;

public class MemLeak
{

    public static void main(String[] args)
    {
        long maxSize = 3221225472L; // 3GB in bytes
        long startSize = 0;

        byte[] byteArray = new byte[1048576]; // 1MB in bytes
        ArrayList<byte[]> list = new ArrayList<byte[]>();

        while (startSize != maxSize) {
            list.add(byteArray);
            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            startSize = startSize + 1048576;

        }

    }
}

在我的情况下,我认为byte Array中的元素数量等于字节大小,但事实并非如此。
变量long maxSize = 3221225472L超出了int范围。

有人能够给我一些指导吗?非常感谢!

英文:

I really hope that my title is correct, but let me explain exactly what I mean by the title.
I need to write a program that creates an byte Array with the size of 1MB and add it in an ArrayList&lt;byte[]&gt;.

I need to be doing that until I have added the amount of 3GB. Also after every allocation I should use Thread.sleep(20).

I think I understand how the implementation should look like, but I'm having difficulties with calculating that exact 1MB and the variables.

Here is how my code looks like:

import java.util.ArrayList;

public class MemLeak
{

	public static void main(String[] args)
	{
		int maxSize = 3221225472; // 3GB in bytes
		int startSize = 0;

		byte[] byteArray = new byte[1048576]; // 1MB in bytes
		ArrayList&lt;byte[]&gt; list = new ArrayList&lt;byte[]&gt;();

		while (startSize != maxSize) {
			list.add(byteArray);
			Thread.sleep(20);
			startSize = startSize + 1048576;

		}

	}
}

In my case I have thought that the number of elements in an byte Array equals to size in bytes, but that doesn't seem to be the case.
The Variable int maxSize = 3221225472 is also out of int range.

Could someone give me some pointers?
Thanks, much appreciated!

答案1

得分: 0

如果将字节数组声明移到循环内部,那么每次迭代都会分配1MB的空间。重复这个过程3072次,总共分配3GB的空间。无需跟踪最大大小。

如果目标是使ArrayList及其数据恰好占用3GB内存,那将会更加困难,因为ArrayList类在数据之外还占用了自己的空间,并且它还执行自己的内部内存管理,因此在需要创建更多内部空间时,它的内存占用可能从小于3GB变为大于3GB。查看ArrayList.add()的源代码以了解我的意思。

英文:

If the byte array declaration is moved inside the loop then each iteration allocates 1MB. Do this 3072 times to allocate 3GB of total space. No need to keep track of maxSize.

If the goal is for the ArrayList + its data to consume exactly 3GB of memory, that will be more difficult, because the ArrayList class consumes its own space outside of your data, and it also does its own internal memory management, so it might go from less than to more than 3GB when it needs to create more internal space. Have a look at the source code of ArrayList.add() to see what I mean.

huangapple
  • 本文由 发表于 2020年5月4日 01:04:02
  • 转载请务必保留本文链接:https://java.coder-hub.com/61578444.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定