• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

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

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you use -Xmx switch when running your Java application to limit the heap size?
 
Sheriff
Posts: 22040
108
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot query the amount of memory an object uses, and you cannot control it either.
 
Praveen Kumar Singh
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic