1

I am working on a project, in which need to load thousands of object's data in HashMap/Hashtable/ArrayList. No issue with small application but it goes out of memory in large application.

Please suggest how to handle this situation?

RTA
  • 1,251
  • 2
  • 14
  • 33
  • You will need to increase java heap space. Calculate the required space and increase it accordingly. [`This answer`](http://stackoverflow.com/questions/13076097/read-big-text-file-to-hashmap-heap-overflow) of mine explains how to calculate and increase it. – Amit Deshpande Nov 06 '12 at 13:33
  • 2
    Try this http://stackoverflow.com/questions/1565388/increase-heap-size-in-java – Vivek Nov 06 '12 at 13:34
  • My application is not a eclipse application. Its a desktop standalone Java application. so how to increase Memory from Java code? – RTA Nov 06 '12 at 13:42
  • Yea as @Thom points out use a `random access file` or as @Randy says a database.. I`d say if you are flexible enough to support a DB for it. Go for it – Mukul Goel Nov 06 '12 at 13:43
  • read Vivek comment to know how to give the heap size you want using the command line...Just out of curiosity, what is a large application for you ? Do you have a out of memory exception ? – LB40 Nov 06 '12 at 13:43
  • 2
    You cannot increase the allocated amount of heap space in a running Java program. If you find that you are running out of memory then increase the heap size prior to launching your app. – Perception Nov 06 '12 at 14:40
  • Vivek's comment is not theoretical since he gives you the command line. – LB40 Nov 06 '12 at 15:27

5 Answers5

3

Why does it need to be in memory? I would say use a random access file.

Thom
  • 14,013
  • 25
  • 105
  • 185
1

I am wondering about your requirement here, why is it important to load thousands of objects at the same time? Can you provide more details about it? Perhaps your implementation can be reworked so that you don't need that many objects loaded in memory.

imnd_neel
  • 109
  • 1
  • 9
  • After seeing your responses, it is still my opinion that you should rethink the need to load all the objects in memory at the same time. Since you cannot know how many objects you'll need to load, you cannot find out for sure what your heap size should be, even if you try to increase it. I think you should consider using a File or Database as suggested by @Thom/Randy. – imnd_neel Nov 06 '12 at 14:33
1

don't read all the data into memory at once, or expand the memory available prior to execution.

You can not increase heap size programmatically.

NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
1

Either you have to increase the memory or u have to check through the code whether there is some point where application is creating many objects(probably in loops) . If it is there nullify that objects(make sure it will not affect your application flow). Another option try to use a lighter object (say A bean can be made lighter to a string object if you can properly override the toString() method. This will also increase the performance of your application)

Rahul
  • 93
  • 13
0

You could use a cache system, like ehcache for example. That would give you some control over the "memory" used. There's other cache implementation, ehcache might not suit your needs.

Jonathan Drapeau
  • 2,610
  • 2
  • 26
  • 32