1

right now, i need to load huge data from database into a vector, but when i loaded 38000 rows of data, the program throw out OutOfMemoryError exception. What can i do to handle this ?

I think there may be some memory leak in my program, good methods to detect it ?thanks

MemoryLeak
  • 7,322
  • 23
  • 90
  • 133
  • Thanks for your answers, but i don't want to increase my heap size, I think i may need to find a good algorithm. – MemoryLeak Aug 12 '09 at 13:16
  • 2
    @Hooligan: Tell us why you are loading the data into a vector and maybe we can offer you a good algorithm. – Brian Aug 12 '09 at 13:31
  • our old program encounter such a outofmemoryerror, so i need to optimize it, and the program loaded all the data into field and then process base on that. – MemoryLeak Aug 12 '09 at 13:47
  • @Hooligan: you are missing @Brian's point. You need to tell us something more about the data and/or what your program doing with it. Otherwise, nobody can offer any specific solutions to avoid loading those 38,000 rows into a huge Vector that is too big to fit into your available memory. – Stephen C Aug 12 '09 at 15:27
  • thank you, i think i have fixed it, because our program loaded some redundant data! – MemoryLeak Aug 13 '09 at 04:42

10 Answers10

7

Provide more memory to your JVM (usually using -Xmx/-Xms) or don't load all the data into memory.

For many operations on huge amounts of data there are algorithms which don't need access to all of it at once. One class of such algorithms are divide and conquer algorithms.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
3

If you must have all the data in memory, try caching commonly appearing objects. For example, if you are looking at employee records and they all have a job title, use a HashMap when loading the data and reuse the job titles already found. This can dramatically lower the amount of memory you're using.

Also, before you do anything, use a profiler to see where memory is being wasted, and to check if things that can be garbage collected have no references floating around. Again, String is a common example, since if for example you're using the first 10 chars of a 2000 char string, and you have used substring instead of allocating a new String, what you actually have is a reference to a char[2000] array, with two indices pointing at 0 and 10. Again, a huge memory waster.

user44242
  • 1,168
  • 7
  • 20
1

You can try increasing the heap size:

 java -Xms<initial heap size> -Xmx<maximum heap size>

Default is

java -Xms32m -Xmx128m
Tommy
  • 4,011
  • 9
  • 37
  • 59
1

Do you really need to have such a large object stored in memory?

Depending of what you have to do with that data you might want to split it in lesser chunks.

Alberto Zaccagni
  • 30,779
  • 11
  • 72
  • 106
1

Load the data section by section. This will not let you work on all data at the same time, but you won't have to change the memory provided to the JVM.

T0xicCode
  • 4,583
  • 2
  • 37
  • 50
1

Maybe optimize your data classes? I've seen a case someone has been using Strings in place of native datatypes such as int or double for every class member that gave an OutOfMemoryError when storing a relatively small amount of data objects in memory. Take a look that you aren't duplicating your objects. And, of course, increase the heap size:

java -Xmx512M (or whatever you deem necessary)

Daniil
  • 1,398
  • 2
  • 17
  • 28
1

You could run your code using a profiler to understand how and why the memory is being eaten up. Debug your way through the loop and watch what is being instantiated. There are any number of them; JProfiler, Java Memory Profiler, see the list of profilers here, and so forth.

Alex Feinman
  • 5,393
  • 1
  • 30
  • 48
0

Let your program use more memory or much better rethink the strategy. Do you really need so much data in the memory?

JoshJordan
  • 12,676
  • 10
  • 53
  • 63
Boris Pavlović
  • 63,078
  • 28
  • 122
  • 148
0

I know you are trying to read the data into vector - otherwise, if you where trying to display them, I would have suggested you use NatTable. It is designed for reading huge amount of data into a table.

I believe it might come in handy for another reader here.

Helen Neely
  • 4,666
  • 8
  • 40
  • 64
0

Use a memory mapped file. Memory mapped files can basically grow as big as you want, without hitting the heap. It does require that you encode your data in a decoding-friendly way. (Like, it would make sense to reserve a fixed size for every row in your data, in order to quickly skip a number of rows.)

Preon allows you deal with that easily. It's a framework that aims to do to binary encoded data what Hibernate has done for relational databases, and JAXB/XStream/XmlBeans to XML.

Wilfred Springer
  • 10,869
  • 4
  • 55
  • 69