Why local variables are stored on stack?

Hi,
Can anyone tell why local variables are stored on stack and objects on heap?
Thanks
Neha

paulcw wrote:
I'm pretty sure that Java doesn't store any objects on the stack.I wouldn't be so sure if I were you.
Why wouldn't Java be open to possible optimizations? It's definately faster to keep an object on the stack than letting it go through the garbage collector.
In fact the Java compiler from Sun has had this optimization in the pipeline for a long time. I'm sure it's due "anytime soon" now.
So it's wrong to say that objects are always stored on the heap. They can be store on the stack if it can be proved that their lifetime is limited to that of a method call. This requires the compiler to perform a so called "escape analysis" which has been in place for a long time in Sun compilers.
If you want to learn more, Google for "escape analysis".

Similar Messages

  • SQL environment variable into a local variable in stored procedure ?

    How do i set a SQL environment variable in a session ? How can i read an SQL environment variable into a local variable declared in my stored procedure ? Does any exceptions occur in the process ?
    I need this very urgently, can anyone help ?
    thanks
    srini

    You can use a pipelined table function to return it, which is covered here:
    http://blog.mclaughlinsoftware.com/plsql-programming/pipelined-functions-plsql-tables/
    Alternatively, you can use a SQL Object type wrapped by a collection of that SQL object type inside a TABLE call. I've blogged on that too, here:
    http://blog.mclaughlinsoftware.com/2009/03/23/object-record-collections/
    If you need something more, leave a comment on the blog. I'm not here on the forum too frequently. Hope that helped.

  • ADF BC / Why bind variables are mandatory in the sql query

    I got this error during view object excecution in the component browser :
    (oracle.jbo.SQLStmtException) JBO-27122: Erreur SQL lors de la préparation d'une instruction. Instruction : SELECT * FROM (Select distinct(socialgroup.socialgroup_i) from socialgroup, socialgroupmember, lodgingallocation, lodge
    where socialgroup.socialgroup_i = socialgroupmember.socialgroup_i and socialgroupmember.t_socialgrouprole_i = t_rolegroup_ipar.fgetflextypologyclassitem_i(t_rolegroup_ipar.fisbeneficiary) and socialgroupmember.datefrom <= :DateTo and nvl(socialgroupmember.dateto, :DateFrom) >= :DateFrom and socialgroupmember.requester_i = lodgingallocation.requester_i and lodgingallocation.datefrom <= :DateTo and nvl(lodgingAllocation.DateTo, :DateFrom) >= :DateFrom and lodgingallocation.lodge_i = lodge.lodge_i) QRSLT ORDER BY "SOCIALGROUP_I"
    ----- LEVEL 1: DETAIL 0-----
    (java.sql.SQLException) Tentative de définition d'un nom de paramètre qui ne se trouve pas dans le SQL: T_SICategory_I
    The bind variables T_SICategory_I is not yet use in the sql query but will be used later so i defined it for the view object.
    Is it a reason that the run time check this ? It would be more convenient to set the bind variables early when defining the view object and add them later during the development iteration.

    Design-time defined named bind variables can be marked as required, or not.
    This is decided by the use in the where clause or in view criteria. In my case the bind variable was not used in where clause neither in view criteria and that causes the error.
    May be i would be nice to add a check box (a flag) that enable or disable the bind variables for this checking so we will be able to let it defined even we removed some part of the query corresponding to the corresponding restriction or we defined it earlier for a part of the query that is not yet defined.
    In my current case i fully defined the bind variables but would refine my query later ... in that cause i would have to remove this bind variable and loose all the definitions to run this view object.
    sorry for my english ...

  • Where do you store local final vaiable in? Stack or Heap or else?

    Hi all,
    I know local variables are stored in the stack, but how about local final variables? Are they stored in the stack too? What are their life times? Do they "die" with the method after the method is finished?
    class Example{
    void doStuff() {
    final String z = "local variable";
    Where is z stored? Is it gone when doStruff() is finished?
    Thanks a lot

    davy_wei wrote:
    Thanks DrClap, but it's not the answer to my question.Yes, it was. You asked where final local variables are stored and DrC gave you the correct answer--all local variables, whether final or not, are on the stack. He answered the question you asked.
    >
    See the complete example,
    class Example {
    private String x="Outer";
    void doStuff() {
    String x="local variable";
    class InnerClass {
    public void exampleMethod() {
    System.out.println("x is "+x);
    System.out.println("Local variable x is "+x);
    MyInner mi=new InnerClass();
    mi.doStuff();
    otherMethodInTheExample(mi);    // you can pass the reference of mi to other method
    }You are not able to compile it because the virable x is local. The InnerClass can not access the local variables in the enclosing method, which is doStuff(). Before even after the method completes, the inner class object mi might still be alive on the heap if the method otherMethodInTheExample() store the reference in an instance variable. That's the reason why InnerClass can not access the local variable x.
    However, if we change the local variable x to final, the code can compile and work.The local variable is still stored on the stack. However, since is value is known and known not to change before the inner class gets it, the inner class can get a copy of its value. This would not work if it were non-final.
    Now I have the question. Why the InnerClass can access x when it is set to final. Is x store in the heap too?The inner class gets its own copy of the variable. Since its value is final, it's okay to have multiple copies floating around, since it's impossible for them to get out of sync.

  • Where are stored the separated variables data?

    Guys,
    Does anyone here know where are stored the data for separated variables?
    I'm doing some research in my enviroment to know the capacity needed for process demand and I need to count the separated variables usage of my database.
    Any helps are appreciated.
    Regards,
    Luiz

    Hi Luiz,
    Separated instance variables are stored in the Engine's "PDETACHEDFIELD" table.
    Dan

  • Default initialisation of member variables and local variables

    I don't understand why member variables are initialized with default values by Java.
    Objects are initialized with "null" and primitives with "0", except boolean, which is initialized with "false".
    If these variables are used locally they are not initialized. The compiler requires them to be initialized by the programer, for example "String s = null".
    Why? What is the use of that difference?
    And why are arrays always initialized with default values, no matter if they are member variables or local variables? For example String[] s = new String[10]; s[0] to s[9] are initialized with "null", no matter if "s" is a local or member variable.
    Can someone please explain that strange difference, why it is used? To me it has no sense.

    Most of the time I have to initialize a local variable
    with "String s = null" in order to use it because
    otherwise the compile would complain. This is a cheap
    little trick, but I think everyone uses it.
    I wouldn't agree with "most of the time". The only cases where it is almost necessary to do that is when the variable should be initialized in a loop or a try-catch statement, and that doesn't happen too often.
    If local variables were initiliazed automatically without a warning it would be a Bad Thing: the compiler could tell when there is a possibility that a variable hasn't been assigned to and prevent manymanymany NullPointerExceptions on run time.
    And you didn't answer me why this principle is not
    used with arrays if it is so useful as you think.
    Possibly it is much more difficult to analyse the situation in the case of arrays; what if values are assigned to the elements in an order that depends on run time properties such as values returned from a random number generator.
    The more special rules one has to remember, the more
    likely one makes errors.I agree, but what is the rule to remember in this case?

  • Local variable for an Array of fixed size

    Hello,
    I have a two multirate loops in a VI. 
    In one loop, I want to refer an fixed sized array initialized in the other loop.
    But I coudn't name the array, so I can't refer it.
    Is there any way to refer it?
    Thanks,
    Young.

    If you need a local variable, there is no other way than to create an indicator for it.
    In LabVIEW there is no "fixed length" array : you can always add or remove an array element, and you don't need to declare it as in other languages. They are intrinsic dynamic objcets. Of course, the memory manager has to cope with this, that's why it's often better to initialize an array, to give it its final size immediately, resulting in faster running programs. However, this is only noticeable with relatively large arrays (> 10000-100000 elements).
    May be you should explain in more details what you intend to do, because trying to reproduce a C approach in LV is probably not the best thing to do. For instance, you said that you are initializing your array in a first loop. You mean that you re-initialize the array at each iteration ? I suppose no, so may be you could put the initialize step out of the loop, and wire the array to your two parallel loops.
    Remember also that local variables are not always good programming solutions, since using them will generate copies of their content each time they are refered to. And that can low down your program very significantly...
    Message Edité par chilly charly le 11-05-2005 05:05 PM
    Chilly Charly    (aka CC)
             E-List Master - Kudos glutton - Press the yellow button on the left...        

  • Variation between local variables

    Hello, I use version 7.1 for developing a scada application for seeing liquid tanks levels, and  I need to see the flow depicted at tank indicator from one to another in this way.
    The first tank increase it liquid level only when an electrovalve is activated, the level is writen in a numeric indicator.
    The liquid flows from a second tank only when a second electrovalve is activate.
    When both electrovalves are activated the level variation must be saw both indicator and controls.
    I fail in third point, Is it possible to make it as I madeor anything similar? The vi I made became crazy when I turn on.
    Thank you very much.
    Attachments:
    levels.vi ‏46 KB

    1.  Your VI is designed to only run one time.  It runs once. Then it stops.  My guess is your are using the Run Continuously button.  Don't.  It is only intended for special situations like if you want to debug a VI.  If you want your VI to keep running, then use a while loop around it and a stop button.
    2.  You need a wait statement in your loop.  Without a wait statment,  your VI will run as fast as the CPU allows.
    3.  Local variables are almost always used incorrectly by new LabVIEW users.  Read this thread Why some people say Local Variables are bad... for some reasons why.
    Then I would recommend looking at the online LabVIEW tutorials
    LabVIEW Introduction Course - Three Hours
    LabVIEW Introduction Course - Six Hours

  • Best way to remove local variables

    Hello,
    I have been told that I need to remove all the local variables in my VI, because they are slow.  I can not seem to find a good option for replacing them but keeping the same functionality.
    What I am using the local variables for is to update 3 different graphs on the front panel from 3 different sources.  Each of these sources are located in while loops so that data can be acquired asynchronously.  In closing these 3 while loops is a case statement.  The case statement is for switching between acquiring and saving the data or just acquiring data.  The local variables are used to update the indicators and graphs from the acquiring and saving data case.  It is these local variables that I need to remove.
    Does anyone have any suggestions?
    -- Z

    Zurvan wrote:
    I have been told that I need to remove all the local variables in my VI, because they are slow.
    Who told you that?
    As others have said, local variables typically don't cause slowdown (They cause additional datacopies in memory, have the potential of causing race conditions and cause messy code).
    What is "slow" in your definition? (missing data, cannot keep up with the instrument, slugging UI, etc.).
    What is the CPU use when your program runs?
    So apparently you have a loop that updates all indicators from local variables. What is the loop rate of that display loop? Do you spin that millions of times per second or pace it at a reasonable rate? How much data is in the graphs?
    Remember, a graph does not need to update unless the data changes.
    LabVIEW Champion . Do more with less code and in less time .

  • LabVIEW 2010 local variable behavior

    Hi -
    I upgraded to LabVIEW 2010 from 2009SP1 and the attached sub VI no longer works correctly.  This is a state machine case structure that gets called inside a loop.  I'm setting a Boolean local variable true, then on the next call when the case changes the variable has been reset to false. It did not do this in LV2009.
    Someone will immediately tell me I have a race condition, but these local variables are not being written anyplace else, just within these separate cases.
    See the attached code, specifically, the local variable "Enable" is getting reset when going from case "Enable" to case "Turn on".
    Anyone else seen this issue, or can shed any light?
    I fixed the VI by writing the value in each case, but this different behavior makes me wonder what other issues are lurking in LV2010?
    Thanks
    Solved!
    Go to Solution.
    Attachments:
    eq99_cycling.vi ‏42 KB

    This code looks more like some sort of an Action Engine than a state machine, but not quite.  It gets called repeatedly from a state machine, and it decides what the next state will be depending upon the inputs.  Instead of using all those local variables, I would use uninitialized shift registers (nothing wired to the left side).  They hold their value from one call to the next.  One more case needs to be added in this case.  An unnamed, default case that would set the initial values of all the shift registers.  Make it the default case and don't even name it.  It will be called first automatically because the shift register is uninitialized so the default will be called.  Inside the default case, set all the shift registers to desired values.  Next time this subvi is called, those values will still be there.  That is as long as nothing is wired on the left side,  Whatever is wired to the state shift register will be the next case called.
    See attached vi.  It is broken because I did not wire anything to the indicators.
    - tbob
    Inventor of the WORM Global
    Attachments:
    eq99_cycling-4[2].vi ‏33 KB

  • Question about Local Variables (Multiple answers welcomed!)

    A couple of questions about Local Variables
    1. Programmers always say: “Do not abuse of Local Variables”. I’d like to know, when and where Local variable are most efficiently used?
    2. If I have to create a couple of local variables, is there anyway to “clone” them without going through the repetitive “create/local variables” mouse click each time? (When I try to copy and paste, it creates a new variables instead of the one that I am trying to reproduce)
    3. Which is faster in execution: Updating a variable through a) writing to property node/value or b) through local variable
    Everyone’s input is welcomed, so if this question is already answered, please
    feel free to add additional comments/answers!

    1. Use Local Variables in user interface code and no where else. The only exception is using a local variable of a cluster output to define the datatype for a bundle by name node.
    2. You can drag copy them then right click to get to a menu of all the currently defined controls and indicators on the VI.
    3. B. The problem with A is that it forces a thread switch to the user interface thread--which can take time if you aren't already in it, and it's a very convoluted process under the hood. NI's advice never update indicator values through a property node unless you absolutely, positively can't figure out some other way of doing it.
    Mike...
    Certified Professional Instructor
    Certified LabVIEW Architect
    LabVIEW Champion
    "... after all, He's not a tame lion..."
    Be thinking ahead and mark your dance card for NI Week 2015 now: TS 6139 - Object Oriented First Steps

  • F8Pro - local variables not displayed in debugger - flashlocalvars.zip (0/1)

    Im not sure if this is me doing something stupid but I have
    been
    playing round with classes and noticed that in the debugger,
    the local
    variables are no longer displayed in the debugger after
    returning from
    a custom class get method. I have managed to work round this
    behaviour for a while but Im now stuck.
    I have posted a sample flash file with actionscript and
    sample class
    at
    http://www.zerofiveone.com/flashlocalvars.zip
    which displays
    this behaviour everytime. View the testscript.as file for
    info on
    where to place your breakpoints.
    If someone could put my out of my misery and let me know if I
    am doing
    something wrong I would apprecaite it.
    If im not doing anything wrong is there a workaround?
    Any help would really....really be greatly appreciated as Im
    stuck
    trying to work a fairly complex bug from some of my
    actionscript and
    this is causing me many headaches.
    Cheers,
    Tom

    Its a known problem then :(
    This has been round for a long while, annoying really.
    Thanks for the reply anyway.
    Cheers,
    Tom
    On Sun, 11 Jun 2006 14:45:35 +0000 (UTC), "the fleece"
    <[email protected]> wrote:
    >I use a work around if I can't see an object by sticking
    a :
    >
    >_global.TEMP=myObject
    >
    >and look at it through the global scope

  • Wire or local variable?

    The best method to pass data inside the same VI is wire or local variable? or the same? Which one is more effient? Thanks.

    Everything Dennis said above is true.
    Let's look at a seasoned text programmer suddenly forced to use LabVIEW: The first mental hurdle the new LabVIEW programmer might face is the fact that there are no visible "variables", something that is central to text based programming languages. Looking for "variables", he might stumble on local variables and suddenly he thinks he found the solution to keep programming the old-fashioned way.
    What you might see as a result, is a LabVIEW program that has all controls and indicators completely disconnected, all lined up on the top or left side of the diagram (Similar to the variable definitions section in classic text code ). Then, on the right side everything is done writing and reading from local variables. Each disconnected code segment duplicating a programming statement, e.g. [Length] x [Height] = [Area] (where [x] are now a local variables).
    This is NOT in the spirit of LabVIEW! In LabVIEW, the wire itself is the variable. Controls and indicators are just user interface access ports to the data, they contain their own data copies and are read or updated whenever dataflow might service them. Local variables are just secondary access points to the same controls or indicators.
    Lets look at a (slightly flawed) analogy: I need to give you a CD with my LabVIEW program. I can (1) hand it to you directly (=wire) or I can (2) drop it off at the front desk (=front panel ) and you can pick it up later (local variable). As you can see, case (1) is more efficient and easier to debug. We both immediately know if the transaction succeeded. Case (2) could have a race condition, e.g. what happens if you try to pick it up before I had a chance to drop it off?
    Local variables should be used sparingly. There are two main uses:
    (1) Sometimes, it is required to either read from indicators or write to controls programmatically. This could be to initialize controls with a set of default values or similar.
    (2) Exchanging data between two independent parallel processes in the same VI. For example to stop all while loops with a single control.
    Anything that can be done with a wire should be done with a wire. If a wire is not sufficient, a shift register might do the trick. Local variables should only be used as a last resort.
    Remember: Always go with the dataflow!

  • How to declare local variables in PL/SQL stored programs

    Where do I declare local variables in a PL/SQL stored program?
    I get compiler errors with either of the options below:
    FUNCTION GET_PRINCIPAL_BALANCE (CUT_OFF_DATE IN DATE, TITLE_SN IN DATE, TOTAL_BALANCE OUT NUMBER) RETURN NUMBER IS
    TOTAL_BALANCE NUMBER;
    BEGIN
    RETURN TOTAL_BALANCE;
    END;
    FUNCTION GET_PRINCIPAL_BALANCE (CUT_OFF_DATE IN DATE, TITLE_SN IN DATE, TOTAL_BALANCE OUT NUMBER) RETURN NUMBER IS
    BEGIN
    TOTAL_BALANCE NUMBER;
    RETURN TOTAL_BALANCE;
    END;

    Your local variable cannot have the same name as the formal out parameter. This is a procedure example, but since functions should not have out parameters anyway ...
    session2> CREATE PROCEDURE p (p_id IN NUMBER, p_did OUT NUMBER) AS
      2     p_did NUMBER;
      3  BEGIN
      4     p_did := p_id * 2;
      5  END;
      6  /
    Warning: Procedure created with compilation errors.
    session2> show error
    Errors for PROCEDURE P:
    LINE/COL ERROR
    0/0      PL/SQL: Compilation unit analysis terminated
    1/1      PLS-00410: duplicate fields in RECORD,TABLE or argument list are
             not permittedThe proper way to create a function would be something like:
    CREATE FUNCTION f (p_id IN NUMBER) RETURN NUMBER AS
       l_did NUMBER;
    BEGIN
      l_did := p_id * 2;
      RETURN l_did;
    END;You should really assign a value to the variable before you return it.
    John

  • Labview FPGA: Why not have local variable without indicator?

    In Labview FPGA, to transfer data among parallel loops, there are three ways or maybe more, local variable, global variable and block memory. The problem with local variable is that it needs an indicator along with, not like global variable. I was thinking why Labview doesn't make a kind of local variable not need indicator? I guess local variable itself doesn't cost resource much compared to global variable, however when it comes into indicator, it will cost more. Just an idea, though. I guess there is a reason NI doesn't do that. 

    As in Labview document, global variable can be used for transfer data among VIs, which local variable isn't capable of. 
    Global variable is not always a better solution. From my experience, it is still unclear among different compilations using local vs global in terms of resources. A thread here http://forums.ni.com/t5/LabVIEW/FPGA-global-variables-vs-front-panel-items/td-p/1407282 talks about that too.
    Stephen, is there any documents say that global makes timing worse? I didn't see this noticeable.

Maybe you are looking for

  • Moving iPhone from broken laptop

    Have wifes XP laptop, screen smashed, keyboard damaged, total loss. Have accesss to drive, drive ok Moving her iPhone to her xp desktop copied last backup from laptop to correct folder location. C:\Documents and Settings\USER NAME\Application Data\Ap

  • Fast Entry Screen for f-54 & F-39

    hai, Any Fast Entry Screen is available for F-54 & F-39 for down payment clearing. govind.

  • Cannot extract the embedded font ArialNarrow,Bold

    I am having this problem with 2 PDF files on multiple machines. I have tried Adobe Reader 8, Acrobat Standard 9 and Reader 9. Does anyone have any suggestions? The PDF will not display past page 2. Arial Narrow Bold is installed on all the machines t

  • Visa Gift Card Dilema

    I heard that for a pre paid Visa Gift Card to work that you must use the same adress that it was first bought with but in my case I bought it at one adress and then I moved so when I went to buy something on ebay it didnt work. Can somebody help me?

  • Where do I install Avid Meridien codecs for use in Final Cut Pro X on Lion?

    I'm using FCPX and imported a couple of files that were output with the Avid Meridien codec. When I try to use the files in FCPX, I can hear the audio, but I get no video. How do I get FCPX to see the codecs?