Re: Class with only methods - less memory?
- From: as4109@xxxxxxxxx
- Date: 24 Aug 2006 14:39:00 -0700
redefined.horizons@xxxxxxxxx ha escrito:
I am working on a program that creates hundreds, if not thousands, of
objects in memory. This often causes the program to run out of memory
and shut down.
I am looking for a solution to this. I am curious how much memory is
required from objects that belong to a class that has only method
definitions, and no member variables or other objects included in its
definition.
Object vptr size (probably the same as a native pointer) +
garbage-collection overhead size (probably at least the size of a
native pointer); so at least 8 bytes on a 32-bit machine. There may
also be overhead for the synchronization primitives, or the need to do
synchronization may mean that an Object has to be aligned to a native
cache-line (probably 8 or 16 bytes, possibly more).
On a 32-bit machine, those limitations as well as limitations on what
part of the memory is available to user programs may mean you cannot
have more than about 12 million objects in a single JVM. That would
require 3 GB of memory.
I want to know if I can solve my memory problem by creating an object
that accesses its "data" or member variables off of the hard disk
instead of from RAM. I know this will impose a serious speed or
performance penatly. However, if this will work I can take some steps
to get around the speed problem.
If you have the disk-space, your operating-system will probably allow
you to create a swap-file big enough (3 GB) to allow that JVM to exist
in virtual memory. I just tried it: first I tried running "java
-Xms1100M" and got the error
Error occurred during initialization of VM
Could not reserve enough space for object heap
Then I added a 1-GB swap file and tried the same command; it was
successful. The exact way to add virtual memory depends on your
operating system.
Will I reduce RAM usage by creating hundreds or thousands of objects
from a class that only contains method definitions?
You'll save some memory, but what can you do with a class that
contains only method definitions, and no state? Some ideas to ponder:
1. Use "java.lang.ref.WeakReference" or "java.util.WeakHashMap", or
both, or something similar.
2. Store your data-structure in SQL tables.
3. java.io.RandomAccessFile
4. Store your data in some other Map, but only put entries in the map
for things that are not in their default state.
5. byte[] can store 8 true-false values per byte of memory it uses
Exactly what you choose depends on your application, and you aren't
very specific here.
*David*
.
- Follow-Ups:
- Re: Class with only methods - less memory?
- From: Arne Vajhøj
- Re: Class with only methods - less memory?
- References:
- Class with only methods - less memory?
- From: redefined . horizons
- Class with only methods - less memory?
- Prev by Date: plotting with java
- Next by Date: Re: plotting with java
- Previous by thread: Re: Class with only methods - less memory?
- Next by thread: Re: Class with only methods - less memory?
- Index(es):
Relevant Pages
|