SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot DuncanM's Avatar
    Join Date
    Apr 2005
    Posts
    100
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript: How to Fail Memory Allocation Gracefully?

    Hello.

    I have written several Javascript programs that accept user input (numbers) from a textarea box. The data then gets converted to floating point numbers for use in the actual programs.
    As far as I know, the general procedure follows:
    Step 1: Any data in a textarea box is by default one big string so it must be input to the Javascript program for processing as a string. Call this string TEXTAREABOXSTRING
    Step 2: Split the string (separate the numbers by whitespace) up into the individual entries, placing the individual entries into a raw array of strings. Call this array RAWARRAYOFSTRINGS
    Step 3: Convert all the array entries into numbers and place them in the proper numeric array for further use in the program. Call this array NUMERICARRAYTHATWILLBEUSEDINPROGRAM

    Up to now, all the programs have explicitly enforced an upper limit as to the size of the numeric array that the program will accept. There is a statement on the HTML page telling users about this limit, and in the Javascript code itself there is a check to ensure the numeric array is no larger than the specified limit.

    However, this check only happens at Step 3, when NUMERICARRAYTHATWILLBEUSEDINPROGRAM is filled.

    I would like to try something different; I would like to start using dynamic arrays. However, I am wondering what checks to include to ensure the memory allocation can be performed. And if it cannot, how do I code the program so that it fails gracefully?

    For example, say a user tries to deliberately break the program. He copies and pastes a HUGE block of numbers from an e-book. Some scenarios:
    (i) TEXTAREABOXSTRING is so huge, the user doesn't have enough memory and the program fails.
    (ii) The program accepts TEXTAREABOXSTRING okay, but there is not enough memory for both TEXTAREABOXSTRING and RAWARRAYOFSTRINGS. So the program fails at Step 2.
    (iii) The program accepts TEXTAREABOXSTRING and RAWARRAYOFSTRINGS okay, but there is not enough memory for TEXTAREABOXSTRING, RAWARRAYOFSTRINGS, and NUMERICARRAYTHATWILLBEUSEDINPROGRAM. So the program fails at Step 3.
    In fact, triple the memory is being used for the same data even though it is only the numeric array that will be used in the program (once it is filled).
    Is there a way to de-allocate memory being used by TEXTAREABOXSTRING? It is not going to be used later in the program and I would like to reduce the chance that a memory allocation error might happen later.

    What can I do to ensure memory allocation failures happen gracefully?
    (Should I even worry about it, or is it handled internally by the browser?)

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,710
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by DuncanM View Post
    Is there a way to de-allocate memory being used by TEXTAREABOXSTRING? It is not going to be used later in the program and I would like to reduce the chance that a memory allocation error might happen later.
    About the only thing that you can do in that case is to assign an empty string to that text area box.

    Quote Originally Posted by DuncanM View Post
    What can I do to ensure memory allocation failures happen gracefully?
    (Should I even worry about it, or is it handled internally by the browser?)
    Garbage collection is automatically handled by the browser. There isn't much that JavaScript provides to deal with such issues.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Zealot DuncanM's Avatar
    Join Date
    Apr 2005
    Posts
    100
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    About the only thing that you can do in that case is to assign an empty string to that text area box.

    . . .

    Garbage collection is automatically handled by the browser. There isn't much that JavaScript provides to deal with such issues.
    Okay. Thanks for the info.

    I am accustomed to coding in C++, so usually wrap memory allocation blocks in a try block, and catch exceptions in a catch block. I thought there might be some similar steps that can be taken in JavaScript.

    Good suggestion, assigning an empty string to TEXTAREABOXSTRING once it has been broken up into its individual entries and saved in RAWARRAYOFSTRINGS.
    That way, I am not using up twice the memory saving what is essentially the same data in two places.

    I suppose I could also delete RAWARRAYOFSTRINGS one entry at a time as the entries are converted to numbers and saved in NUMERICARRAYTHATWILLBEUSEDINPROGRAM.
    For example, I could loop through RAWARRAYOFSTRINGS as follows:

    Code:
    for (var i = 0; i < RAWARRAYOFSTRINGS.length; i++){
         NUMERICARRAYTHATWILLBEUSEDINPROGRAM[i] = parseFloat(RAWARRAYOFSTRINGS.shift());
    }
    That way, as one array grows the other shrinks (I think). Again, I can avoid using up twice the memory saving what is essentially the same data in two places.

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,826
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by DuncanM View Post
    I am accustomed to coding in C++, so usually wrap memory allocation blocks in a try block, and catch exceptions in a catch block. I thought there might be some similar steps that can be taken in JavaScript.
    JavaScript does have try and catch blocks just as C++ does.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •