my dog learned polymorphism*
The moose likes Java in General and the fly likes How to limit the size of a object in terms of MB ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to limit the size of a object in terms of MB ?" Watch "How to limit the size of a object in terms of MB ?" New topic
Author

How to limit the size of a object in terms of MB ?

Praveen Kumar Singh
Ranch Hand

Joined: Mar 04, 2009
Posts: 43
Hi All,

I have a requirement in my project which I want to discuss here.
In my project we have a configuration file which actually define the cache size.
This cache size is not in terms of no of nodes but in terms of MB, which I feel is completely logical as we always know the memory size of system.
So my question here is, Do we have some way by which we can limit the size of an java object in terms of MB ?
I searched on net and got some class which do some operation to know this which is very costly in terms of CPU cycle.
What I am looking for is some light weight way to do this.

Praveen
SCJP, SCWCD, SOA
Martin Vanyavchich
Ranch Hand

Joined: Sep 16, 2008
Posts: 241
Can you use -Xmx switch when running your Java application to limit the heap size?


SCJP 6, OCMJD 6, OCPJWSD 6
I no good English.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

You cannot query the amount of memory an object uses, and you cannot control it either.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Praveen Kumar Singh
Ranch Hand

Joined: Mar 04, 2009
Posts: 43
I agree with Rob,
i am doing R&D on it from long time and yet not got anything.
Few library claim to do so but actually they just calculating the difference of java heap size,
which is very indirect and very costly in terms of performance.

java.lang.instrument is also not usefull in anyway.

@Martin : yes, by this way, we can control the heap size of application but not the object
i need to control the size of object.
Mukesh Ranjan
Greenhorn

Joined: Jun 24, 2009
Posts: 22
It is true that you cannot control the memory size allocation for an object but you can use Runtime class to use available memory and maz memory and can implement logic based on that, but but but it is not a perfect way of handling memory.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

it is not possible to reduce the size of object created by JVM.
because Objects are created at runtime and who knows how much memory will be taken by JVM to create objects.

you can control the heap size but that will not reduce the memory taken by objects created at runtime.
if it will not get enough memory then it will simply throw "OutOfMemory" error

if we can change the configuration of JVM by ourself then i think all the advantages of Java will became useless.
Like Java become more prone to virus.


SCJP6.0,My blog Ranchers from Delhi
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

why not you often call System.gc().this will garbage collect the unusable object which can save the memory.

But call to System.gc() will totally depends upon the JVM whthere it will collect the unusable objects or not we cannot force it.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

shanky sohar wrote:why not you often call System.gc().this will garbage collect the unusable object which can save the memory.

No, it will not. You cannot force the garbage collector to run. If you call System.gc(), you are only suggesting to the JVM that it might be good to run the garbage collector. But there's no guarantee that it will actually run.

Also, calling System.gc() does not prevent OutOfMemoryErrors. You only get an OutOfMemoryError if there is really no memory left to allocate; if the garbage collector failed to make enough memory available.

In general, it's better to just let the garbage collector do its job automatically. Calling System.gc() can even have negative effects, like dramatically lowering the performance of your program.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Jesper Young wrote:Calling System.gc() can even have negative effects, like dramatically lowering the performance of your program.


Thanks buddy.i donot know about this.
Gaurav Raje
Ranch Hand

Joined: Jul 23, 2010
Posts: 136
It is known to crash your system at times....... Especially if your application has native code. Even more if it has TSR (Terminate but Stay Resident) native code in it. Bot not just if it does.....


one way i can think of to limit the size of an object is to use all static primitive data structures... Like arrays. It is just the closest you can get
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Gaurav Raje wrote:It is known to crash your system at times....... Especially if your application has native code. Even more if it has TSR (Terminate but Stay Resident) native code in it. Bot not just if it does.....


Well... no. Only if your native code has bugs in it.

On to the OP's original question about limiting the size of an object. Note that every class creates objects of some precisely fixed size, depending only on its member variables -- except for arrays. Perhaps what you're really interested in is limiting the size of arrays that can be created? One can imagine using AOP (aspect-oriented programming) do this in at least a limited way.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to limit the size of a object in terms of MB ?